统一登记一个接口上的多个对象,并告诉在哪里团结注入他们 [英] Unity Register For One Interface Multiple Object and Tell Unity Where to Inject them

查看:93
本文介绍了统一登记一个接口上的多个对象,并告诉在哪里团结注入他们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我一直有麻烦试图告诉团结,对于一个接口,如果它有多个实现,我希望它在不同的classes.Here注入它们就是我的意思是:



比方说,我有一个接口 IProductCatalogService 和两个实现
ProductCatalog:IProductCatalogService ProductCatalogService:IProductCatalogService



我怎么会去告诉团结,对于类。我想在我的传入构造类型的实例 ProductCatalog 和类 B 我想 ProductCatalogService 的一个实例



我使用统一在一个ASP.NET Web API项目,我已经设置了解析器的 GLobalConfiguration



对于简单的1对1的注册一切正常。



下面是我试过,但它不似乎工作:



<预类=郎-CS prettyprint-覆盖> 公共类DependencyServiceModel
{
公共类型从{搞定;组; }
公共类型为{搞定;组; }
公开的IEnumerable<类型> ForClasses {搞定;组; }
}

公共无效RegisterTypeForSpecificClasses(DependencyServiceModel dependencyService)
{
的foreach(在dependencyService.ForClasses VAR forClass)
{
串uniquename = Guid.NewGuid()的ToString()。

Container.RegisterType(dependencyService.From,
dependencyService.To,uniquename);

Container.RegisterType(forClass,uniquename,
新InjectionConstructor(
新ResolvedParameter(dependencyService.To)));
}
}

DependencyServiceModel 是接口,是对象巫婆我想实例化和 ForClasses 是我想使用对象类型。


解决方案

在下面你的例子中有一个接口实现两次注射点播分成两个不同的客户端类,就像你提出要求。诀窍是使用命名注册。

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

container.RegisterType&所述;客户端1>(新InjectionConstructor(新ResolvedParameter&所述;的IFoo>(Foo1)));
container.RegisterType&所述;客户机2>(新InjectionConstructor(新ResolvedParameter&所述;的IFoo>(foo2的)));

客户端1客户端1 = container.Resolve<客户端1>();
客户端2客户端2 = container.Resolve<客户机2>();
}
}

公共接口IFoo的
{

}

公共类Foo1:IFoo的
{

}

公共类foo2的:IFoo的
{

}

公共类客户端1
{
公共客户端1(的IFoo富)
{

}
}

公共类客户机2
{
公共客户机2(的IFoo富)
{

}
}

这是最有可能是你做错了:

  Container.RegisterType(forClass,uniquename,
新InjectionConstructor(
新ResolvedParameter(dependencyService.To)));

您为您的具体类命名的注册。相反,你应该有



  Container.RegisterType(forClass,空,
新InjectionConstructor(
新ResolvedParameter( dependencyService.To,uniquename)));


Hi I have been having trouble trying to tell Unity that for an Interface if it has multiple implementations , I want it to inject them in different classes.Here is what I mean:

Let's say I have an interface IProductCatalogService and two implementations ProductCatalog : IProductCatalogService and ProductCatalogService : IProductCatalogService.

How would I go about telling Unity that for Class A I want in my constructor passed an instance of type ProductCatalog and for Class B I want an instance of ProductCatalogService.

I am using Unity in an ASP.NET Web API project and I have set the resolver in the GLobalConfiguration.

For simple 1 to 1 registrations everything works.

Here is what I have tried but it does not seem to work:

public class DependencyServiceModel
{
    public Type From { get; set; }
    public Type To { get; set; }
    public IEnumerable<Type> ForClasses { get; set; }
}

public void RegisterTypeForSpecificClasses(DependencyServiceModel dependencyService)
{
    foreach (var forClass in dependencyService.ForClasses)
    {
        string uniquename = Guid.NewGuid().ToString();

        Container.RegisterType(dependencyService.From, 
            dependencyService.To, uniquename);

        Container.RegisterType(forClass, uniquename, 
            new InjectionConstructor(
                new ResolvedParameter(dependencyService.To)));
    }
}

In the DependencyServiceModel, From is the interface, To is the object to witch I want to instantiate and ForClasses are the Type for which I want to use the To Object.

解决方案

In the example below you have an interface implemented twice and injected on demand into two different client classes, just as you request. The trick is to use named registrations.

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

        container.RegisterType<Client1>(new InjectionConstructor(new ResolvedParameter<IFoo>("Foo1")));
        container.RegisterType<Client2>(new InjectionConstructor(new ResolvedParameter<IFoo>("Foo2")));

        Client1 client1 = container.Resolve<Client1>();
        Client2 client2 = container.Resolve<Client2>();
    }
}

public interface IFoo
{

}

public class Foo1 :IFoo
{

}

public class Foo2 : IFoo
{

}

public class Client1
{
    public Client1(IFoo foo)
    {

    }
}

public class Client2
{
    public Client2(IFoo foo)
    {

    }
}

This is most probably what you do wrong:

 Container.RegisterType(forClass, uniquename, 
        new InjectionConstructor(
            new ResolvedParameter(dependencyService.To)));

You create a named registration for your concrete class. Instead you should have

 Container.RegisterType(forClass, null, 
        new InjectionConstructor(
            new ResolvedParameter(dependencyService.To, uniquename)));

这篇关于统一登记一个接口上的多个对象,并告诉在哪里团结注入他们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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