Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Few confusing things about factories in DDD

a) I'm a bit puzzled whether in most cases we should only have a factory that produces the entire Aggregate or should we also have a factory that creates only the Aggregate root?

b) Should a factory that builds the entire Aggregate build both root and non-root objects by itself or should it delegate the building of non-root entities/VOs to other factories?

Thus, if Aggregate contains 5 different types of non-root entities, should Aggregate factory create these non-root entities by itself or should we have additional five factories ( one factory for each non-root entity ), to which Aggregate factory would delegate the creation of particular type of non-root entity?

Thank you

like image 204
user437291 Avatar asked Feb 12 '13 18:02

user437291


People also ask

What is a factory in DDD?

A factory is a tactical pattern used in the DDD world. It helps us create complex objects. It is important to keep in mind that we should only implement this pattern when the instantiation of an object is complex. The use of a factory may add unnecessary complexity.

Where do I put my DDD factory?

Therefore, if you have some domain specific rules and logic you would like to enforce, place the factory in the domain layer - after all, the factory creates domain objects. Note however that you may have other types of factories in other layers.

Are factories part of the domain?

Factories have nothing to do with the domain of an organisation, but they do form an important part of the domain of an application. It is the domain of the application that is responsible for creating and working with Domain Objects.

What problems does Domain-Driven Design Solve?

This method also uses various principles and patterns to close the gap between business reality and software code. Domain-driven design is most helpful in solving domain complexities as it works to maintain the project's primary focus as the core domain.


1 Answers

In Eric Evans' DDD book, page 138, it's written in bold:

Create entire aggregates as a piece, enforcing their invariants.

Then in the next page:

A FACTORY should only be able to produce an object in a consistent state. For an ENTITY, this means the creation of the entire AGGREGATE [...]

Concretely, this means that you would only have one factory to create the entire aggregate. There may be other classes (factories) involved in building your non-root entities or value objects, but there is only one factory responsible for creating an aggregate. This factory creates a full aggregate, not just a root object.

like image 172
Meta-Knight Avatar answered Oct 21 '22 08:10

Meta-Knight