Can a domain service inject multiple repositories or operate with different aggregate roots?

472 views Asked by At

Can a domain service inject multiple repositories or operate with different aggregate roots? If yes, would it be considered a violation of the SRP principle?

I am looking in depth and I have this question regarding domain services. If I have a business logic that involves working with several Aggregate Roots, what would be the best way to solve it with a Domain Service?

Thanks!

1

There are 1 answers

2
Eben Roux On

Can a domain service inject multiple repositories or operate with different aggregate roots?

I see no problem with this as I find that working with multiple aggregates is usually where you need a domain service since the functionality doesn't seem to fit on a particular aggregate. If the service does not fetch the relevant objects then they could be passed in from the integration/application concern. In fact, you probably want to start there: a method that accepts the relevant aggregates. You may find that it makes more sense to retrieve the objects in the caller before invoking the service method.

If yes, would it be considered a violation of the SRP principle?

The SRP is about what your class does and not what it uses so unless you are adding more functionality than is required I don't see how SRP is negatively affected.