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...
- An aggregate is a cluster of objects
- Each aggregate has a aggregate root
- The aggregate root is a specific entity and the only object other parts of the application can reference to.
For this, I made the following classes / interfaces:
Entity
public interface IEntity<TKey> {
TKey Key { get; set; }
}
public abstract class EntityBase<TKey> : IEntity<TKey> {
// key stuff, equality comparer..
}
Aggregate Root
public interface IAggregateRoot<TKey> : IEntity<TKey>
{
}
Repository
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
{
}