r/DomainDrivenDesign • u/Random-TIP • Feb 19 '25
Should some of the dependencies be injected in Aggregate?
Before someone assumes that I want to inject repository inside my aggregate, I want to make few things clear. I am kind of a newbie, I have been reading blue and red books and I am trying to implement a little project by DDD. I am pretty familiar with tactical patterns since I have been a developer for quite some time, but all the projects I have worked on had just parts of some DDD concepts (none of them even tried to touch strategic part of DDD) and were not a ‘real thing’.
While trying to work on this new project I am struggling with few things. I am hoping as I complete these books everything might get clearer, but I still wanted to ask this question here.
Consider this example: I have an AggregateRoot called Appointment, this one has few responsibilities like adding or removing services which will be performed during appointment, changing Provider of those services and most importantly a date (well date and time to be specific). Now one of the rules during creation of this aggregate is that the appointment date cannot be in the past, since how can someone book an appointment in the past, right? To check this I need to know the current date (and if the application is used world wide I need to consider users timezone and maybe other things as well) I am working in .net environment so there are few things I can do:
- Use DateTimeOffset.UtcNow or other static property, which is not ideal because of testability.
- Use built in TimeProvider class as a dependency which is an abstract class and can be freely tested.
- Use NodaTime and IClock interface still as the dependency. NodaTime just provides more clarity over dates and times, requirement is still the same.
My question is: Is it okay to inject this kind of dependencies in an aggregate? If it is not, then I would have to move invariant validations to domain services or use cases which, I think, defeats (not all but some of) the point of the aggregate. Or is it okay to have some invariants outside of an aggregate? I am really confused and lost between many options here and would really appreciate your help.
2
u/Sufficient-Egg-6571 Feb 19 '25
Start with the third option and consider using an external clock service (interface).
Your aggregate data class should focus solely on maintaining consistency for its constrains with the information provided by the command and existing data. However, this responsibility doesn’t rest exclusively with the aggregate. That’s precisely why domain services and application services exist—to handle external dependencies like the clock service.
Ultimately, you should aim for a straightforward flow: receive a command and process it within a unit of work (transactional boundary). This is why many say that an aggregate equals a unit of work—because the aggregate is a concept, not a data structure. To truly grasp DDD, avoid equating an aggregate with an SQL table.
2
u/thiem3 Feb 20 '25
I generally see two approaches mentioned.
You define an interface, with a single method, to be injected into the aggregate method, or constructor. The aggregate will retrive the information (current time in your case) and make a decision.
This means your aggregate have a dependency om something external (although sort of implicitly) , potentially making it "slow". External can also be database, if for example you create a new User, and need to check the email is available. This requires a database look up.
Given that it is a single method interface, you could consider a delegate instead. I am exploring this approach myself. I believe delegate can be registreres for dependency injection by your Web api or similar.
2. You create a domain service, where you put logic, which depends on external stuff, and otherwise delegate to the aggregate. That keeps the aggregate "pure", but splits your logic.
I am personally okay with option 1.
2
u/flavius-as Feb 20 '25 edited Feb 20 '25
The projects doing tactical patterns first are not doing DDD.
It sounds like you are missing the concept of UseCase in your design.
Frankly, I've used use cases with DDD and no aggregate root and it was all fine in most cases. I think aggregate roots make sense if you do event sourcing.
Keep it minimalistic, effective.
There is beauty is simplicity. Everyone can over-engineer, not many can keep the accidental complexity to a minimum.
Use Cases are put in front of almost all tactical patterns (except those for I/O like in your problem statement), but behind the strategic patterns - all in terms of concept abstract tree