The need for layering
A project must have its corresponding business logic, and the general business logic cannot be particularly simple. If such a project is written in a layer, then the logic of this layer will become complex, not conducive to later maintenance. If layered according to different functions, each layer is responsible for the corresponding logic, then improve the readability of the program to facilitate later maintenance.
Layering introduction
The project is divided into 5 tiers, and if the project is not particularly complex, you can reduce the number of layers appropriately. The 5 tiers are: business logic layer, data access call, service call, presentation layer and user layer respectively.
Case
Here, a small case is used to show the collaboration of each layer, and the detailed role of each layer will be explained later. The cases are as follows:
Supermarkets sell goods to general customers and retailers, respectively. Now for discount activities, general customers and retailers adopt different discount policies.
Business Logic Layer
This includes the business logic between the entity (model) and the entity (model) involved in the business, and uses the appropriate design pattern to match the code to the design principles, depending on the situation.
The interface that declares data access IProductRepository , this layer does not care about the logic of data data query and storage.
Data access claims
The warehouse model is used here Repository , and the ORM framework is generally used. The common ORM Framework is the Entity Framework and NHibernate.
Service Layer
Projects that typically need to publish services require a service layer that Facde hides business business logic based on the appearance pattern.
Defines the request and return data classes, and the data return type structure is typically
public class ProductListResponse { public bool Success { get; set; } public string Message { get; set; } public IList<ProductViewModel> Products { get; set; } }
And this involves mapping the object, adding ViewModel, which contains only the attributes that need to be viewed by the user (third-party plug-ins are available AutoMapper ).
Presentation Layer
The operating logic of the user's presentation layer. The common patterns are the MVC and MVVM patterns, which are designed to separate the presentation layer logic from the UI layer logic. The advantage of MVVM is that unit tests can be created from the UI layer, but creating a command class for each page Presenter also increases the burden of code maintenance.
Ui
The interface layer that is presented to the user. Typically you need to combine CSS to control page styles and JavaScript page interactions.
Code download
Web Project Structure layering