2013-04-20 80 views
6

去在demo CQRS code here命令和事件處理程序是分別有線瞭如下:多種類型的用於SetHandlerInterface()與membus和IOC容器

public interface CommandHandler<in T> 
{ 
    void Handle(T command); 
} 

public interface EventHandler<in T> 
{ 
    void Handle(T @event); 
} 

bus = BusSetup.StartWith<Conservative>() 
     .Apply<FlexibleSubscribeAdapter>(a => 
     { 
      a.ByInterface(typeof(IHandleEvent<>)); 
      a.ByInterface(typeof(IHandleCommand<>)); 
     }) 
     .Construct(); 

我使用與membus鉤住在IoC容器和它的工作原理通過實現IEnumerable<object> GetAllInstances(Type desiredType)接口與我的夢想容器,但是不像登記這個方法,我不能拆出來單獨命令和事件的界面演示:

this.Bus = BusSetup.StartWith<Conservative>() 
    .Apply <IoCSupport>(c => 
     { 
      c 
      .SetAdapter(SimpleInjectorWiring.Instance) 
      .SetHandlerInterface(typeof(CommandHandler<>)) 
      /*.SetHandlerInterface(typeof(EventHandler<>))*/; 
      // only CommandHandler or EventHandler can be used - not both 
     }) 
    .Construct(); 

任何人都可以請讓我知道如果有什麼辦法在這附近,我們可以註冊任意數量的類型?

回答

3

恐怕當前版本的MemBus無法做到這一點 - 沒有特別的原因,請介意。我知道,儘管底層基礎架構相同,但能夠區分事件和命令是有意義的。

現在唯一的解決方法是使用單個接口將IOC掛接到MemBus中。

如果應該在MemBus中引入這樣的功能,必須考慮IOC容器中的查找機制應該如何。它可能不得不請求所有接口的所有處理程序,或者需要採用某種方式對事件和命令「消息」進行分類/區分。

3

我只是設置此爲概念的證明,因爲我用的每Microsoft CQRS Journey guidelines以下約定:

我想使用的許多事件處理IOC容器的(SimpleInjector)API強制實施該慣例,因爲它迫使您製作single vs multi handlers registrations explicit by design。現在,我的IOC容器在任何時候都會拋出一個Exception來處理同一命令的兩個處理程序被意外註冊。

爲了讓MemBus支持這個約定,我需要創建自己的ISetup,ISubscriptionResolver和IoCAdapter(分別從IoCSupport,IoCBasedResolver和IocAdapter的代碼開始)。我還必須創建一個新的IocAdapter接口,該接口也支持單一的GetInstance()方法;現在該接口大致匹配System.Web.Http.Dependencies.IDependencyScope接口(GetService和GetServices)。

// Setup 
return BusSetup 
    .StartWith<Conservative>() 
    .Apply<CommandingEventingSupport>(
     adapter => 
      { 
       adapter.SetAdapter(new MemBusSimpleInjectorAdapter(container)); 
       adapter.SetCommandHandlerInterface(typeof(IHandleCommand<>)); 
       adapter.SetEventHandlerInterface(typeof(IHandleEvent<>)); 
       adapter.SetCommandTest(obj => obj is IDomainCommand); 
      }) 
    .Construct(); 

然後解析器分派給的GetInstance命令和事件GetAllInstances ...

// Command vs Event 
    public IEnumerable<ISubscription> GetSubscriptionsFor(object message) 
    { 
     bool isCommand = _isCommandFunc(message); 
     Type typeToCreate = isCommand 
      ? _commandHandlerType 
      : _eventHandlerType; 
     var handlesType = ConstructHandlesType(typeToCreate, message.GetType()); 
     var mi = handlesType.GetRuntimeMethods().First(); 
     return isCommand 
        ? new[] { mi.ConstructSubscription(_adapter.GetInstance(handlesType)) } 
        : _adapter.GetAllInstances(handlesType).Select(mi.ConstructSubscription); 
    }