简体   繁体   English

汇总和汇总根

[英]Aggregate & Aggregate Roots

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 . 我目前正在尝试定义某些基类和接口,以在DDD和CQRS项目中使用它们,但是我正在努力定义聚合聚合根

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) 我试图找到一些参考,并在CQRS框架中找到以下实现:(CQRS与DDD有很大不同吗?我认为在不应用事件源时它几乎相同)

public abstract class Entity<TAggregateRoot> where TAggregateRoot
    : AggregateRoot
{

}

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: 因此,根据您对IEntityEntityBaseIAggregateRoot定义,我认为:

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. IAggregate<TRoot, TKey>explicit TRootEntity<TAggregateRoot> explicit TRoot

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 . 在大多数应用程序中,您只需要为IAggregateRoot实现一个非通用接口 ,为Entity 一个基类或接口

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM