得到统一解决同一类型的多个实例 [英] Getting unity to resolve multiple instances of the same type

查看:138
本文介绍了得到统一解决同一类型的多个实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的多类型注册的一个简单的解决(最终构造注入,但使用.Resolve,看是否团结,甚至能够这样的事情。



在每一种情况下下面,统一解决空的,它应该被解决2。



是否有统一的一些开关,在2007年后的行为把?还是我只是彻底失踪?什么



下面是我的代码:

 公共接口的IFoo { } 
公共类Foo1:的IFoo {}
公共类foo2的:的IFoo {}

类节目
{
静态无效的主要(字串[] args )
{
变种集装箱=新UnityContainer();
container.RegisterType<的IFoo,Foo1>();
container.RegisterType<的IFoo和Foo2>();

// container.Resolve< IEnumerable的<的IFoo>>();返回0
// container.ResolveAll<的IFoo>();返回0

变种FOOS = container.Resolve&下;的IFoo []≥();
Console.WriteLine(foos.Count());

到Console.ReadLine();

}
}


解决方案

在统一只能有一个默认的注册(没有名字的注册在 container.RegisterType<的IFoo,Foo1>();
)。如果多个默认登记执行,最后一个胜



为了注册多个实现相同的接口,你需要指定名称的注册:

  container.RegisterType<的IFoo,Foo1>(registration1); 
container.RegisterType<的IFoo和Foo2>(registration2);



此外,统一才明白默认阵列。如果你想解决一个数组,那么你将被罚款与缺省行为。否则,你将需要注册阵列和你有兴趣,喜欢收集之间的映射:

  container.RegisterType< IEnumerable的< ;的IFoo>中的IFoo []≥(); 



另外要注意的是,解决集合时默认的注册将不予退还。
为例给出:

  container.RegisterType<的IFoo,Foo1>(); 
container.RegisterType<的IFoo和Foo2>(registration1);
container.RegisterType<的IFoo,Foo3>(registration2);
container.RegisterType&所述; IEnumerable的&下; IFoo的>中的IFoo []≥();

如果您解决的IEnumerable<的IFoo> ,其结果将只包含 foo2的 Foo3 ,但不会有实例 Foo1 ,因为未包括默认的注册。


I want to do a simple resolve of multiple type registrations (ultimately constructor injected, but using .Resolve to see if Unity is even capable of such things.

In every case below, Unity resolves 0 items where it should be resolving 2.

Is there some switch in unity that turns on post-2007 behavior? Or am I just drastically missing something?

Here is my code:

public interface IFoo {}
public class Foo1 : IFoo{}
public class Foo2 : IFoo{}

class Program
{
    static void Main(string[] args)
    {
        var container = new UnityContainer();
        container.RegisterType<IFoo, Foo1>();
        container.RegisterType<IFoo, Foo2>();

        // container.Resolve<IEnumerable<IFoo>>();   returns 0
        // container.ResolveAll<IFoo>(); returns 0

        var foos = container.Resolve<IFoo[]>();
        Console.WriteLine(foos.Count());

        Console.ReadLine();

    }
}

解决方案

In Unity there can only be one default registration (A registration without a name as in container.RegisterType<IFoo, Foo1>(); ). If multiple default registrations are performed, the last one wins.

In order to register multiple implementation for the same interface, you need to assign names to those registrations:

container.RegisterType<IFoo, Foo1>("registration1");
container.RegisterType<IFoo, Foo2>("registration2");

Also, Unity only understand arrays by default. If you want to resolve as an array then you will be fine with the default behaviour. Otherwise you will need to register a mapping between the array and the collection you are interested in, like:

container.RegisterType<IEnumerable<IFoo>, IFoo[]>();

Another important note is that the default registration won't be returned when resolving a collection. For example given:

container.RegisterType<IFoo, Foo1>();
container.RegisterType<IFoo, Foo2>("registration1");
container.RegisterType<IFoo, Foo3>("registration2");
container.RegisterType<IEnumerable<IFoo>, IFoo[]>();

If you resolve IEnumerable<IFoo>, the result will only contain instances of Foo2 and Foo3, but there will not be an instance of Foo1 because the default registration is not included.

这篇关于得到统一解决同一类型的多个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