Unity容器,如何基于“调用”类注入特定的类

时间:2016-11-10 20:53:18

标签: c# dependency-injection asp.net-web-api2 unity-container

我有以下代码段

var element = $("[class^='option-'] ").filter(funtion(){
    return $(this).attr("class").match(new RegExp("option-\d*-" + id)) ;
})[0];

这是WebApi2项目。 所以我有3个控制器,

 public static void RegisterComponents(IUnityContainer container)
    {
        // register all your components with the container here
        // it is NOT necessary to register your controllers
        container.RegisterType<ISocialClient, ClientA>("a");
        container.RegisterType<ISocialClient, ClientB>("b");
        container.RegisterType<ISocialClient, ClientC>("c");

        GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);
    }

在Unity中是否有办法配置,为SocialAController获取“ClientA”实现,为SocialBController获取“ClientB”实现?

1 个答案:

答案 0 :(得分:0)

mspasiuk,

您可以通过以这种方式注册例如SocialAController来完成此操作:

    container.RegisterType<SocialAController>(new InjectionConstructor(new ResolvedParameter<ISocialClient>("a")));