DDD Quickly by Unknown
Author:Unknown
Language: eng
Format: epub
Published: 2014-05-30T16:00:00+00:00
Factories
Entities and Aggregates can often be large and complex – too complex to create in the constructor of the root entity. Infact trying to construct a complex aggregate in its constructure is in contradiction with what often happens in the domain itself, where things are created by other things (like electronics get created in on assembly lines). It is like having the printer build itself.
When a client object wants to create another object, it calls its constructor and possibly passes some parameters. But when the object construction is a laborious process, creating the object involves a lot of knowledge about the internal structure of the object, about the relationships between the objects contained, and the rules applied to them. This means that each client of the object will hold specific knowledge about the object built. This breaks encapsulation of the domain objects and of the Aggregates. If the client belongs to the application layer, a part of the domain layer has been moved outside, messing up the entire design. In real life, it is like we are given plastic, rubber, metal, silicon, and we are building our own printer. It’s not impossible, but is it really worth doing it?
Creation of an object can be a major operation in itself, but complex assembly operations do not fit the responsibility of the created objects. Combining such responsibilities can produce ungainly designs that are hard to understand.
Therefore, a new concept is necessary to be introduced, one that help to encapsulate the process of complex object creation. This is called Factory. Factories are used to encapsulate the knowledge necessary for object creation, and they are especially useful to create Aggregates. When the root of the Aggregate is created, all the objects contained by the Aggregate are created along with it, and all the invariants are enforced.
It is important for the creation process to be atomic. If it is not, there is a chance for the creation process to be half done for some objects, leaving them in an undefined state. This is even more true for Aggregates. When the root is created, it is necessary that all objects subject to invariants are created too. Otherwise the invariants cannot be enforced. For immutable Value Objects it means that all attributes are initialized to their valid state. If an object cannot be created properly, an exception should be raised, making sure that an invalid value is not returned.
Therefore, shift the responsibility for creating instances of complex objects and Aggregates to a separate object, which may itself have no responsibility in the domain model but is still part of the domain design. Provide an interface that encapsulates all complex assembly and that does not require the client to reference the concrete classes of the objects being instantiated. Create entire Aggregates as a unit, enforcing their invariants.
There are several design patterns used to implement Factories. The book Design Patterns by Gamma et all. describes them in detail, and presents these two patterns among others: Factory Method, Abstract Factory. We won’t try to present the patterns from a design perspective, but from a domain modeling one.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Fanny Burney by Claire Harman(26249)
Empire of the Sikhs by Patwant Singh(22774)
Out of India by Michael Foss(16695)
Leonardo da Vinci by Walter Isaacson(12810)
Small Great Things by Jodi Picoult(6691)
The Six Wives Of Henry VIII (WOMEN IN HISTORY) by Fraser Antonia(5241)
The Wind in My Hair by Masih Alinejad(4850)
The Crown by Robert Lacey(4578)
The Lonely City by Olivia Laing(4572)
A Higher Loyalty: Truth, Lies, and Leadership by James Comey(4558)
The Iron Duke by The Iron Duke(4126)
Millionaire: The Philanderer, Gambler, and Duelist Who Invented Modern Finance by Janet Gleeson(4108)
Sticky Fingers by Joe Hagan(3916)
Papillon (English) by Henri Charrière(3914)
Joan of Arc by Mary Gordon(3790)
Alive: The Story of the Andes Survivors by Piers Paul Read(3737)
Stalin by Stephen Kotkin(3730)
Aleister Crowley: The Biography by Tobias Churton(3429)
Ants Among Elephants by Sujatha Gidla(3282)
