Strategic design provides us with a high-level view of our software systems , It mainly includes the fields / Subdomain 、 General language 、 Concepts such as bounded context and architectural style ,
The tactical design makes the strategic design concrete and detailed , It is mainly concerned with the implementation at the technical level , It's also the most practical place for programmers .
The purpose of tactical design is to ensure the realization of strategy . stay DDD in , Code is the design itself , You don't need the red tape and you'll never be able to get real-time updated design documents .
Watch out for anemia , It's not hard to create domain objects that are full of behavior , We need to change our thinking , Think of domain objects as providers of services , Instead of data containers , Think more about what behaviors a domain object can provide , Not data .
A solid model is an independent thing , Using the hyperemia model , With business attributes and business behaviors . Each entity has a unique identifier , Its personalization can be distinguished from all other types of the same or different entities . A lot of time , Entities are mutable , Its state changes over time .
Make multiple modifications to an entity , The modified data may not be the same as the original , But they are still the same entity , Because the only sign hasn't changed .
The value object
A value object , It's a model of an unchanging conceptual whole , There is no unique identifier . In this model , There is only one true value . It's not like an entity , It has no unique identifier , Instead, the equivalence is determined by the property comparison encapsulated by the value type . Besides , A value object is not a physical object , It's often used to describe 、 Quantify or measure an entity .
A typical value object is an address value object , For example, when placing an order , There is a single person 、 Commodity information 、 Offers 、 And shipping address information , If the address information is saved 、 City 、 Attributes such as area represent , There will be some bits and pieces , Take it out to form a “ Address ” attribute , It would be more appropriate , This set of multiple values is the value object .
stay DDD in , Entity and value objects are very basic domain objects , Both entity and value objects are individual objects , They are the ability of individuals to behave . Aggregation is used to ensure that these domain objects implement common business logic , Can guarantee the data consistency .
Aggregation is a combination of entities and value objects closely related to business and logic . Aggregation is the basic unit of data modification and persistence .
Four basic rules of aggregation design
- Protect business rule invariance within aggregation boundaries
- Aggregation should be designed to be small
- Other aggregations can only be referenced by identifier
- Update other aggregations with final consistency
Each aggregation has a root entity , It's called aggregate roots , The outside world can only communicate with the aggregation through the aggregation root . The main purpose of aggregation root is to avoid the lack of uniform business rule control due to complex data model , And it leads to aggregation 、 Data inconsistency between entities .
If you compare aggregation to a team , So the aggregation root is the team's leader, The needs of other teams need to be consistent with that of the team leader Only after consultation can we start construction .
When an operation is not suitable for placing on aggregate and value objects , The best way is to use domain services .
Where domain services can be used , Overuse of domain services will lead to anemia domain models .
- Perform a significant business operation process
- Transform domain objects
- Multiple domain objects have been calculated as input , The result is a value object
Domain services don't need to define interfaces , Define the implementation directly
- If the interface has different implementations , Then we need to consider whether there are specific functional behaviors in the domain
- If we use dependency injection or factory , Even if the interface and implementation classes are merged together , We can still achieve this goal . Relying on inverted containers （ for example Spring） The injection of the service instance will be completed , Because the client is not responsible for the instantiation of the service , It doesn't know whether the interface and the implementation class are separate or combined .
The difference between domain services and entity methods
- The entity method completes the business logic of a single entity itself , Relatively simple atomic business logic
- Domain service is the business logic of relative services composed of multiple entities
Repositories are used to hold and retrieve aggregate objects , Think of the repository as a collection of objects , Not the database CRUD, It's not a table, a warehouse .
It needs to be imperceptible to users , It's just like using a collection in memory .
Warehousing came into being to focus on domain models , Don't focus on structure tables .
Domain events are a record , Records events that have a significant impact on the business in the context of the bounds , Often used to ensure consistency between two aggregations .
For naming domain events , It must embody the general language of the model , These nouns are bridges between models , It's important to communicate fully what's going on .
The domain event type name should be a statement of what happened in the past , The past tense of a verb , Such as OrderCreated.
- 《 Domain-driven design —— How to deal with the complexity of software core 》
- 《 Implement domain-driven design 》
- 《 Domain driven design essence 》