RegisterOpenGeneric与SimpleInjector解决了错误的类型

时间:2013-10-18 20:13:01

标签: c# generics dependency-injection inversion-of-control simple-injector

我们将从一个独立的故事开始,只是为了让您了解原因: 我想处理任何针对同一接口更改数据的操作:ICommand 存在一些名为ICommandHandlers的东西可以处理我想要的任何命令。 所以,如果我想要一个CreatePersonCommand,我需要一个CreatePersonCommandHandler。

所以这是控制台应用程序的主体,它演示了这一点:(需要Simple Injector

// 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
    }
}

因此,无论出于何种原因,SimpleInjector总是尝试解析我所拥有的DeleteCommandHandler<>的{​​{1}}。 同样,订单无关紧要。 我有其他的,封闭式的命令处理程序(及其各自的命令),只是继承了CreateBaseCommand<>,工作正常。

我花了很多时间从this处完成所有可能的注册类型。

1 个答案:

答案 0 :(得分:4)

<强>更新

这绝对是当前版本中的一个错误。这不知何故滑过了测试裂缝的单元。代码错过了一个检查,用于验证构建的封闭通用实现是否实际实现了请求的封闭通用服务类型。如果所有泛型类型约束都有效,则框架会认为分辨率成功,这在您的情况下是不正确的。

修复相当容易,即将推出的v2.4肯定能解决这个问题,但与此同时,你必须采取以下解决方法。

更新2:

实际上非常讨厌,在某些情况下可能很难解决。除 RegisterOpenGeneric 外,装饰者注册也会受到影响。这使我得出结论,这必须快速修复,不能等到下一次次要发布。因此,我将Version 2.3.6推送到NuGet和CodePlex。 v2.3.6解决了这个问题。

解决方法:

解决方法是阻止提供嵌套到其他类型(例如DeleteBaseCommand<TModel>)的泛型类型参数。相反,您可以回退使用泛型类型约束,如以下示例所示:

public class CreateCommandBaseHandler<TCommand, TModel> 
    : ICommandHandler<TCommand, TModel> // no nested generic arguments here
    where TCommand : CreateBaseCommand<TModel> // but type constraint here.
{
    public TModel Handle(TCommand command)
    {
        // create the thing
        return default(TModel);
    }
}

public class DeleteCommandBaseHandler<TCommand, TModel> 
    : ICommandHandler<TCommand, TModel>
    where TCommand : DeleteBaseCommand<TModel>
{
    public TModel Handle(TCommand command)
    {
        // delete the thing
        return default(TModel);
    }
}