user2354863
user2354863

Reputation: 71

Can a Domain Model Factory Call a Repository?

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?

Upvotes: 2

Views: 1224

Answers (3)

Bruno Bertechini
Bruno Bertechini

Reputation: 31

+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

Upvotes: 3

Dmitry
Dmitry

Reputation: 17350

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.

Upvotes: 0

Yves Reynhout
Yves Reynhout

Reputation: 2990

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

Upvotes: 1

Related Questions