Factory

1 min read

As the name suggests, Factory create Aggregates. It’s not that every Aggregate requires its Factory, usually instantiating will take place somewhere during orchestration on Application level. But what about more complex Aggregates where some business rules come into play?

Who Doesn’t Like A Pizza?

To illustrate how useful and convenient Factory is, let’s create a Pizza Aggregate together with Factory service responsible for delivery of fully initialized objects graph.

Our Factory has two building methods that create two types of Pizza: a simple margherita and slightly more complex tuna. First one needs the size only, because option like double cheese is not available. Second one has four toppings and two rules, in case of big Pizza client gets extra free onion and double cheese.

Summary

Factory is a great building block of Tactical DDD, it creates complex object graphs based on business logic. Thanks to the specialized building methods, we may obtain a valid and consistent Aggregate without any further checks.