当前位置:网站首页>DDD concept is complex and difficult to understand. How to design code implementation model in practice?

DDD concept is complex and difficult to understand. How to design code implementation model in practice?

2022-06-25 17:18:00 InfoQ


Write it at the front :

Today, I'm going to talk to you ,DDD Complicated concept , How to design code implementation model in practice . Maybe you just read about this part , It is necessary for me to say more , About this topic , On the frame , It is divided into two parts : Methods  +  Practice chapter .
The former part , Methods . To introduce in detail DDD Contains several core concepts , And around these concepts DDD Code implementation model composition structure . As for why it is necessary to say , I made it clear at the beginning of the last article that .
in addition , Considering that some friends may just click in , I haven't read the last one yet , Or did not have time to see , So here is another point   I would like to share the necessity of this topic , So as to help you quickly know what you can gain more or less .
To the point , It is a pity , At present, the industry is concerned about how to implement these concepts , There is no uniform set of standards and specifications , This leads us to the specific development process , Often feel unable to start .
So , This article has specially refined a complete set of DDD Code implementation model .
Besides , About the way you look , Let me say one more thing . It is updated and released in two parts , This involves succession . If you haven't read the last one , friend , I suggest you spend a few minutes first , Or combine the ideas of big and small titles in the article to guide , Have a general understanding of the following text framework . We can finish the last article , Then enter the sharing of this article , Combined with , better .

​01  How to design DDD Code implementation model ?

Based on the analysis of DDD When the code implements the model , For the four components mentioned in the previous article , We need to sort out their code structures and dependencies . For code structure , We need to define the composition of the code package , As well as the technical components included internally .
After defining the package structure , Dependency means that we need to further clarify the interaction between these code packages and technical components . Based on these two points , Let's first discuss the code implementation model of domain objects .

︎  Domain object code implementation model

For domain objects , We usually use “domain” The word , Name the top-level package of the code package structure , All technical components under the package structure , Both belong to the category of domain objects .
To be specific , stay DDD in , Domain objects include domain model objects 、 Field events 、 The commands and query objects involved in the resource library and application services , Domain model objects can be divided into aggregations 、 Entity and value objects .
therefore , stay DDD All code implementation models , Domain objects involve the most complex code structures , It can be divided into two levels , Pictured 1 Shown .
null
edit
Add picture comments , No more than  140  word ( Optional )
chart 1
You can see , there “domain” Represents the entire domain object , and “model” Represents the domain model object , Note the difference in naming between the two , And their affiliation . Domain objects are DDD The foundation of the code implementation model , Contains the implementation of the core business logic .

︎  Application service code implementation model

Similarly , For application services , We usually use “application” To name the top-level package structure . Application services include query services and command services , So in the name of the sub package , Can also use “commandservice” and “queryservice” Distinguish , Pictured 2 Shown .
null
edit
Add picture comments , No more than  140  word ( Optional )
chart 2
obviously , Command service and query service , It depends on the domain object code to implement the command object and query object in the model , We use dashed lines to represent this layer of dependency . stay DDD Code implementation model , Application service is a code model with the most complex interaction relationship .
One side , It needs to combine command and query operations , Assign to domain model objects such as aggregate objects .
On the other hand , It also requires separation and infrastructure , And other gauge contexts .
About the latter , When we discuss the case study , There will be further development .

︎  Infrastructure code implementation model

Actually , So called infrastructure , refer to DDD Specific technologies used in the application 、 Tools and frameworks . Common infrastructure components mainly include these aspects :
  • Data persistence (Persistence)
  • Message communication (Messaging)
  • The system configuration (Config)
  • safety control (Security)
therefore , The package structure of the infrastructure is not fixed , But flexible organization according to specific technical development requirements , Here is a common package structure , Pictured 3 Shown . For infrastructure , We used “infrastructure”, Name the package structure .
null
edit
Add picture comments , No more than  140  word ( Optional )
chart 3
There is one thing to note in the above figure , On behalf of data persistence “persistence” package , Communicating with a representative message “messaging” package , It is the most common in the infrastructure code implementation model , Because they correspond to the resource library and domain events in the domain object respectively .
stay DDD in , The definitions of the repository and domain events are located in the domain object code implementation model , They have nothing to do with the specific implementation technology . Persistence and message communication related to specific implementation technologies , In the infrastructure code implementation model . This reflects the design principle that domain objects and implementation technologies are separated from each other .

