About the Domain Driven Design (DDD) in the aggregation design considerations

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

On the theoretical knowledge of DDD summary, can refer to this article.

A brief discussion on DDD community website an article about the aggregation design several principles.:

This paper address:, the address is included in an article on some design principles of how effective the polymerization, a total of 3 PDF files. The article points out several aggregation design principles:

  1. Aggregation is used to package the true invariance, rather than simply the objects together,
  2. Polymerization should try to design a small,
  3. Correlation between polymerization by ID, rather than the object reference,
  4. The strong consistency of polymerization, polymerization between the final consistency,

These principles above, the author expatiate gradually through an example. Below I according to my understanding of each principle to do a simple description of the.

Aggregation is used to package the true invariance, rather than simply the objects together

This principle is to emphasize the real use, except our own polymerization package information that is of concern, the main purpose is to encapsulate the business rules, guarantee the data consistency. In my opinion, this is the design of polymerization is the most important and most need to consider the point; when we were designing the polymerization, we should think more of the polymeric encapsulation of what the business rules, implement what data consistency. The so-called business refers to the rules, such as a bank account balance of not less than 0, the number of the order details in the order cannot be 0, orders can not be two detailed corresponding commodity ID is same, the order details in commodity information must be lawful, commodity name cannot be empty, you create must reply to the incoming reply posts (because there is no post reply is not a legitimate response), etc.,

Polymerization should try to design a small

This principle, more is to consider from a technical point of view. The author illustrated through an example, in this case, a great start aggregation design, contains a lot of entity, but later found because the polymerization contains too much, resulting in more people operation concurrency conflict seriously, cause the system availability variation; later development team will split the original into a plurality of polymerization small polymerization, of course, split into small after polymerization, the polymerization is maintained within the same business rules in multiple small polymerization reflected. So the implementation can solve the problem of concurrent conflict, can ensure that the polymerization to encapsulate the business rules, data consistency model level; in addition, a friend of &ldquo recovery in war, Ling Feng; ” mentioned, the aggregation design small there is a benefit, is a business decision: polymerization, business changing polymerization. The aggregation design reduces the possibility of small except for concurrency conflict, also reduced the business change, the number of split a polymerization, polymerization greatly reduces the possibility of reconstruction (resolution), which can make the domain model we can adapt to the business.

The association between ID polymerization

This principle, is taken into account, there is no need to be through object references to related polymerization,

The strong consistency of polymerization, polymerization between the final consistency

Background the principle is: if CQRS+Event Sourcing architecture to implement DDD, the polymerization by Domain Event (for field events) to achieve the interaction, so also does not need the object references between polymerization and polymerization, and there is no need to field service, because the service has been Process (process polymerization root) and Process Manager (no state process manager, replaced). Process polymerization root, responsible for packaging the current state of the process and process how to go the next step logic, including process encountered rollback processing logic is abnormal; process manager, no state. Responsible for the coordination of all participants in the polymerization process message interaction between root, it will accept the polymerization root generated domain event, and then sends a command. On the other hand, Due to the introduction of the CQRS, So we only need to handle the business logic of domain, Without the need to cope with the query dependent demand, All kinds of query demand exclusively by various query service implementation; so we domain can be very thin, Only by polymerization of root to encapsulate the necessary business rules (guarantee strong consistency in data aggregation)., Then each aggregate root do any state changes, Will have the corresponding field events, Then the event will be persistent to EventStore, EventStore used to persist all events, The domain state to restore, Needs only by Event Sourcing reduction; in addition, When the event persistence is completed, The framework will be the event by event bus out, Then Process Manager can respond to events, Then send the new command to inform the polymerization root corresponding to do the necessary processing,

Architecture diagram above this process can be in any CQRS (chart eNode) to find, here I will not map. Transfer scene on the classic eNode with this idea to realize it, are interested can go to download the eNode source code, and then look at the BankTransferSample this example clearly. In addition, because the send event response and Command is asynchronous, so, this structure, polymerization of root interaction is asynchronous,

Need a little it is, If you only need to pay attention to how to realize the polymerization of business rules and do not need to consider the benefits of query demand, That is we do not need to maintain a variety of statistical information in the domain., State information can be as long as the maintenance of various business rules potential must rely on; for example, If a forum, There are pages and posts, Before, We may have the attributes of a post in the Forum on the total number of object, When added to a post, On the property plus 1; while in the CQRS framework, Within the domain section aggregate root without maintenance total number of Posts this statistical information., The total number of posts in the inquiry terminal database independent maintenance,

Thinking from the polymerization and philosophical point of view, why the need for state?

