background
Domain model objects are only used to store application data. The business logic resides in the services layer and manages the data of domain objects. In the service layer, each application entity corresponds to a service class. Are you familiar with this mode, especially in small and medium-sized projects or projects at the beginning of the time, are how convenient how to; Yes, this is the anemia model.
The general style is like this.
1. Web layer: receives user input and transmits data to the service layer;
2. Service layer: processes business logic, permission management and authorization, and communicates with the storage layer;
using BQoolCommon.Interface.Repository.Dapper; using BQoolCommon.Interface.Service; using BQoolCommon.Models.BQoolCommon_SetMain; using BQoolCommon.Models.Enum; using BQoolCommon.Models.ViewModel; using System; using System.Collections.Generic; using System.Configuration; namespace BQoolCommon.Service { public class UserPermissionService : IUserPermissionService { private readonly IInnerSiteMapDapperRep _innerSiteMapDapperRep; private readonly IInnerSiteMapService _innerSiteMapService; private readonly IAccountChannelRelService _accountChannelRelService; private readonly IUserMgmtService _userMgmtService; public UserPermissionService( IInnerSiteMapDapperRep innerSiteMapDapperRep, IInnerSiteMapService innerSiteMapService, IAccountChannelRelService accountChannelRelService, IUserMgmtService userMgmtService) { _innerSiteMapDapperRep = innerSiteMapDapperRep; _innerSiteMapService = innerSiteMapService; _accountChannelRelService = accountChannelRelService; _userMgmtService = userMgmtService; }...Copy the code
3. Storage layer: communicate with database and persist data;
using System.Linq;
using BQoolCommon.Interface.Factory;
using BQoolCommon.Interface.Repository.Entity;
using BQoolCommon.Models.BQoolCommon_SetMain;
using BQoolCommon.Models.ViewModel;
namespace BQoolCommon.Repository.Entity
{
public class WeChatSubscribeEntityRep : GenericEntityRep<WeChat_Subscribe>, IWeChatSubscribeEntityRep
{
public WeChatSubscribeEntityRep(IBqoolSetMainDbContextFactory factory) : base(factory)
{
}
}
}
Copy the code
Problem to spy out
The problem is the service layer, which takes on too many responsibilities, such as business logic, permission checking, and so on, which violates the single responsibility principle and creates a lot of dependencies. As business complexity increases, the code contained in the service layer can become very large and complex. The service layer should contain the management of application logic and user session. The domain layer should contain the business logic that handles the state of the session that is relevant to the business.
Improvement ideas
We need to move business logic from the service layer to the domain model, which has the advantage that the service layer can be solely responsible for application logic (such as data validation, authorization checking, start and end transactions, etc.) and the domain model can be solely responsible for its associated business logic. With electricity system for example, when the architecture design can completely for orders, goods, inventory, and other fields model to model, the relevant business can be respectively in different areas in the model, some are likely to repeat business code will be concentrated in a place, thus reducing the possibility of a copy-and-paste, this is the model of the pump.
impact
The congestion model makes the service class smaller and has a single responsibility. For example, the CRUD and other operations on goods can be placed in two different service classes, one for the CRUD operations on goods and one for other operations related to goods. This makes service classes small, loose, and testable, while reducing the cost of understanding and reuse by others.
conclusion
1. From the standard and long-term perspective, the hyperemia model must be appropriate.
2, But if it is only a small project, for fast, of course, it is to develop low cost anemia model.