Examples of "use case optimal query" and partial aggregates in DDD

In Vaughn Vernon's book "Implementing Domain-Driven Design", he talked about repositories that allows you to query for partial aggregates using a "use case optimal query": (from page 430) It may at times be advantageous to query Aggregate parts out of the Repository without directly accessing the Root itself. This might be so if an Aggregate holds a large collection of some Entity type, and you need to get access only to the instances that match a certain criterion. Of course, this might make sense only if the Aggregate allows for such access by navigation through the Root. You wouldn’t design a Repository to provide access to parts that the Aggre- gate Root would not otherwise allow access to by way of navigation. Doing so would violate the Aggregate contract. Another reason might influence you to design in special finder methods. Certain use cases of your system may not follow the exact contours of a single Aggregate type when rendering views of domain data. They may instead cut across types, possibly composing just certain parts of one or more Aggregates. In situations like this you might choose not to, in a single transaction, find whole Aggregate instances of various types and then programmatically compose them into a single container, and supply that payload container to a client. You might instead use what is called a use case optimal query. This is where you specify a complex query against the persistence mechanism, dynamically placing the results into a Value Object (6) specifically designed to address the needs of the use case. But I have a hard time visualizing what a concrete implementation would look like. If that "large collection of some Entity type" have to be accessible through the aggregate root anyways, then what's the point of having a repository that allows partial access to the aggregate? Are there any public repositories or resources that shows a concrete implementation of the above concept? There are these two well-known projects: https://github.com/VaughnVernon/IDDD_Samples (by Vaughn Vernon) https://github.com/citerus/dddsample-core (by Eric Evans' company) But they unfortunately don't include an implementation of the concept. Any examples would be really appreciated.

Jan 18, 2025 - 10:05
Examples of "use case optimal query" and partial aggregates in DDD

In Vaughn Vernon's book "Implementing Domain-Driven Design", he talked about repositories that allows you to query for partial aggregates using a "use case optimal query":

(from page 430)

It may at times be advantageous to query Aggregate parts out of the Repository without directly accessing the Root itself. This might be so if an Aggregate holds a large collection of some Entity type, and you need to get access only to the instances that match a certain criterion. Of course, this might make sense only if the Aggregate allows for such access by navigation through the Root. You wouldn’t design a Repository to provide access to parts that the Aggre- gate Root would not otherwise allow access to by way of navigation. Doing so would violate the Aggregate contract.

Another reason might influence you to design in special finder methods. Certain use cases of your system may not follow the exact contours of a single Aggregate type when rendering views of domain data. They may instead cut across types, possibly composing just certain parts of one or more Aggregates. In situations like this you might choose not to, in a single transaction, find whole Aggregate instances of various types and then programmatically compose them into a single container, and supply that payload container to a client. You might instead use what is called a use case optimal query. This is where you specify a complex query against the persistence mechanism, dynamically placing the results into a Value Object (6) specifically designed to address the needs of the use case.

But I have a hard time visualizing what a concrete implementation would look like. If that "large collection of some Entity type" have to be accessible through the aggregate root anyways, then what's the point of having a repository that allows partial access to the aggregate?

Are there any public repositories or resources that shows a concrete implementation of the above concept? There are these two well-known projects:

But they unfortunately don't include an implementation of the concept.

Any examples would be really appreciated.