Ddd-use aggregate to design class libraries

Source: Internet
Author: User
ArticleDirectory
    • Aggregation
    • Maintain data consistency and strengthen the invariant
    • How do external objects reference internal objects?
    • Variable root object

Openexpressapp uses the csla Class Library Framework. When designing class libraries, you can use the domain-driven design concept of DDD. This article introduces the concept of aggregation, after understanding this concept, you can clearly understand the relationship between management databases, which can reduce the complexity of complex systems and improve maintainability.

Aggregation

A model contains a large number of domain objects. No matter how many considerations are made during design, we will see that many objects are associated with other objects and form a complex network, if the design is poor, this object network will bring poor maintainability and low performance.

Aggregation is a domain pattern used to define object ownership and boundaries. ItUse boundaries to separate internal and external objects,A series of related object combinations for data changes. Aggregation makes the model more compact by defining clear ownership and boundaries, avoiding the network of objects (LINKS) with root errors, which can reduce unnecessary relationships between objects, associate closely related objects to improve system management.

The aggregate class hides the usage of the Collaboration class from the caller and can be used to encapsulate the complex, invasive, and State-dependent requirements of the domain class.

Rules
    1. Each aggregation has a root object, which is an entity.
    2. The external object can only access the root object. If you want to access the aggregated sub-object, you must also navigate through the root object
    3. The root object and sub-object can be referenced to any other aggregated root object.
    4. When the root of the aggregation is established, all the objects contained in the aggregation are established, and all the constants are enhanced.
Maintain data consistency and strengthen the invariant
    1. Other objects can only hold references to the root object, but cannot directly modify the aggregated sub-objects. To change sub-objects, they can only perform certain operations through the root object.
    2. The root object can change other objects, but this is an operation contained in the aggregation and it is controllable.
    3. If the root is deleted or removed from the memory, all other objects in the aggregate will also be deleted, because no other objects will hold any of them. When modifications to the root object indirectly affect other objects in the aggregation, it is easy to strengthen the invariant because the root will do this. If external objects can directly access internal objects and change them, it will become difficult to manage the consistency and immutability of these objects.
How do external objects reference internal objects?
    1. Navigation through the root object
    2. During temporary use, the root object can pass internal temporary references to external objects, but as a restriction, after the operation is complete, the external object cannot hold this reference. A simple implementation method is to pass a copy of a value object to an external object. What happens to this object is no longer important because it does not affect the consistency of aggregation in any way.
Variable root object

Sometimes an object is an aggregated root object at a certain time, and another aggregated sub-object at another time. This type of situation usually appears in the Resource Information,

    1. There are many materials under the material category. In this case, the material Category + material is an aggregation and the material is a sub-object.
    2. The warehouse receiving ticket details are stored into one material. At this time, the warehouse receiving ticket + warehouse receiving ticket details are aggregated, and the material is another aggregated root object referenced externally.

The above example shows that material resources can sometimes be used as root objects and can be designed as sub-objects. In csla2008, a root object that can be switched is used to handle this situation. However, when this happens, I always feel awkward.CodeI also feel uncomfortable. One of my suggestions is as follows:

If the internal object is only an external objectRead-only reference, You can obtain sub-objects in the following way, so that the material object is only a sub-object in the aggregation object, and does not need to be able to switch the root object.

 

If you have other methods, please reply!

 

 

Refer:Dddquicklychinese Edition

 

You are welcome to reprint it. Please note: Reprinted fromZhou jingen [http://zhoujg.cnblogs.com/]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.