What data type should a repository return?

In domain driven design, repositories are created for aggregate roots only. Does this mean that all repository methods should return an instance or a collection of the aggregate root for which the repository is defined? Is it incorrect to return an entity that is inside the aggregate? I have checked the code for the DDD sample project created by Eric Evans' company and Swedish company Citerus. The repository returns other data types but they seem related to the aggregate root. For example, ICargoRepository has a method that returns the next TrackingId, a value object. https://github.com/chandmk/esddd/blob/master/src/NDDDSample/app/domain/NDDDSample.Domain/Model/Cargos/ICargoRepository.cs /// /// Get A unique, generated tracking Id. /// /// tracking Id TrackingId NextTrackingId(); HandlingEventRepositoryHibernate has a method that returns HandlingHistory, which is constructed inside the method with a collection of HandlingEvent and returned. https://github.com/chandmk/esddd/blob/master/src/NDDDSample/app/infrastructure/NDDDSample.Persistence.NHibernate/HandlingEventRepositoryHibernate.cs public HandlingHistory LookupHandlingHistoryOfCargo(TrackingId trackingId) { return new HandlingHistory(Session.CreateQuery("from HandlingEvent as h where h.cargo.trackingId.id = :tid").SetParameter("tid", trackingId.IdString).List()); } TL;DR: Can a repository return entities/value objects other than the aggregate root for which it was defined?

Mar 11, 2025 - 20:52
 0
What data type should a repository return?

In domain driven design, repositories are created for aggregate roots only. Does this mean that all repository methods should return an instance or a collection of the aggregate root for which the repository is defined? Is it incorrect to return an entity that is inside the aggregate?

I have checked the code for the DDD sample project created by Eric Evans' company and Swedish company Citerus. The repository returns other data types but they seem related to the aggregate root.

For example,

ICargoRepository has a method that returns the next TrackingId, a value object. https://github.com/chandmk/esddd/blob/master/src/NDDDSample/app/domain/NDDDSample.Domain/Model/Cargos/ICargoRepository.cs

/// 
/// Get A unique, generated tracking Id.
/// 
/// tracking Id
TrackingId NextTrackingId();

HandlingEventRepositoryHibernate has a method that returns HandlingHistory, which is constructed inside the method with a collection of HandlingEvent and returned. https://github.com/chandmk/esddd/blob/master/src/NDDDSample/app/infrastructure/NDDDSample.Persistence.NHibernate/HandlingEventRepositoryHibernate.cs

public HandlingHistory LookupHandlingHistoryOfCargo(TrackingId trackingId)
{
    return new HandlingHistory(Session.CreateQuery("from HandlingEvent as h where h.cargo.trackingId.id = :tid").SetParameter("tid", trackingId.IdString).List());
}

TL;DR: Can a repository return entities/value objects other than the aggregate root for which it was defined?