Polymerization of angle

First of all, what is the status? Very simple, such as a commodity inventory information, then the inventory information has a quantity of this attribute, indicates the current item is in stock and how many pieces; then why do we need to record the property? Why need to record this state? Because of the existence of business rules. In this example, because of the presence of “ goods inventory can not be negative ” a business rule such that the rules, if can guarantee, the number of inventory must first record commodity; because the quantity of goods inventory is sold as commodities and reduced, and reduced by: Product.Count = Product.Count - 1 such logic operation to achieve; the logic operation to operation of the premise is to have the goods inventory information. From this example we are not difficult to understand, a polymerization root many state, not the best design up, but some potential business rules requirements, the state must be designed to achieve the corresponding business rules; there are many such examples, such as a balance not bank account number is less than 0, leading to attribute our bank account we must design a current balance,

Another reason is, looks like crap, ha ha. That is: because we care about these information, so we need to design the polymerization; for example, to a post in a forum for example, as a post, we are usually concerned about post title, description, post, post time, the section (if the forum version block this concept words); so, we will be in the root of these attributes aggregation design post, to express our concern the information state,

A philosophical point of view

The following expression in the concepts of object from the philosophy angle, partial:

Man can never fully understanding of things, because the one we recognize always things. What we say is actually the objective things in the minds of the reaction, which is objective existence do not know because of the changing. Also an iron bar, in the opinion of the steel manufacturer, it is finished; in the eyes of machinery processing factory, which is the raw material; in the waste station looks, he is a commodity. Finished products, raw materials, goods, the three have different properties, is of a different nature. Why did the same thing in different people's eyes is different? This is because we always take useful for us to understand things. When the iron bar as commodity, material attribute of it still exists, but we don't care.

So, summed up, because some ways we care about an object, so we will for his design of some state attribute,

Some thoughts on the design of polymerization

It is simply mentioned, aggregation design should consider it encapsulates what business rules this problem. Here I want to say a little bit more of my thoughts:

The most important patterns of GRASP in nine mode: Information Expert

Or in the forum as an example, Create a post, There is a business rule, That is the post post, title, description, the plate (if the forum has the concept of plate it) cannot be null or an invalid value, Because this information as long as there is any invalid, That means be created post is invalid, There is no guarantee the business rules, The consistency of the data and there is no way to talk about the domain model; if three layer architecture in anemia, That post only a data carrier, Does not contain any business rules, The post will be constructing an empty object out posts, Then we give some attribute assignment this empty thread object, And then save the post objects to the database; this design, Post object is a data container, It can not control their own state, Because of its status is being (such as service) to modify the; such design, Quite so no business rules are encapsulated in the business object inside, But transferred to the external service, Although this is no problem, In fact most of us have been doing, Because such code is free to do, Also very efficient, Ha-ha.

GRASP nine model in an object-oriented model called information expert , do not know if you have seen no, the mode of description is: will assign responsibilities to perform the duties have required information for object ; this pattern tells us, if an object is responsible for the maintenance of some information, it we have a duty to maintain these information. The properties of the object, that is the object attribute cannot be external change casually, attribute their own maintenance and modification. The constructor and the general method will change the state of the object, so the public methods, constructors and ordinary object, must uphold this principle; this is very important, otherwise, if like anemia model, the object is not called the object, but just an ordinary hold data container, and a record in the database has no essential difference. In fact, in my opinion, this is the difference between the largest local polymerization in DDD to the anemia model entity. Polymerization not only state, there are strict maintenance of various methods of their state, including constructors; and anemia model, only the state, no action,

On the DDD in a domain object is the root account aggregation

This problem is not very clear, the universal method of determining, my idea is:

  1. First, from us to the areas of the most basic common sense understanding of the thinking, whether the object is independent of the life cycle, if there is, it basically is the root of the polymerization,
  2. If an object within the domain, we will in the background there is an independent module to manage it, it basically is the root of the polymerization,
  3. Whether there is an independent business scenarios to create or modify an object,
  4. If the object has a global unique identification, it is also the root of the polymerization,
  5. If you are not sure whether an object is the root of polymerization, and put it, would assume that it is aggregate root also just as well, then can look you have identified the root should be specific about what information polymerization polymerization; maybe you clear analysis of those polymerization of a range of other, also derive you didn't know whether the object should be the polymerization polymerization root root.

About a polymerization should be what information on polymerization

  1. We need to go in the attribute design concern,
  2. Analysis of the polymerization to package and to achieve what the business rules, which like the example above (inventory) that are derived to design which attributes to the polymerization state,
  3. If we modify an object or, will always be concatenated to create or modify some cascade information, such as a task system, when we create a task, may upload some accessories, the attachment of the description information (such as the annex ID, annex, annex download address) should be aggregated in the task of polymerization root,
  4. Polymerization only need to value objects and internal entity may, without reference to aggregate root other, other aggregate root will only make the polymerization of boundary fuzzy reference,

Thinking about how to design more reasonable polymerization to encapsulate all business rules

This point in the above several principles, in fact already mentioned a little, that is to design a small polymerization, the starting point here is to think from a technical point of view, in order to reduce the common object (high polymerization) concurrent modifications, thereby reducing the concurrency conflict can, thus increasing availability the system (because the system users do not frequently because of concurrent conflict and lead to its operation failed); on this point, I also want to give a few examples to explain, in fact, to achieve a variety of business rules, can design a variety of polymerization, polymerization is just one big,

For example, Post and reply, We all know that a post a reply, There is no post, Reply has no meaning; so many people will think that the post should be aggregated reply; but in fact does not need such, If you do, It is a forum for, The same post is more likely to people at the same time to reply is very high, That such words, Many people at the same time to reply to a post, Will lead to more people at the same time to modify the same post object, It would cause everybody could not reply, Because there will be a concurrency violation or database transaction waits for the timeout, Because everyone in the same post polymerization modification of root; in fact if we from business rules perspective to think about, It can be found, In fact, posts and reply., There is only one simple rule, That is the answer once created, He corresponding posts cannot be modified in this way can;, To achieve this rule is actually very simple, The reply as polymerization root, Then the incoming reply post polymerization root constructor, Then back to save the post ID, Then reply will post the ID settings to not allow external (private set; you can), So we can achieve the business rules, At the same time also did many people while pushing a post reply, Not to the same post object concurrent modification, But every reply is parallel to the database inserts a record to reply,

Therefore, through this example, we found that, in order to realize the domain model in various kinds of business rules, there is more than one way, we should not only consider the cohesion relations from a business perspective, but also from the technical angle, but no matter from what point are considered, in order to achieve the required business rules as the premise,

From this example, We also found a good thing, It is a forum, The posting and reply are two independent business scenarios; one of the, And then after a period of time, Another published reply to this post; I will post and reply are designed to be independent is very easy to understand; although here post and reply is a one to many, Reply to leave the post really has no meaning, But will reply design in post no good, But the availability of the system decreases; the opposite, Like the above mentioned about creating tasks at the same time upload attachment example, While a task is corresponding to a plurality of accessory information, But we found that, Figure attachment information always with a task can be created or modified, Is modified. That is to say, an attachment to information we have no independent business scenarios to modify tasks; therefore, there is no need to task attachments designed as independent polymerization root,

The ENode framework supports on the polymerization and polymerization of interaction design

ENode provides a DDD+CQRS+Event Sourcing+In Memory+EDA based application development framework of these techniques,

  1. ENode at the framework level limits of a command can only be modified by an aggregate root, which put an end to our Unit of Work model to the transaction way to modify the one-time multiple polymerization root,
  2. ENode provides a mechanism for atomic operation and concurrent conflict detection reliable, to ensure the strong consistency of individual operation,
  3. ENode provides a reliable mechanism to ensure the event, the event asynchronous communication way to achieve eventual consistency of polymerization between interaction can aggregate data between our domain; if some complex business scene is a process, then we can realize the process state tracking and flow through the Process+ Process Manager thought transference,
  4. Because event eNode based on domain, so, we aggregate root need not refer, each aggregation root only need to be responsible for their status updates, and then after update generate the corresponding domain event, this essence is implemented: Don’ t  Ask,   Tell this design principle,
  5. ENode provides reliable event delivery mechanism, can ensure that command side and query side data is a final agreement,
  6. ENode provides the in memory design, so that our domain can be very efficient operation, persistent event does not require transaction, obtaining the aggregation root access directly from the in memory,
  7. ENode provides a lot of design, so that we can maximize the polymerization to the root different instances of parallel operation, so as to improve the throughput of the whole system,

The use of eNode, will force you to think about how to design the process of polymerization, asynchronous interaction between polymerization; force you to think, how to define domain event, will change in the field of state explicit; forcing you to various operating outside the field of explicit, namely define various command; forcing you to split the data and architecture of command side and query side separation, separation technology. The reduction is, we do not need to re design of unit of work, do not have to design domain service, without statistical information for the aggregation design various non redundant first hand.,

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Blithe at November 17, 2013 - 9:16 AM