user3054039
user3054039

Reputation: 29

The use of factories and interfaces in domain driven design

I am a newbie in DDD. I am trying to figure out if it is acceptable to have all entities created from factor(ies) even for those with simple constructors. The reason being I'd like to have all entity classes to implement interfaces to allow the flexibility of changing the implementation classes without changing the classes that use these entity classes. For example: if we are phasing out the use of a certain entity class gradually.

I would like to hear if this something that anybody else has considered or your comments. I welcome any comments pro/against as long as you provide logical reasoning behind it.

Thank you

Upvotes: 2

Views: 170

Answers (2)

Mik378
Mik378

Reputation: 22171

Nothing wrong about using factories for all entities that are Aggregate roots.

Factories are useful when there is a need to validate the entity with elements that should not belong to the entity itself, like a call of isEmailUnique() (asking repositories for response).

Warning: CQRS goes against the validation of email unique on server (command side), but it's another subject.

Factories would be useful to implement validation that encompasses several entities.

Of course, if you really don't need a specific validation concept, YAGNI is here: simply validate inputs in the entity's constructor, or better, making use of the builder pattern (from Joshua Bloch).

Upvotes: 1

user2880879
user2880879

Reputation: 297

Hi Regarding your first question. "Should you be able to create Entity classes using its constructor" then my answer is yes. Because this object is then used by business objects and data access layer (DAO). Its good idea to have factory method for business objects and DAOs. Because when you need to create these classes you want to inject dependencies for example database connections, reference to different beans etc.. You should use Spring in this case.

Lets say when you phase out some entities along the line. Means your domain model has changes and also that means your business logic has changed. In this case you have to make changes to DAOs and Business logic layer to incorporate those changes. So there is not escape from re-factoring your code during these kind of events.

Upvotes: 0

Related Questions