Hierarchy principle to avoid circular dependency ].
Not only do we have to pay attention to principles when doing things in the technical field, but those who violate the principles will be heartless punishments.
For the design of DDD, we also need to observe the corresponding principles. Of course, if we do not observe it in the early stage, there will be no difference, but the problems will be exposed in the development stage.
Let's look at two basic design principles.
Simplified Aggregation]
The design principle of streamlining aggregation is undoubtedly the most important. Some Software Engineering Methodology books often guide us in UML business modeling,"No technical implementation issues need to be considered at this stage"According to this guiding principle, I designed the UML and successfully created the ER relationship diagram. As a result, I found that such a database structure cannot be used as the final project development database. What's wrong? I repeatedly searched the guidance books and then found a sentence like this on the professional DDD book:
I think this sentence really describes the gap between methodology and enterprise applications. Many technical ideas or theories are really good, but to use them, we need to solve many problems. DDD cannot avoid this problem. How to avoid excessive design when designing a UML model? Here, we only need to abide by the principle of streamlined aggregation.
In the previous example, we designed a complete UML domain model, but we didn't streamline and aggregate it.] The problem is that it cannot be developed in projects.
1.1
650) this. width = 650; "style =" border-right-0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px "title =" 8 "border =" 0 "alt =" 8 "src =" http://www.bkjia.com/uploads/allimg/131228/1625212C5-0.jpg "width =" 746 "height =" 386 "/>
The initial version of the domain model we have built should be like this. There is a strong relationship between the entity and the entity. The association between the entity and the aggregation is too large, leading to the overall pull. If you follow this link to create a database, there must be a lot of primary/Foreign keys between databases, affecting the database design. If such a relationship is used in a program, there will also be many problems, and we cannot use a few aggregates, when we use a certain aggregation, it will drag out the associated aggregation one after another, not only does the query impede the creation of aggregation in the Factory, but also cannot be constructed during the creation of aggregation. No matter what operations are performed on the aggregation Repository, the program logic will be affected, therefore, we need to split a complicated and huge relationship.
Disconnect all parts of the red line and associate the aggregation with IDs. This will become clearer. Because few programs require the participation of all business models on a certain business logic point, this facilitates program development and database design, making it easier for ORM to use. The delay loading of ORM is actually to aggregate dependencies. You can query the desired model as needed. However, even though the program can be said in the past, the design of the database cannot be said. For different ORM frameworks, the ing principle is different and requires slight adjustment when constructing the model. For example, in EntityFramework, the ing scheme supported by the ing scheme ensures that your model can be smoothly mapped. Here, we will not talk about a detailed project for comprehensive practice. Then we will analyze the specific problems. [Wang qingpei has all rights reserved. For more information, please sign it.]
Finally, let's take a look at the decomposed class diagram:
1.2
650) this. width = 650; "style =" border-right-0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px "title =" 9 "border =" 0 "alt =" 9 "src =" http://www.bkjia.com/uploads/allimg/131228/1625215060-1.jpg "width =" 829 "height =" 371 "/>
In this way, the relevant core domain models can be used directly, without worrying about the problem of slow loading of the orm framework.
Separation of Use Cases and functional interface design patterns )]
The separation of Use Cases and functional interfaces is actually an occupational disease that some people who are new to DDD are familiar with, because we are all familiar with object-oriented design. During UML modeling, we like abstraction very much and clearly represent generalized relationships with inheritance, such as user types. Different users have different behavior permissions, during the preliminary design, we generally establish an inheritance relationship about users to express generalized business models. However, in the encoding stage, we will find that the obvious problem is that we include the Repository behavior in the user aggregation of the launch case, which may be a bit abstract. We still use examples for analysis;
1.3
650) this. width = 650; "style =" border-right-0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px "title =" 10 "border =" 0 "alt =" 10 "src =" http://www.bkjia.com/uploads/allimg/131228/1625215333-2.jpg "width =" 803 "height =" 294 "/>
I separated the Admin and distribution use cases. If you want to express it, you cannot place the distribution behavior in Admin. When we model relevant permissions, we often subconsciously place their respective behaviors in their respective roles, if multiple roles share behavior in the later stage, it will be written in the abstract class and passed down using the virtual method. The problem lies in the role behavior. We know that if there is a behavior, it is possible to execute the IRepository operation related to other aggregation operations in this behavior. In this way, the domain model will be messy, vertical analysis fails.
1.4
650) this. width = 650; "style =" border-right-0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px "title =" 11 "border =" 0 "alt =" 11 "src =" http://www.bkjia.com/uploads/allimg/131228/16252121A-3.jpg "width =" 617 "height =" 316 "/>
DDD focuses on the field drive. In our opinion, Dispatching and CheckOrders all inherit the administrator role, and the Administrator belongs to the background administrator, which means employees of the enterprise. Consumers are managers. Similarly, consumers may have the same situation. There may be many types of consumers, including VIP series (VIP1 \ VIP2 \ VIP3 ...), There are diamond members. If this design is done, it cannot be said to be wrong. This is completely in line with the ideological requirements of DDD, but it is not ideal in actual situations.
Here we use our long-term design model. We can separate users from behavior through many medium models in the design model, then, the rules and conditions used are abstracted to completely independent users. When users use the rules, there is no direct attribution of behavior, but in fact they do have behavior.
1.5
650) this. width = 650; "style =" border-right-0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px "title =" 12 "border =" 0 "alt =" 12 "src =" http://www.bkjia.com/uploads/allimg/131228/1625212142-4.jpg "width =" 503 "height =" 441 "/>
In professional DDD terminology"Protocol mode"Service Rules can be extracted for objectization, and even configured at the end. What excites us most is that our painstaking design model can finally be used in a large area in system design. Isn't it a big surprise!
1.4 tools and frameworks
Any architecture needs the support of frameworks and tools to become perfect.
When we develop in a certain architecture, we must support a lot of tools and frameworks to make the development work more convenient.Agile Development]The same idea. In the traditional three-tier architecture, we all need"Object ing", "AOP \ IOC"And so on. In DDD, we also need a lot of tools and frameworks that have not yet appeared. currently, only the EntityFramework framework on the NET platform has done a lot of work for DDD. If our domain model cannot be mapped to the database, the cost of domain model development will be very high.
In the design phase, we lack a modeling tool for specific fields. Unlike UML, UML is too technical and universal. It is often mentioned in DDD.Field ExpertHe is the most authoritative leader in the field. The UML we created may not be easy to understand. After the technicians are technical, the formation of UML has actually changed,Field Expert] Is an uncertain guarantee. If we can language domain models, this will be a major achievement. Field Expert] He is familiar with everything, characters, and links in the field, but he cannot express his ideas clearly. If he can have a tool to assist his design, this tool can convert the designed model into a code model or a database model smoothly and equivalent. If this gap can be crossed, it will have a great influence on the industry.
1.8
650) this. width = 650; "style =" border-right-0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px "title =" 13 "border =" 0 "alt =" 13 "src =" http://www.bkjia.com/uploads/allimg/131228/1625215060-5.jpg "width =" 660 "height =" 388 "/>
If we can equivalent the technology of the real model, then everyone will really like requirement analysis, analysis and design.
Since it is a model-driven design, when we analyze such a system for users, the premise is that we have abstracted and encapsulated all the details in it, and each process can be split, finally, it can be combined to form an overall business model. Of course, this is only a technical outlook, and it is also our goal and goal.
We recommend the latest Martin Fowler book: domain-specific language