Skip to content

Instantly share code, notes, and snippets.

@ritasker
Created September 29, 2016 12:52
Show Gist options
  • Save ritasker/0a2f5a3b44248641afba1d2905fe41c2 to your computer and use it in GitHub Desktop.
Save ritasker/0a2f5a3b44248641afba1d2905fe41c2 to your computer and use it in GitHub Desktop.
CommandHandler classes
public abstract class CommandHandler<TCommand, TResult> where TCommand : ICommand<TResult>
{
public abstract TResult Handle(TCommand command);
}
public class SomeCommandeHandler : CommandHandler<SomeCommand, Guid>
{
public override Guid Handle(SomeCommand command)
{
// Do stuff here
return Guid.NewGuid();
}
}
public class SimpleInjectorCommandHandlerResolver : ICommandHandlerResolver
{
private readonly Container _container;
public SimpleInjectorCommandHandlerResolver(Container container)
{
_container = container;
}
public CommandHandler<ICommand<TResult>, TResult> ResolveForCommand<TResult>(ICommand<TResult> command)
{
var handlerType = typeof(CommandHandler<,>).MakeGenericType(command.GetType(), typeof(TResult));
return (CommandHandler<ICommand<TResult>, TResult>) _container.GetInstance(handlerType);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment