I'm trying to implement a simple CQRS-application example.
This is a structure of my "Command" part:
public interface ICommand
{
}
//base interface for command handlers
interface ICommandHandler<in TCommand> where TCommand: ICommand
{
void Execute(TCommand command);
}
// example of the command
public class SimpleCommand: ICommand
{
//some properties
}
// example of the SimpleCommand command handler
public class SimpleCommandHandler: ICommandHandler<SimpleCommand>
{
public void Execute(SimpleCommand command)
{
//some logic
}
}
This is interface ICommandDipatcher
. It dispatches a command to its handler.
public interface ICommandDispatcher
{
void Dispatch<TCommand>(TCommand command) where TCommand : ICommand;
}
This is a default implementation of ICommandDispatcher
and the main problem is to obtain the necessary command handler by the type of the command via Autofac
.
public class DefaultCommandDispatcher : ICommandDispatcher
{
public void Dispatch<TCommand>(TCommand command) where TCommand : ICommand
{
//How to resolve/get object of the neseccary command handler
//by the type of command (TCommand)
handler.Execute(command);
}
}
What is the best way to resolve implementation of ICommandHanler
by type of the command in that case via Autofac?
Thanks!