问题
I would like to know how to implement factories in domain driven design. (examples)
Where should be placed interfaces and implementations of factories ? Do I need to create interfaces for Domain objects which factories creating ? Do I need to create factories for repositories, services, ...
I am using Dependency Injection Containers how can I put them together with factories ?
Thanks.
回答1:
Factories should be simple classes, usually static. They can also be implemented as static methods on the entity or value object they create. Factories should create domain objects directly and only domain objects. Moreover, factories should not be tied with dependency injection because domain objects shouldn't have dependencies injected into them.
Domain objects should not implement interfaces - that is a needless abstraction.
Services and repository implementations on the other hand do have dependencies and should be created by the DI container.
来源:https://stackoverflow.com/questions/15299970/ddd-how-to-implement-factories