我们将从一个独立的故事开始,以便您理解原因: 我想处理针对同一接口更改数据的任何操作:ICommand 存在一些称为 ICommandHandlers 的东西,它们可以处理我想要的任何命令。所以,如果我想要一个 CreatePersonCommand,我需要一个 CreatePersonCommandHandler。
所以这里是一个控制台应用程序的主体,它演示了这一点:(需要简单的注入器)
// The e.g. CreatePersonCommand, with TResult being Person, as an example.
public interface ICommand<TResult>
{
}
//This handles the command, so CreatePersonCommandHandler
public interface ICommandHandler<in TCommand, out TResult>
where TCommand : ICommand<TResult>
{
TResult Handle(TCommand command);
}
// Imagine a generic CRUD set of operations here where we pass
// in an instance of what we need made
public class CreateBaseCommand<TModel> : ICommand<TModel>
{
public TModel ItemToCreate { get; set; }
}
public class DeleteBaseCommand<TModel> : ICommand<TModel>
{
public TModel ItemToDelete { get; set; }
}
public class CreateCommandBaseHandler<TModel>
: ICommandHandler<CreateBaseCommand<TModel>, TModel>
{
public TModel Handle(CreateBaseCommand<TModel> command)
{
// create the thing
return default (TModel);
}
}
public class DeleteCommandBaseHandler<TModel>
: ICommandHandler<DeleteBaseCommand<TModel>, TModel>
{
public TModel Handle(DeleteBaseCommand<TModel> command)
{
// delete the thing
return default(TModel);
}
}
public class Program
{
private static Container container;
static void Main(string[] args)
{
container = new Container();
// Order does not seem to matter, I've tried both ways.
container.RegisterOpenGeneric(typeof(ICommandHandler<,>),
typeof(DeleteCommandBaseHandler<>));
container.RegisterOpenGeneric(typeof(ICommandHandler<,>),
typeof(CreateCommandBaseHandler<>));
container.Verify();
// So I want to make the usual hello world
var commandToProcess = new CreateBaseCommand<string> { ItemToCreate = "hello world"};
// Send it away!
Send(commandToProcess);
}
private static void Send<TResult>(ICommand<TResult> commandToProcess)
{
//{CreateBaseCommand`1[[System.String,..."}
var command = commandToProcess.GetType();
//{Name = "String" FullName = "System.String"}
var resultType = typeof (TResult);
//"ICommandHandler`2[[CreateBaseCommand`1[[System.String,..."}
// so it's the right type here
var type = typeof(ICommandHandler<,>).MakeGenericType(command, resultType);
// This is where we break!
var instance = container.GetInstance(type);
// The supplied type DeleteCommandBaseHandler<String> does not implement
// ICommandHandler<CreateBaseCommand<String>, String>.
// Parameter name: implementationType
}
}
DeleteCommandHandler<>
因此,无论出于何种原因,SimpleInjector 总是试图解决CreateBaseCommand<>
我所拥有的问题。同样,顺序无关紧要。我有其他封闭类型的命令处理程序(及其各自的命令),它们只是继承ICommandHandler<,>
了哪些工作正常。
我花了很多时间来完成我可以从中找到的所有可能的注册类型。