︎  Context integration code implementation model

Last , Let's discuss the context integration code implementation model . It should be noted that , This model is the most difficult to implement , Because it involves a variety of system integration technology systems .
Implement the model for this code , We first need to make it clear that it is oriented to multiple bounded contexts , So we need to consider the flow of data , That is the so-called introversion (Inbound) Data and extroversion (Outbound) data .
One side , Bound context , Access portals need to be exposed for use by other contexts . From the perspective of the current context , This is a Inbound operation . When a context makes a request to an external context , So that's one Outbound operation , Pictured 4 Shown .
null
edit
Add picture comments , No more than  140  word ( Optional )
chart 4
In the design of code implementation model , We will also use “inbound” and “outbound” To name the package structure . So under these two package structures , What technical components should be included ?
Let's talk about “outbound” Package structure , Pictured 5 Shown .  In the figure ,“rest” In bag REST API Make an external request , Into internal Command and Query object , And hand it over to the application service for processing . In this transformation process , It is often necessary to introduce specialized DTO(Data Transfer Object, Data transmission object ) object , And assembler (Assembler) object .
null
edit
Add picture comments , No more than  140  word ( Optional )
chart 5
meanwhile ,“eventpublisher” Event publishers in packages (Event Publisher), Is used to publish domain events facing the external bound context .
next , We discuss “inbound” Package structure . In a gauge context , Data Inbound There are two main types of operations , One is anticorrosive coating (Anti-Corruption Layer,ACL), Used to send messages to remote REST API Initiate the request and get the result . The other is the event handler used to respond to domain events (Event Handler), Pictured 6 Shown .
null
edit
Add picture comments , No more than  140  word ( Optional )
chart 6
Context based integration process , In both contexts “inbound” and “outbound” The technical components contained in the package structure , In fact, it is one-to-one correspondence , Pictured 7 Shown .
You can see , A bounded context “inbound” Medium “acl” and “eventhandler”, Each corresponds to another bounding context “outbound” Medium “rest” and “eventpublisher”.
null
edit
Add picture comments , No more than  140  word ( Optional )
chart 7
thus , About DDD Four types of code implementation models in , Have introduced . In the following content , We will be based on a specific application scenario , Through case study , Put these code implementation models into practice . Based on this case , You can take all of the content introduced earlier in this article , And the daily development process , Further master the implementation methods and skills of converting the model into specific code .

02 DDD Code implementation model case analysis

In the real world , Work order processing is a very common business requirement . The initiation of work orders , It is usually because the user needs to consult or complain about the order .
In this scenario , be based on DDD The design method of , We can split the work orders separately (Ticket)、 Customer service (Staff), And the order (Order) These three bounds context . In these three contexts ,Ticket Context , Will be separated from Staff and Order These two contexts are integrated , To create a work order application , Pictured 8 Shown .
Please note that , The picture shows Ticket Context , Two different ways of context integration .
in the light of Staff Context ,Ticket The context will use REST API, Complete the acquisition of customer service data in the work order .
And for the Order Context , Domain events are used , That is, once Order The state of ,Order The context will send the corresponding domain event to Ticket In the context of .
null
edit
Add picture comments , No more than  140  word ( Optional )
chart 8

︎ Ticket Example of context code implementation model

