[英]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
{
}
聚合根只是一個實體,您無需顯式定義整個聚合。 聚合是對象(實體和值)的層次結構,可以通過聚合根來解決。
因此,根據您對IEntity
, EntityBase
和IAggregateRoot
定義,我認為:
知識庫
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 TRoot
, Entity<TAggregateRoot>
explicit TRoot
。
另外,請不要過於籠統,保持簡單。 在大多數應用程序中,您只需要為IAggregateRoot
實現一個非通用接口 ,為Entity
一個基類或接口 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.