Factories

Learn what a factory in Domain Driven Design is and how to implement it to create complex entities.

As Eric Evans mentions in his book, there may be occasions when an object’s creation and assembly are complex. Such a process may need to orchestrate a lot of logic in order to create the complex object. He mentions an example related to open a bank account, this process requires many objects in order to be executed properly. Oftentimes the logic required to create those objects is not related to business logic. It can lead to messing up the business logic. To deal with this situation, Domain Driven Design suggests implementing a factory.

What are factories?

Factories are a tactical pattern in the Domain Driven Design world. It helps in creating complex objects. It is important to keep in mind implementing this pattern only when the instantiation of an object is complex. Factory may add unnecessary complexity.

Despite helping in creating aggregates, it does not mean a factory has to be in every aggregate inside the domain logic. A factory should only be inside the aggregate root as this is the object that orchestrates all business logic.

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy