Today I finally did the first task, learning the first instance after the API. Sales Setup Development API.
First, Hierarchy 1. API Layers
The project structure mainly has five layers, namely API, BizModel, Data, Dbmodel, Logic.
2. The role of each layer
First, for the microservices architecture, the front and back ends are separated, the backend provides data support, and the Frontend UI gets the data through the API.
The API receives parameters for the controller, and the controller layer does not do any logical processing, just through the model receive parameters, call logic to do the logical processing.
BizModel is the Entity Data model that corresponds to the front-end UI. As we all know, Dbmodel is the Entity Data Model for the corresponding database field, and BizModel corresponds to the UI control. For example: a user, including the ID, username, password, email and other fields, and the front-end may not just fill in the user's fields, may fill in the user's classmate, family information, the Dbmodel will not be able to correspond. So BizModel passes data for the front end, which can correspond to fields in multiple Dbmodel. BizModel is divided into Dbmodel by logic and then SQL action.
Data layer for database read and write operations.
Dbmodel is the Entity Data model.
Logic is the logical layer, the BizModel is separated into Dbmodel, and the data layer is called to perform read and write actions.
Second, API Building 1. Corresponding data table field construction Dbmodel
View the data tables that need to be manipulated, write the corresponding fields
public class Preferencemodel {public Guid Adminuserkey {get; set;} public string Msccode {get; set;} public int Shippingaddresssource {get; set;} public bool Audit1 {get; set;} public bool Audit2 {get; set;} public int Autofeedback {get; set;} public int Ebaymsgshowlines {get; set;} public bool DirectSalesAudit1 {get; set;} public bool DirectSalesAudit2 {get; set;} public bool NewTemplate {get; set;} }
2. Corresponding UI reference construction BizModel
public class preferenceentity {public Guid Adminuserkey {get; set;} public string Msccode {get; set;} public int? Shippingaddresssource {get; set;} public bool? Audit1 {get; set;} public bool? Audit2 {get; set;} public int? Autofeedback {get; set;} public int? ebaymsgshowlines {get; set;} public bool? DirectSalesAudit1 {get; set;} public bool? DirectSalesAudit2 {get; set;} public bool? newtemplate {get; set;} }
3.Data Layer Database reading and writing method
Do not show in detail here, self-processing can be.
4.Logic layer processing logic and invoke the data layer method to perform the action
Here is the updated logic, in the project sales settings are divided into two pages, three update actions, corresponding to the same data table. So the logic is not very complex, but want to update the different data, do not affect the other data, with only one API, it is necessary to do some processing.
Originally my idea is the data layer three methods to update each other, in the logic layer to differentiate, this method is not not, but in the principle of streamlining and maintenance, only one method update, the logical layer processing. So the idea is that the controller receives the parameter call logic, passes the parameter through entity to logic, and then, in logic, determines that if the parameter of a field does not pass the new value, the original data value is used for the update. The idea is correct and the next step is implemented.
Encountered problem int, bool Type field can not judge non-empty, so do not know the default type int 0 and bool default false is not passed over the value.
WORKAROUND: In bizmodel bool and int are defined as bool, and int?, using invisibility, you can tell if the variable is empty, if it is not empty, then cast the parameter, and update the data.
5.Controller definition
API only GET, PUT, POST, delete methods, in order to standardize, controller naming must be standardized. The controller receives the parameters and calls logic to return the data.
At this point, the main build of the API is complete, then build to see if there is an error, no error trial run, into the swagger test.
After the test is successfully released, the release is completed using postman for testing.
Webapi instance--First API