Going over demo CQRS code here the command and event handlers are wired up separately as below:
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();
I am using an IoC container hooked in with membus and it works a dream by implementing the IEnumerable<object> GetAllInstances(Type desiredType)
interface with my container, however unlike the demo with this method of registration I cannot split out the interfaces for separate commands and events:
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();
Can anyone please let me know if there any way around this so we can register for an arbitrary number of types?
I am afraid that the current version of MemBus cannot do that - for no particular reason, mind you. I know that it makes sense to be able to distinguish between Events and Commands, even though the underlying infrastructure is the same.
The only workaround right now is to use a single interface to hook the IOC into MemBus.
If such a feature should be introduced into MemBus one has to think how the lookup mechanism into the IOC Container should look like. It would probably have to request all handlers of all interfaces, or some way to categorize / distinguish between event and command "messages" would have to be introduced.