Validation within aggregates

Hi all,

When validating within aggregates sometimes we need information which
is provided by query repositories, hence we have to inject them into
the aggregates, I was wondering when/how to inject those query
repositories since aggregates are loaded from aggregate repository.

Thanks & Regards,


Hi Setya,

this is primarily a domain of personal preference. Some people like to inject domain objects with infrastructural objects, such as repositories, services, etc. An alternative (and my preference) is to have the command handler use these services to get the data and pass that as parameters to the methods that need them. If a domain class really needs a service, I pass the implementation of that service as a method parameter.

If you want to inject resources into your aggregates, there are a few options. One is to use the SpringPrototypeEventSourcingRepository, which uses a Spring-defined propotype bean to inject resources. The other is to create a specialized implementation of a Repository (the AbstractEventSourcingRepository only has one abstract method) that injects resources just prior to returning the reconstructed instance. If you don’t use event sourcing, extend the AbstractRepository (or one of the more specialized subclasses) to do the job.



Hi Seyta,

I agree with Allard that putting such checks in the command handler is
a good idea:

There is a "unique check" done for user name and email address:
Code line: "constraintSet.add(userName, emailAddress);"

In the above case it's a small database inside the command server
itself but it could also be a call to the query server.



Thank you all for your response, I'll check the source code examples.