统一。在InjectionFactory中返回泛型类型

时间:2016-02-25 17:34:50

标签: c# unity-container

在WebAPI项目中,我有两个相同通用接口的不同实现:

public interface Interface<T> {}

public class Class1<T> : Interface<T> {} where T : class
public class Class2<T> : Interface<T> {} where T : class

我需要根据请求中的某些参数决定运行时,使用哪种实现。所以我确认了这样的统一容器:

....
container.RegisterType(typeof(Interface<>),
            new InjectionFactory(
                c =>
                    ToggleInjectionFactory.Get(container,     
                                               typeof(Class1<>),
                                               typeof(Class2<>),
                                               HttpContext.Current.Request)
            ));
...

// Factory
public class ToggleInjectionFactory
{
...
    public static object Get(IUnityContainer container, Type typeTo1, Type typeTo2, HttpRequest request)
    {
        if (Toggle.IsOn(request))
        {
            return container.Resolve(typeTo1);
        }
        return container.Resolve(typeTo2);
    }
}

但由于typeTo1typeTo2包含通用参数,因此出现错误。错误讯息:

Resolution of the dependency failed, type = "Class1`1[T]", name = "(none)".
Exception occurred while: while resolving.
Exception is: ArgumentException - Type Class1`1[T] contains generic parameters

有没有办法实现container.Resolve(typeTo1)所以我可以返回一个封闭的类型实例?我不想为我拥有的每个有形类型<T>重写注册语句。

3 个答案:

答案 0 :(得分:2)

可以使用一些方法来获得您想要的结果,但是随着逻辑变得越来越复杂并且类的数量增加,更简单的方法会因为不能很好地扩展。我在这里想到按公约注册,有条件注册和命名注册。

Unity允许您注册开放式泛型并让它们自动解析为封闭式泛型。但是,使用InjectionFactory的发布方法不起作用,因为InjectionFactory没有Unity用来创建封闭泛型类型的解析BuildContext

实现您想要的一个解决方案是使用Unity容器扩展,因为Container扩展将具有可用的构建上下文,并允许您确定要构建的正确类型。在这种情况下,可以在对象解析的TypeMapping阶段完成简单类型映射。

public class ToggleExtension : UnityContainerExtension
{
    private Toggle toggle;

    public ToggleExtension(Toggle toggle)
    {
        this.toggle = toggle;
    }

    protected override void Initialize()
    {
        Context.Strategies.Add(new ToggleBuildUpStrategy(this.toggle),
            UnityBuildStage.TypeMapping);
    }
}

public class ToggleBuildUpStrategy : BuilderStrategy
{
    private Toggle toggle;
    public ToggleBuildUpStrategy(Toggle toggle)
    {
        this.toggle = toggle;
    }

    public override void PreBuildUp(IBuilderContext context)
    {
        // If we have an Interface<> then do some type mapping to the applicable concrete class
        // Note that I'm using Toggle here because something similar was used in the question
        if (context.BuildKey.Type.IsGenericType && 
            context.BuildKey.Type.GetGenericTypeDefinition() == typeof(Interface<>))
        {
            Type target = null;

            // Luckily HttpContext.Current.Request request context is available here
            // For other non-static contexts might have to work out how to inject into the extension
            if (this.toggle.IsOn(HttpContext.Current.Request))
            {
                target = typeof(Class1<>);
            }
            else
            {
                target = typeof(Class2<>);
            }
            // Get generic args
            Type[] argTypes = context.BuildKey.Type.GetGenericArguments();

            // Replace build key type Interface<> => Class1<> or Class2<>
            // So that the correct type is resolved
            context.BuildKey = new NamedTypeBuildKey(target.MakeGenericType(argTypes), 
                context.BuildKey.Name);
        }
    }
}

这是我使用的Toggle实现 - 它与问题中的不完全相同,但非静态使测试更容易:

public class Toggle
{
    private bool isToggleOn;

    public void SetToggleOn()
    {
        isToggleOn = true;
    }

    public void SetToggleOff()
    {
        isToggleOn = false;
    }

    public bool IsOn(HttpRequest request)
    {
        // Implement more complicated toggle logic
        return isToggleOn;
    }
}

最后进行一些测试以确保正确的类型得到解决:

IUnityContainer container = new UnityContainer();
Toggle toggle = new Toggle();
toggle.SetToggleOn();

container.AddExtension(new ToggleExtension(toggle));

Interface<X> x = container.Resolve<Interface<X>>();
Debug.Assert(x.GetType() == typeof(Class1<X>));

toggle.SetToggleOff();

x = container.Resolve<Interface<X>>();
Debug.Assert(x.GetType() == typeof(Class2<X>));

答案 1 :(得分:0)

好像你已经使解决方案复杂化了。相反,更简单的方法是不将其推入IoC容器。原因是DI要保持逻辑独立于依赖关系及其逻辑。

在这种情况下,您有一个确定依赖堆栈的开关,然后您只需将其推入解决方案中,从而在容器和实际逻辑之间创建依赖关系。这可能会导致问题,因为它只是推动解决问题,而不是实际解决问题。在这种情况下,确定开关的逻辑很可能适合于具有对象分辨率(这是逻辑最有可能的......什么是“切换”) 似乎如果你有一个逻辑部分的开关,它应该是确定解决的第一步的所有者。当然,您仍然可以使用一些IoC作为具体类分辨率的最终接口,但不要将开关逻辑推送到IoC。

换句话说,您是否可以使用IoC解析的另一层接口,然后调用解析器使用基本接口的类型开关?

IBaseInterface resolvedObject = Toggle.IsOn(request) ? di.Get<IToggledInterface>() : di.Get<INotToggledInterface>();

注意:正在解析的接口使用IBaseInterface进行最终曝光。他们都捏造了界面,但是除了允许IoC解析的“类型”定义之外,它们本身可能不会对签名添加任何内容。

答案 2 :(得分:0)

我从 Randy 的回答中获得了很大的启发,但我不同意您必须使用 Unity 扩展程序跳过箍。如果您小心一点,InjectionFactory 可以正常工作。

下面的代码只是将任何开放类型 I<> 解析为相应的类型 C<>
它不执行切换部分,但这只是一个 if 语句。

对于不耐烦的人:runnable fiddle

用法:

interface I<T> { }

class C<T> : I<T> { }

...

var ioc = new UnityContainer();
ioc.RegisterType(
    typeof(I<>), //can't use ioc.RegisterType<I<>>(...), C# doesn't support that
    // generic type mapping defined here
    // the first type parameter must match what's above, in this case typeof(I<>)
    new OpenGenericTypeFactory(typeof(I<>), typeof(C<>)) 
);

var c = ioc.Resolve<I<double>>();
Console.WriteLine(c.GetType());

打印:

C`1[System.Double]

工厂的实现:

class OpenGenericTypeFactory : Microsoft.Practices.Unity.InjectionFactory {
    public OpenGenericTypeFactory(Type from, Type to)
        : base((container, type, _) => Create(container, from, to, type))
    {
    }
    
    private static object Create(IUnityContainer container, Type fromType, Type toType, Type requestedType)
    {
        if (!requestedType.IsGenericType || requestedType.GetGenericTypeDefinition() != fromType)
        {
            throw new Exception($"Injection factory for {fromType}: got type {requestedType} instead.");
        }

        Type[] argTypes = requestedType.GetGenericArguments();

        var closedGenericTarget = toType.MakeGenericType(argTypes);
        try
        {
            return container.Resolve(closedGenericTarget);
        }
        catch
        {
            throw new Exception($"Failed to resolve type {closedGenericTarget} for requested type {requestedType}");
        }
    }
}