Reputation: 2229
I'm currently trying to define certain base classes & interfaces to use them in DDD & CQRS projects, however I'm struggling with the definition of aggregates and aggregate roots.
The blue book tells us that...
For this, I made the following classes / interfaces:
public interface IEntity<TKey> {
TKey Key { get; set; }
}
public abstract class EntityBase<TKey> : IEntity<TKey> {
// key stuff, equality comparer..
}
public interface IAggregateRoot<TKey> : IEntity<TKey>
{
}
public interface IRepository<TAggregate, TRoot, TKey>
where TAggregate : IAggregate<TRoot>
where TRoot : IAggregateRoot<TKey>
{
TRoot Root { get; set; }
void Add(TAggregate aggregate);
}
Now, did I understood this correctly? How might a aggregate interface look like then?
public interface IAggregate<TRoot, TKey>
where TRoot : IAggregateRoot<TKey>
{
}
I tried to find some references and in a CQRS framework I found the following implementation: (Does CQRS differ so much from DDD? I thought it's pretty much the same when not applying event sourcing)
public abstract class Entity<TAggregateRoot> where TAggregateRoot
: AggregateRoot
{
}
Upvotes: 0
Views: 4365
Reputation: 1151
Aggregate root is just an entity and you need not to explicitly define whole aggregate. Aggregate is a hierarchy of objects (entities and values), which can be addressed through aggregate root.
So, with your definition of IEntity
, EntityBase
and IAggregateRoot
I consider:
Repository
public interface IRepository<TAggregateRoot, TKey>
where TAggregateRoot : IAggregateRoot<TKey>
{
TAggregateRoot Get(TKey id);
void Delete(TAggregateRoot aggregateRoot);
void Add(TAggregateRoot aggregateRoot);
}
Aggregate root entity
public abstract class AggregateRootEntityBase<TKey>
: EntityBase<TKey>, IAggregateRoot<TKey>
{
}
Constructions like IAggregate<TRoot, TKey>
, explicit TRoot
, Entity<TAggregateRoot>
need not to be realized.
Also, please, don't overgeneralize and keep it simple. In most applications you just have to implement one non-generic interface for IAggregateRoot
and one base class or interface for Entity
.
Upvotes: 1