2011-09-21 101 views
1

我想通過它實現了一個接口來訪問我聚合根:CommonDomain/EventStore接口擷取

repository.GetById[[IMyInterface]](id);  

什麼我需要告訴CommonDomain或EventStore做到這一點?我相信我的IConstructAggregates收到執行存儲事件的聚合類型。我需要保留自己的ID的地圖嗎?

例如,說我有這總比分根:

class AggRoot1 : IInterface1, IInterface2 {} 
class AggRoot2 : IInterface1, IInterface2 {} 

我已經保存有 'idFromAggRoot1' 的aggregate1實例。 現在我要像這樣獲取:

repository.GetById<IInterface1>(idFromAggRoot1); 

我怎樣才能知道我應該創建以後,因爲有IInterface1兩個執行者? AggRoot1? AggRoot2? IInterface1? Activator會在這裏炸彈,所以我知道我需要實現IConstructAggregates,但不知道是否有其他描述符告訴我原始提交agg根類型是什麼。

+0

請給我解釋之實踐使用這樣的結構是什麼?你是否在思考「角色」? –

+0

剛剛看到這一點 - 實際使用是編寫接口在我的總根,而不需要客戶端知道它正在執行的實際實現。 – SonOfNun

+0

我承認解耦可能是一件好事,但是你能給出一個兩個聚合根類型的例子來實現相同的接口但不共享相同的實現嗎? –

回答

2

IConstructAggregates.Build()方法可以實現返回你需要的任何類型。

Common.AggregateFactory默認實現通過Activator.CreateInstance(type) as IAggregate創建聚合實例。

一個自己的實現可能是這樣的:

public class MyAggregateFactory : IConstructAggregates 
{ 
    public IAggregate Build(Type type, Guid id, IMemento snapshot) 
    { 
     if (type == typeof(IMyInterface)) 
      return new MyAggregate(); 
     else 
      return Activator.CreateInstance(type) as IAggregate; 
    } 
} 

編輯:聚合類型被存儲在事件消息的標題爲EventMessage.Header[EventStoreRepository.AggregateTypeHeader]

+0

謝謝,但仍然不回答我的問題。我編輯了我的帖子以提供更多詳細信息 – SonOfNun

+0

請參閱上面的我的編輯。 –

+0

這有幫助,謝謝。看起來我需要擴展EventStoreRepository來獲取流並讀取第一個事件來獲取元數據。我在回購中提出了一個問題,在IConstructAggregates中提供這個問題。 – SonOfNun