当前位置:网站首页>Implementing DDD based on ABP -- domain logic and application logic
Implementing DDD based on ABP -- domain logic and application logic
2022-07-26 19:40:00 【JavaMonsterr】
This paper mainly introduces the problem of multiple application layers , Including the reason and realization . Through understanding, it introduces how to distinguish domain logic and application logic , What are the right practices , Which are not recommended or wrong practices .
One . The problem of multiple application layers
1. Introduction to multiple application layers
I wonder if you will encounter a situation , adopt ABP Build a backend API project , At first it was for Web End item ( such as ,Vue) Providing back-end interface services , With the development of the project and the complexity of the business , Added the mobile terminal App, The official account 、 Small program etc. , Not only for Web End supply API service , But also for mobile terminals App, The official account 、 Applets, etc API service . This scenario is the problem of multiple application layers . In other words, you now need to build a system with multiple applications :

- Web Applications : For example, the ASP.NET Core MVC technology , It is mainly used to show products to users , Visitors can view products , Login and authentication are not required .
- Back end management applications : For example, the front end is Vue, Back end ASP.NET Core API, It is mainly used to add, delete and modify products .
- Mobile applications : For example, the front end is uni-app, The back end is ASP.NET Core API, adopt REST Interface and back-end communication .
2. Examples of multiple application layers
Because of the complexity of the business , Each application system has its own different application service methods , Different inputs and outputs DTO, Unused authentication and authorization rules, etc , So if you integrate all business logic into an application system , It will make the system more difficult to develop 、 Maintenance and testing , And lead to potential Bug risk . therefore , Create a separate application layer for each application , And they all share the core domain logic with a single domain layer . For more specific explanation , Create different for each application type .csproj project :
- Back end management applications :IssueTracker.Admin.Application and IssueTracker.Admin.Application.Contracts project
- Web Applications :IssueTracker.Public.Application and IssueTracker.Public.Application.Contracts project
- Mobile applications :IssueTracker.Mobile.Application and IssueTracker.Mobile.Application.Contracts project
Two . How to distinguish domain logic and application logic
Usually ,DDD Business logic in includes domain logic and application logic . Domain logic consists of the core domain rules of the system , Application logic implements application specific use cases . That's all , But it is not easy to distinguish between domain logic and application logic .
1. Create an organization in the domain services layer Organization
Let's create in the domain service Organization This example , To explain as briefly as possible :
public class OrganizationManager:DomainService
{
private readonly IRepository<Organization> _organizationRepository; //Organization The warehouse of
private readonly ICurrentUser _currentUser; // The current user
private readonly IAuthorizationService _authorizationService; //Authorization Service for
private readonly IEmailSender _emailSender; // Email service
// Public constructors , Dependency injection
public OrganizationManager(IRepository<Organization> organizationRepository, ICurrentUser currentUser, IAuthorizationService authorizationService, IEmailSender emailSender)
{
_organizationRepository=organizationRepository;
_currentUser=currentUser;
_authorizationService=authorizationService;
_emailSender=emailSender;
}
// Create a new organization
public async Task<Organization> CreateAsync(string name)
{
// If the organization has the same name , So throw an exception [ correct ]
if(await _organizationRepository.AnyAsync(x=>x.Name==name))
{
throw new BusinessException("IssueTracking:DuplicateOrganizationName");
}
// Check whether you have the permission to create [ error ]
await _authorizationService.CheckAsync("OrganizationCreationPermission");
// Record ⽇ Records [ error ]
Logger.LogDebug($"Creating organization {name} by {_currentUser.UserName}");
// Create a new organization
var organization = new Organization();
// Send an email to remind [ error ]
await _emailSender.SendAsync("[email protected]", " New organization ", " New organization name :"+name);
// Return an organization instance
return organization;
}
}- Domain services do not do permission verification , Permission verification is done in the application layer
- User concept is related to application layer or presentation layer , The log should not contain the user name of the current user
- Create a new organization , And send mail , This business logic should also be placed in the application layer
2. Use domain services in the application layer to create organizations Organization
public class OrganizationAppService:ApplicationService
{
private readonly OrganizationManager _organizationManager; // The domain services of the organization
private readonly IPaymentService _paymentService; // Payment services
private readonly IEmailSender _emailSender; // The mail service
// Public constructors , Dependency injection
public OrganizaitonAppService(OrganizationManager organizationManager, IPaymentService paymentService, IEmailSender emailSender)
{
_organizationManager=organizationManager;
_paymentService=paymentService;
_emailSender=emailSender;
}
// Create organization
[UnitOfWork][ correct ] // The unit of work , Used to commit transactions
[Authorize("OrganizationCreationPermission")][ correct ]
public async Task<Organization> CreateAsync(CreateOrganizationDto input)
{
// ⽀ Pay the organization fee ⽤[ correct ]
await _paymentService.ChargeAsync(CurrentUser.Id, GetOrganizationPrice());
// Through domain service , Create a new organization instance
var organization = await _organizationManager.CreateAsync(input.Name);
// Save and update the organization into the database [ correct ]
await _organizationManager.InsertAsync(organization);
// Send a reminder email [ correct ]
await _emailSender.SendAsync("[email protected]", " New organization ", " New organization name :"+name);
// Return instance [ error ]
return organization;
}
private double GetOrganizationPrice()
{
return 42;//Gets form somewhere...
}
}The input and output parameters of the application service layer are DTO, Cannot return entity . As for why not put payment in domain services , It can only be said that business is not necessarily important in domain services , For detailed reasons, please refer to [1].
reference :
[1] be based on ABP Framework Implement domain-driven design : https://url39.ctfile.com/f/2501739-616007877-f3e258?p=2096 ( Access password : 2096)
边栏推荐
- 调整数组顺序使奇数位于偶数前面且相对位置不变
- 【YOLOv5】--详细版训练自己的数据集 保姆级学习日志记录 手把手教程
- The inventory of chips in the United States is high, and the shipment of chips in China has increased rapidly and the import of 28.3 billion chips has been greatly reduced. TSMC has a showdown
- 2022/07/26 学习笔记 (day16) 链表和栈
- The difference between advanced anti DDoS server and advanced anti DDoS IP
- Configure the server environment
- "Weilai Cup" 2022 Niuke summer multi school training camp 2
- Sre person in charge of station B personally describes the multi activity disaster recovery construction after the 713 accident | takintalks share
- File depth monitoring strategy
- Turn off win10 automatic update completely
猜你喜欢

千万不要随便把 Request 传递到异步线程里面 , 有坑 你拿捏不住,得用 startAsync 方法才行

基于华为云 IOT 设计智能称重系统 (STM32)【一】

Image preview embedding location of blog maintenance record

Software process that testers must know

Redis introduction

什么是联邦图机器学习?弗吉尼亚大学最新《联邦图机器学习:概念、技术和应用》综述

J3: redis master-slave replication

Redis6

Leetcode-138-copy linked list with random pointer
![[C language implementation] - dynamic / file / static address book](/img/5a/655d9a4799b3e874a454a183dee3f1.png)
[C language implementation] - dynamic / file / static address book
随机推荐
Image preview embedding location of blog maintenance record
EN 1504-6 products for protection and repair of concrete structures - reinforcement anchorage - CE certification
The inventory of chips in the United States is high, and the shipment of chips in China has increased rapidly and the import of 28.3 billion chips has been greatly reduced. TSMC has a showdown
[MySQL must know and know] log details
什么是服务器集群?海外服务器集群的优势?
If the key is forgotten and multiple devices have different keys, how does the cloud synchronize
这22个绘图(可视化)方法很重要,值得收藏!
Description of MDM separation of powers and classification and grading authority
基于华为云 IOT 设计智能称重系统 (STM32)【二】结尾有资料
C语言-入门-语法-字符串(十一)
Four methods of closing forms in C #
手机app测试用例怎么写?手机app测试点有哪些?
Configure the server environment
"Weilai Cup" 2022 Niuke summer multi school training camp 2
NLP learning path
DOM案例:10秒倒计时-写跳转页面相关的知识
DDL,DQL,DML语句
基于ABP实现DDD--领域逻辑和应用逻辑
How to solve the problem that win11 has been switched on after upgrading
节约gas-ChiToken的用法