1

I have an entity, Exchange. Exchange needs to populate a list of a VOs, CurrencyPair, at startup. The collection of CurrencyPair is stored in database as master data, and has a repository called ICurrencyPairRepository to get all the available currency pairs. So I will like to get the CurrencyPair collection in the Exchange entity once the app starts.

My question is, can I directly inject the ICurrencyPairRepsoitory's implementation into the Exchange? Or should there be an Infrastructure service implementation that get the CurrencyPairs from the repository?(Ofcourse the ICurrencyPairRepository interface is inside the domain layer, it's implementation is in infrastructure layer)

Or should I inject an application service in Exchange? Are we allowed to inject only Domain Service into an Entity or other services/repositories can be injected too?

Use case:

At start up time, the exchange needs to get all the currency pairs that are allowed to be traded in the application. Whenever a new order comes in to Exchange, it contains the currency pair which it wants to trade. The Exchange then needs to check whether this currency pair is allowed to be traded or not. If yes, it forwards the order, otherwise, discards the request.

As Exchange is an aggregate root, I can initialize it from where the application starts and provide it with a set of currency pairs, but I am interested to know what is allowed to be injected in an entity which is also the aggregate root in this case.

Syed Waqas
  • 2,576
  • 4
  • 29
  • 36
  • 2
    Generally, it is not recommended to inject service/repository to any entities. Could you post your use case also? – Yugang Zhou Oct 14 '14 at 23:49
  • 2
    The reasoning for Hippoom's comment is that adding services and repositories to an entity makes it harder to create and reconstitute the entity, as well as making its behaviour harder to understand. It would help your question if you could give an example of a method in `Exchange` which makes use of the `CurrencyPair` collection. – Ed I Oct 15 '14 at 05:41
  • 1
    @WaqasShah Please review the definitions of Value Object, Repository and Application Service in DDD. Judging by your question, they might not be what you think they are. The notion of Aggregate can help, too. – guillaume31 Oct 15 '14 at 11:48
  • I have provided the use case, please review. also, I do understand the VO, Application service and Repository, but I just want to understand the boundaries that constraint us when using entities with services. Thanks – Syed Waqas Oct 17 '14 at 04:27
  • 1
    Entities very rarely need to use Services, because they usually have all they durably need inside their own aggregate (in the form of other Entities and VOs), and all they occasionally need passed to them as method parameters. Application Services can't be referenced by domain objects because they are in a different, unreachable layer. – guillaume31 Oct 17 '14 at 07:36

1 Answers1

0

For some people it is acceptable to inject repository dependencies directly into the entity, personally I really dislike this solution. It is hard to focus in behavior when you deal also with infrastructure (SRP violation).

What I usually try to do is an application service who retrieve the whole information needed by the entity... And then I am really confortable with testing and writing the behavior.

rad
  • 1,857
  • 1
  • 20
  • 30