I am trying to implement a Command, CommandHandler and CommandDispatcher pattern using Castle Windsor without manually asking the container to resolve a CommandHandler based on Command type (which is generally considered an anti-pattern).
I found this old article, but the implementation of ITypedFactoryComponentSelector
has changed, so now it returns a Func, instead of TypedFactoryComponent
.
Anyway, I would really appreciate if someone can shed some light on the "correct" implementation of this pattern. Current setup (simplified):
public interface ICommand {}
public class CreateUserCommand:ICommand
{
public string Name { get;set; }
}
public interface ICommandHandler<in TCommand> where TCommand: ICommand
{
ICommandResult Execute(TCommand command);
}
public class CreateUserCommandHandler : ICommandHandler<CreateUserCommand>
{
public ICommandResult Execute(CreateUserCommand command)
{
// some logic here
return new CommandResult() {Success = true};
}
}
public interface ICommandDispatcher
{
ICommandResult Submit<TCommand>(TCommand command) where TCommand: ICommand;
}
public class CommandDispatcher : ICommandDispatcher
{
// I DO NOT WANT TO DO THIS:
IWindsorContainer _container;
public CommandDispatcher(IWindsorContainer container)
{
_container = container;
}
public ICommandResult Submit<TCommand>(TCommand command) where TCommand : Commands.ICommand
{
// I DO NOT WANT TO DO THIS TOO:
var handler = _container.Resolve<ICommandHandler<TCommand>>();
if (handler == null)
{
throw new Exception("Command handler not found for command " + typeof(TCommand).ToString());
}
return handler.Execute(command);
}
}
Basically all I want is to configure the container in a way that my WebAPI controller can have a dependency on ICommandDispatcher
and simply do something like
var result = this.commandDispatcher.Submit(new CreateUserCommand("John Smith"));
if (result.Success){
return Ok();
}
Thanks! ;)