After the introduction of the last translated ABP Vnext, many of the ABP good lovers were attracted attention.
Then take the strike and try the new ABP.
I am most interested in the new ABP is its template system, so this time using the module system to do a message board example, share to everyone.
Message Board Module
Our Message board module function is very simple, is to provide user message function (nonsense), for the sake of simplicity, the function is very primitive:
- Access the message board via the menu
- Display message at a glance, the items shown are: Title, author and Time
- Any user can create a message, modify or delete their own message
- Administrator can modify or delete any message
- Message only the title and text are plain text (Rich text is not supported).
Using the ABP module system, the database, Api,ui, etc., which are involved in the above functions, are packaged as a module to be reused in future projects ( so simple to reuse?). )
Attention
- The best way to develop ABP is to understand the basic knowledge of ABP (such as entity,repository,application service, etc.).
- The code in this article is written only for testing and functional validation, and may not be followed by the ABP best practices, and should not be used directly in the actual project
Ready? Let's go to next!
Create a project
Create a module project
Using the template provided on the website, pay attention to select ASP.NET Core Mvc Module
, create a module project, named WAKU.MessageBoard
:
Open the module project with VS2017 (WAKU. MessageBoard.sln), you can see that the template generates a number of projects:
These projects follow the official best practices, which are briefly explained here:
folder |
Engineering |
Description |
Use |
App |
WAKU. Messageboard.demoapp |
Web Engineering for testing |
Module development phase Test app available |
Src |
WAKU. Messageboard.application |
Application Services Engineering |
Defining the service's individual implementation classes |
|
WAKU. MessageBoard.Application.Contracts |
Application Services Contract Engineering |
Define Service Interfaces, DTOs |
|
WAKU. Messageboard.domain |
Domain Engineering |
Define Entity |
|
WAKU. MessageBoard.Domain.Shared |
Domain Sharing Project |
Define constant information that can be shared by all projects, etc. |
|
WAKU. Messageboard.entityframeworkcore |
EF Core Engineering |
Define DbContext, providing EF access to the database infrastructure |
|
WAKU. Messageboard.httpapi |
HTTP API Engineering |
Define the controller to expose the methods in the application service |
|
WAKU. MessageBoard.HttpApi.Client |
HTTP API Client Engineering |
Providing client services for the HTTP API |
|
WAKU. Messageboard.mongodb |
MongoDB Project |
Define DbContext, which provides MONGODB access to the database infrastructure (not used in this article) |
|
WAKU. Messageboard.web |
Web Engineering |
Define UI-related resources for modules using pages, views, scripts, etc. |
Tests |
- |
Each test project |
Define test cases for the above projects |
Prepare the Code
-
Create domain
in WAKU. Messageboard.domain
Project waku\messageboard
Create a new folder named Messages
to hold the Domain class for the message board.
In this example, only leave a message in a domain class, create the message class under the Messages folder, with the following code:
using Volo.abp.domain.entities.auditing;namespace WAKU. messageboard.messages{public class Message:fullauditedentity<int> {//<summary>/// Title://</summary> public string Title {get; set;} <summary>//////</summary> public string Content {get; set;} }}
-
Increase the DbContext property
Modify WAKU. Messageboard.entityframeworkcore
Project waku\messageboard\entityframeworkcore
Imessageboarddbcontext
and Messageboarddbcontext
class, increase the Dbset property of message:
[connectionStringName ("messageboard")] public interface Imessageboarddbcontext:iefcoredbcontext {Dbset<me Ssage> Messages {get; set;}}
[connectionStringName ("messageboard")] public class Messageboarddbcontext:abpdbcontext <messageboarddbcontext>, Imessageboarddbcontext {... public dbset<message> Messages {get; set;} ... }
-
Create repository
Modify messageboardentityframeworkcoremodule
to create a default repository for each entity using the ABP:
[DependsOn (typeof (Messageboarddomainmodule), typeof (Abpentityframeworkcoremodule) )]public class messageboardentityframeworkcoremodule:abpmodule{public override void Configureservices (ServiceConfig Urationcontext context) {context. services.addabpdbcontext<messageboarddbcontext> (options =//create default repository for all entity Options. Adddefaultrepositories (includeallentities:true); }); }}
-
Configure the Model builder
Modification for message messageboarddbcontextmodelcreatingextensions
as follows:
public static class messageboarddbcontextmodelcreatingextensions{public static void Configuremessageboard (This ModelBuilder builder, action<messageboardmodelbuilderconfigurationoptions> Optionsaction = null) {... builder. Entity<message> (m = {m.totable) (options. Tableprefix + "Messages", options. Schema); M.configurefullaudited (); }); }}
Modifying the database connection string
Modify the WAKU.MessageBoard.DemoApp
project under appsettings.json
. The. will be localhost
changed (localdb)\\mssqllocaldb
so that SQL Server is not installed locally, and local db is installed by default when VS2017 is installed
{ "ConnectionStrings": { "Default": "Server=(localdb)\\mssqllocaldb;Database=MessageBoardDemoApp;Trusted_Connection=True;MultipleActiveResultSets=true" } }
Increase Database Migration
Right click WAKU.MessageBoard.DemoApp
, select Set as Startup Project
, set it to start Project
Open Package Manager Console
(PMC), set the default project also to app\WAKU.MessageBoard.DemoApp
, execute the following command:
Add-Migration "AddMessage"
-
Build database
Execute in PMC update-database
The resulting database is located under the %userprofile%
folder, named Messageboarddemoapp.mdf
You can view the generated database through the VS2017 menu View
- SQL Server Object Explorer
and confirm that the messages table is also correctly generated with the table named messageboardmessages
:
Create 3 test data for later development:
-
Create service contract
-
in WAKU. MessageBoard.Application.Contracts
Project waku\messageboard
folder, create messages\dto
folder, and create a class named messagedto
:
public class messagedto:fullauditedentitydto< int>{//<summary>///title://</summary> [Required] public string title {get; set;} <summary>///text///</summary> [Required] public string Content {get; set;}}
-
Create imessageappservice interface under Messages
folder:
public Interface Imessageappservice:iasynccrudappservice<messagedto, int>{}
We have inherited Iasynccrudappservice
interface, this interface defines the basic addition and deletion (CRUD) method, has been able to meet our needs, then can add new methods according to the situation.
The Create complete structure is as follows:
Implement service
WAKU.MessageBoard.Application
under the project's WAKU\MessageBoard
folder, create Messages
a folder, and create a MessageAppService
class named:
public class MessageAppService : AsyncCrudAppService<Message, MessageDto, int>, IMessageAppService{ public MessageAppService(IRepository<Message, int> repository) : base(repository) { }}
IMessageAppService
similar to interfaces, inherited AsyncCrudAppService
classes, using the default implementation of the class add and Delete methods, you do not need to define additional methods.
Create a Controller
WAKU.MessageBoard.HttpApi
under the project's WAKU\MessageBoard
folder, create a class to MessageController
expose the methods in the service:
[Remoteservice] [Area ("Message_board")] [Controller] [Controllername ("Messages")] [Route ("Api/message_board/messages")] [Disableauditing]public class Messagecontroller:abpcontroller, imessageappservice{private readonly Imessageappservice _messageappservice; Public Messagecontroller (Imessageappservice messageappservice) {_messageappservice = Messageappservice; } [HttpGet] [Route ("{ID}")] public task<messagedto> getasync (int id) {return _messageappservice. Getasync (ID); } [HttpGet] [Route ("")] public task<pagedresultdto<messagedto>> Getlistasync (pagedandsortedresultrequ Estdto input) {return _messageappservice.getlistasync (input); } [HttpPost] public task<messagedto> createasync (messagedto input) {return _messageappservice.creat Easync (input); } [HttpPost] [Route ("{ID}")] public task<messagedto> updateasync (int id, messagedto input) {Retu RN _messageappservice.Updateasync (ID, input); } [Httpdelete] public Task deleteasync (int id) {return _messageappservice.deleteasync (ID); }}
It can be seen that the class is just a package of service, defines the API routing and parameter forms, the code is slightly cumbersome. The individual wants to be automatically generated by the ABP, as in previous versions, and is expected to be available in a future release.
Start DemoApp
Now we are ready for the module needs the basic services, start the test project WAKU.MessageBoard.DemoApp
, everything is normal to see the start page:
Testing the API with swagger
Added in the address bar of the browser /swagger
, will enter the swagger page:
Can see the controller in the definition of the various methods have been shown, we can test the basic additions and deletions to check the function is normal, here to test the Get method to see if the previous test data can be removed:
Here we get the message ID 2 through the URL, you can see the data can be removed normally.
Summary
To summarize what we have done so far:
- Domain where message was created
- Increase EF Core-related settings
- The database was generated
- Created a service that defines the Crud method
- The API Controller was created, exposing the service method
- Tested the API
So far, we have made a strong ~ Basic service for the message template, next we will use these basic services, do some slightly practical function: Create a UI for the message board, so that users can really use the message function.
If you like this article, please click "Recommended", more welcome to discuss the message, your support is my greatest power!
ABP Vnext Module System Preliminary interview-Create message board module