Using Factory method instead of Constuctor #214
-
Hi @kgrzybek, |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi, @mehdihadeli. @kgrzybek mentioned about some advantages here:
Factory method has other advantages(can be async, can return objects from cache) but for Domain model I think we can apply this one: it can hide inheritance hierarchy so you can return a subtype from the method. Of corse sometimes for simple aggregates and entities Factory method doesn't have most of these advantages and we could use ctor instead. But it's much practical to decide once that Factory method should be used to create entities from Application so you could think about complex questions in domain model instead of ctor vs Factory method choice every time. To automate such decisions there is architecture tests, look at |
Beta Was this translation helpful? Give feedback.
Hi, @mehdihadeli.
@kgrzybek mentioned about some advantages here:
Factory method has other advantages(can be async, can return objects from cache) but for Domain model I think we can apply this one: it can hide inheritance hierarchy so you can return a subtype from the method.
Also pay attention that often Application doesn't instantiate aggregates but delegate this to Domain model itself.…