Currently moving away from handling commands sync and placing them onto a message bus so they can be handled later but having problems getting back the real type when trying to to load commands rather then type each one out
Here is what I have so far and seems to work fine
Command Dispatcher
public class CommandDispatcher : ICommandDispatcher
{
private readonly IBus _commandBus;
public CommandDispatcher(IBus commandBus)
{
_commandBus = commandBus;
}
public void Dispatch<TCommand>(TCommand command) where TCommand : ICommand
{
var messageType = typeof(TCommand);
_commandBus.Publish(messageType, command);
}
Subscribing to commands
bus.Subscribe<CommandOne>("key", HandleCommand);
bus.Subscribe<CommandTwo>("key", HandleCommand);
Handling messages
private void HandleCommand<TCommand>(TCommand command) where TCommand : ICommand
{
var handler = _container.Resolve<ICommandHandler<TCommand>>();
handler.Handle(command);
}
I basically want to resolve my messages by convention so heres what I want to move towards to so I dont have to type each command type but having issues getting the type back
var commandAssembly = Assembly.GetAssembly(typeof(ICommand));
var commands = commandAssembly.GetTypes().Where(x => typeof(ICommand).IsAssignableFrom(x));
foreach (var command in commands)
{
bus.Subscribe(command, "key", x =>
{
HandleCommand(x);
});
}
Now x is just an object that I cannot pass to the Handlecommand method
Im using autofac for my container and easynetq for my bus.