Code has been uploaded github+gitee, at the end of the article has an address
The book connected to the above: in the first few articles, we took three days to simply understand the next interface document Swagger Framework, has completely liberated our previous Word document, and can be debugged online, and when the project started, we can define some empty interface, or can return false data, This really reached the front and back end does not wait for the defect, or is very good, of course, this from the front and back of my said separation or is very far away, today, we will simply build our project structure.
This project is a real project of my own, the data are real, previously built a MVC + EF Code first project, this project is based on this, the previous time I have been set up, so, this series of tutorials will be restarted.
0. The pink part of the thought brain map completed in this period
First, create a. NET Core class library, model data tier
Among them, the Models folder, is the entire Project database table entity class, here is manually created, of course, can also be automatically created, in a future article I will mention that the use of Sqlsugar T4 created, here first buy a foreshadowing.
Then, the Veiwmodels folder, is a stored Dto entity class, in the development, the general interface needs to accept data, return data, I have been so the use of red fruits, and later found that the drawbacks are very large, not only to expose important information (such as mobile phone number, etc.), Also to the data redundancy (such as I need to accept the user's birthday, but also need a specific year, month, day this is three fields, of course you can also manually open, this is only a chestnut, so can not directly use the database entity class to accept), the use of the DTO class conversion, but frequent conversion will be troublesome, do not panic, later , we will use AutoMapper to automatically convert, here to buy a foreshadowing.
The last is Messagemodel and TableModel, we also basic a look can understand, because in front-end interface, need fixed format, and operation, can not send data directly, will error, in future Vue development, will mention this, here bought a foreshadowing. As follows:
/// <summary> ///generic return information class/// </summary> Public classMessagemodel<t> { /// <summary> ///whether the operation was successful/// </summary> Public BOOLSuccess {Get;Set; } /// <summary> ///return information/// </summary> Public stringMSG {Get;Set; } /// <summary> ///Return Data collection/// </summary> PublicList<t> Data {Get;Set; } }
The whole project runs, yes, continue to create the next layer.
Ii. creation of Blog.Core.IRepository and Blog.Core.Repository storage tiers
Here is a simple storage layer: Repository is a management data persistence layer, it is responsible for the data crud (Create, Read, Update, Delete) service layer is the business logic layer, it often needs to access the repository layer. Some netizens say: Repository (warehousing): The coordination domain and the data mapping layer, using a similar interface with the collection to access the domain object. Repository is a separate layer, between the domain layer and the Data map layer (the data access layer). Its existence makes the domain layer feel the presence of the data access layer, which provides a similar set of interfaces that provide access to domain objects for domain layers. Repository is the warehouse manager, the domain layer needs to tell the warehouse manager, the warehouse manager to bring things to it, do not need to know where things are actually put.
We define the irepository layer, provide all the operating interfaces, build the framework today, I simply write an example, and tomorrow we will nest all the methods.
In IAdvertisementRepository.cs, add a sum interface
Public Interface iadvertisementrepository { int Sum (intint j); }
And then in the AdvertisementRepository.cs to implement the interface, remember to add a reference, this should be, will not elaborate.
Public class advertisementrepository:iadvertisementrepository { publicint Sum (intint j) { return I + J; } }
Run the project, work together, and continue down.
The creation of the Blog.Core.IServices and Blog.Core.Services business logic layer is very similar to the BLL layer in the three-tier architecture we normally use
Service layer is only responsible for the repository storage layer of data to call, as to how to interact with the database, it does not go to the tube, so you can achieve a certain degree of decoupling, join the database to change, such as MySQL, the service layer is completely no need to modify, As for the decoupling of the true meaning, it depends on the injection of dependency, which we'll talk about in the next section.
This adds the interface to the iadvertisementservices.
namespace blog.core.iservices{ publicinterface iadvertisementservices { int Sum (intint j);} }
And then implement the interface in the advertisementservices.
Public class advertisementservices:iadvertisementservices { iadvertisementrepository dal = new advertisementrepository ();
Public int Sum (intint j) { return dal. Sum (i, j); } }
Attention! Here is the introduction of three namespaces
Using Blog.Core.IRepository;
Using Blog.Core.IServices;
Using Blog.Core.Repository;
Iv. Creating a Controller interface call
Remove the system default Valuecontroller, manually add a BlogController controller, you can select an empty one, or you can select a default read-write instance. As follows:
[Produces ("Application/json")] [Route ("Api/blog")] [Authorize (Policy="Admin")] Public classBlogcontroller:controller {//Get:api/blog[HttpGet] Publicienumerable<string>Get () {return New string[] {"value1","value2" }; } //GET:API/BLOG/5[HttpGet ("{ID}", Name ="Get")] Public stringGet (intID) {return "value"; } //Post:api/blog[HttpPost] Public voidPost ([Frombody]stringvalue) { } //PUT:API/BLOG/5[Httpput ("{ID}")] Public voidPut (intID, [frombody]stringvalue) { } //DELETE:API/APIWITHACTIONS/5[Httpdelete ("{ID}")] Public voidDelete (intID) {}}
Next, add a reference to the service tier at the application tier
Using Blog.Core.IServices;
Using Blog.Core.Services;
Then, rewrite the Get method
·······//Get:api/blog /// <summary> ///Sum interface/// </summary> /// <param name= "I" >parameter I</param> /// <param name= "J" >parameter J</param> /// <returns></returns>[HttpGet] Public intGet (intIintj) {Iadvertisementservices advertisementservices=Newadvertisementservices (); returnAdvertisementservices.sum (i,j); }
F5 run the project, debug as follows:
Oh, my God! Wrong spicy! Don't panic, do you remember the privilege we added yesterday, eh! Is there, manual simulation login, get token, inject, will not be able to look at an article, and then execute, the result:
V. Conclusion
Well, today's work is here temporarily, you can see the overall project construction, structure, how to quote, how to test, of course, there are still a lot of small problems, such as:
• If each warehouse needs to be so written, at least four times, it will be too troublesome;
• You need to introduce many namespaces each time the controller interface is called
• Wait, wait.
These questions, the next section we will take everyone together to slowly solve!
VI. Code
Github.com/anjoy8/blog.core.git