obviously , For this scenario ,Ticket The context also has Inbound and Outbound operation . therefore , Its code implementation model is the most complete , Pictured 9 Shown .
null
edit
Add picture comments , No more than  140  word ( Optional )
chart 9
Above picture , We use IDEA This development tool and Spring Boot This particular development framework , To build the Ticket Code implementation model of bounded context . We can see clearly ,DDD Four code implementation models , There are five top-level code package structures . among , The context integration code implementation model also includes “inbound” and “outbound” These two code packages .
We will expand these top-level code package structures , You can get the picture 10 The sub code package structure shown in .
null
edit
Add picture comments , No more than  140  word ( Optional )
chart 10( Slide up and down to see )
All the sub code package structures shown in the figure above , The corresponding descriptions have been given in the previous contents , I won't repeat it here .
Ticket In the context of , Command service TicketCommandService Completed the Staff Context integration of services , At this time, the anti-corrosion coating is used ACL Components , The sample code is as follows .
You can see , Use here AclStaffService This ACL Components , Yes Staff The service initiated a remote call , Then fill the returned results into the command object , And create Ticket polymerization . Final , We go through TicketRepository Completed the persistence operation of the aggregate object .
null
edit
Add picture comments , No more than  140  word ( Optional )
chart 11
Above AclStaffService, It's done right Staff The context provides REST API Call to , The sample code is as follows . It's used here Spring Self contained RestTemplate Template tool class , Complete remote HTTP Endpoint access operations .
null
edit
Add picture comments , No more than  140  word ( Optional )
chart 12

︎ Staff Example of context code implementation model

stay Staff Context , We need to complete the above REST API The construction of , Its code engineering structure is shown in the following figure .
You can see , Comparison Ticket Context ,Staff The code structure of the context is relatively simple , Because the context only needs to provide external “outbound” package , The infrastructure only needs to complete the persistence of domain objects .
null
edit
Add picture comments , No more than  140  word ( Optional )
chart 13

︎ Order Example of context code implementation model

Last , We are here Order Bound context , Its code implementation model is like this , Let's have a look .
null
edit
Add picture comments , No more than  140  word ( Optional )
chart 14
We know Order Context , For Order Domain event publishing mechanism of data , So it's “outbound” The package contains... For publishing domain events “eventpublisher” subpackage , And provides a OrderEventPublisherService, As shown below .
null
edit
Add picture comments , No more than  140  word ( Optional )
chart 15
Through here Spring Cloud Stream, Realize the release of domain events . And in the Ticket In the context of , We can also be based on Spring Cloud Stream, Realize the monitoring and consumption of events in this field , The sample code is as follows .
null
edit
Add picture comments , No more than  140  word ( Optional )
chart 16
Please note that , Above OrderUpdatedEventHandler, be located Ticket Context “inbound” Bag ”eventhandler” In subpackage .
The explanation of these specific implementation codes is not the focus of this article , You can refer to the author in Github The case code on :https://github.com/tianminzheng/customer-service.

03  Summarize and extend thinking

That's the end of today's sharing . This article answers the questions of developers in detail , In the realization of DDD A core problem encountered in applications , That is, how to build DDD Code implementation model . The reason to discuss this topic , The reason lies in DDD Many of the concepts in are rather obscure , And the industry has no idea how to implement these concepts , Provide unified development specifications and standards .
And through the DDD Various complex concepts in are mapped with specific code implementation models , While helping us better understand these concepts , They can also be directly applied to the daily development process .
Through the introduction of this article , Developers can combine their own business development needs , Design a complete set of DDD Code implementation model . Here is also attached the full-text mind map , Help you review 、 Sort out ideas, etc .
null
edit
Add picture comments , No more than  140  word ( Optional )
chart 17  Full text thinking framework map - Help you quickly review 、 carding 、 summary

︎  Last , I think it is necessary to emphasize one point

The DDD Code implementation model , It is just a reference model . The design of code implementation model , It is also related to the technical system adopted . In the case shown in this article , We used Spring Boot、Spring Cloud Stream etc. Spring The development framework in the family , To develop DDD Applications .
And if you use Axon This is based on the event traceability model DDD Development framework , So in the code implementation model , You need to introduce a for event distribution and storage Gateway、EventStore And so on , The traditional data persistence components located in the infrastructure , May not necessarily be used .
Of course , Based on what we introduced today , It's not hard for you to do this DDD Code implementation model to extend .DDD As a system modeling methodology , There are also things like layered architecture 、 Clean architecture 、 Hexagonal architecture and other architectural styles .
For each architectural style , We all need to design the corresponding code implementation model .
Based on the content introduced in this article , Through to DDD The core concepts and implementation models in the , In this paper, I provide a systematic method to design code implementation model , To help you cope with the implementation requirements of different architectural styles .
This is also the core value of this article .​
原网站

版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206251545459157.html