2

In our DDD project we are using a Factory to create our initial aggregate root model 'Order'. There is a business rule that says new Orders have their supplier 'Order.Supplier' defaulted to a specific supplier. We would need to fetch the default supplier from the database.

Is it okay to call the supplier repository in the Order factory when creating the initial order to get the default value? I know that the factories purpose is to create the model in a valid state. A valid new state would require the default value set. In this case am I allowed to call the repository or should I pass in the default supplier into the factory constructor?

3 Answers3

3

+1 on the fact that the Factory OR a DomainService could access a repositor.

BUT

-1 on :

I think this has nothing to do with Domain at all. I think it is an Application rule.

Think with me: What if the business decide now that the default supplier would be another one. Is that a rule that would require a change on which business you are? Would that require a change to your factory? Is that okay?

Remember: Domain points to "Which business we are in" and not "How we do business".

I would rather put this on a ApplicationService. The domain doesn't need to know if there is a default supplier or not. It might require a "supplier" no matter what.

The Order may have a constructor, say Order(Supplier supplier) - > that will force na order to have a supplier.

Or The factory may receive a DefaultSupplierId at its method.

But the domain should NEVER know about it. It doesnt fit there in my opinion.

Bruno

1

The simple answer to your question is, simply, YES.

Yves Reynhout
  • 2,982
  • 17
  • 23
  • Thanks. Since technically the object can still be valid with initializing the Supplier and not setting the default I wasn't sure if this is the correct place for it. – user2354863 May 06 '13 at 20:51
  • I tend to lean towards having the app service call the repository and pass required data to factory. This can reduce coupling and enhance SRP. – eulerfx May 07 '13 at 18:14
  • 1
    Not to split hairs, but I would not put that in an application service, but rather in a domain service (if one doesn't want a dependency on the repository in the factory that is) that embodies the use case. But all in all we're talking details, personal experience and convenience at this point. – Yves Reynhout May 07 '13 at 18:26
0

It can, and there is a very specific case where a Factory needs to call a Repository. When Aggregate identity is generated by ORM (NHibernate HiLo for example) the Repository would expose a method like

class SomeRepository {
    ...
    Identity Generate();
    ...
}

And if Aggregate construction is complex enough to deserve a dedicated Factory, it is natural to call 'Generate' from the Factory.

Community
  • 1
  • 1
Dmitry
  • 17,078
  • 2
  • 44
  • 70