簡體   English   中英

匯總和匯總根

[英]Aggregate & Aggregate Roots

我目前正在嘗試定義某些基類和接口,以在DDD和CQRS項目中使用它們,但是我正在努力定義聚合聚合根

藍皮書告訴我們...

  • 集合是一組對象
  • 每個聚合都有一個聚合根
  • 聚合根是一個特定實體 ,應用程序其他部分可以引用的唯一對象。

為此,我制作了以下類/接口:

實體

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);

}

現在,我是否理解正確? 聚合接口的外觀如何?

public interface IAggregate<TRoot, TKey>
    where TRoot : IAggregateRoot<TKey>
{

}

我試圖找到一些參考,並在CQRS框架中找到以下實現:(CQRS與DDD有很大不同嗎?我認為在不應用事件源時它幾乎相同)

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

}

聚合根只是一個實體,您無需顯式定義整個聚合。 聚合是對象(實體和值)的層次結構,可以通過聚合根來解決。

因此,根據您對IEntityEntityBaseIAggregateRoot定義,我認為:

知識庫

public interface IRepository<TAggregateRoot, TKey>
    where TAggregateRoot : IAggregateRoot<TKey>
{

    TAggregateRoot Get(TKey id);

    void Delete(TAggregateRoot aggregateRoot);

    void Add(TAggregateRoot aggregateRoot);

}

聚合根實體

public abstract class AggregateRootEntityBase<TKey>
: EntityBase<TKey>, IAggregateRoot<TKey>
{

}

IAggregate<TRoot, TKey>explicit TRootEntity<TAggregateRoot> explicit TRoot

另外,請不要過於籠統,保持簡單。 在大多數應用程序中,您只需要為IAggregateRoot實現一個非通用接口 ,為Entity 一個基類或接口

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM