Skip to content

Using Factory method instead of Constuctor #214

Answered by AndreiGanichev
mehdihadeli asked this question in Q&A
Discussion options

You must be logged in to vote

Hi, @mehdihadeli.

@kgrzybek mentioned about some advantages here:

  1. You can put in Factory method some validation logic which is important to create an object from Application. Infrastructure on the contrary uses private constructors to be able restore persisted object even after validation rules were changed.
  2. Factory method has a name so your code becomes more clear.

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.…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@mehdihadeli
Comment options

Answer selected by mehdihadeli
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants