Unity DI,多个命名实例

时间:2015-12-23 18:39:06

标签: c# dependency-injection unity-container

我有这个简化的问题构造。我有多个命名实例,正如您所看到的,每增加一个参数,IBusinessLogic的注册数量就会增加。是否有优雅的方法来实现以下目标,而无需为IBusinessLogic提供如此多的RegisterType。

class Program
{
    static void Main(string[] args)
    {
        IUnityContainer container = new UnityContainer();
        container.RegisterType<ISettings, Settings1>("settings1");
        container.RegisterType<ISettings, Settings2>("settings2");

        container.RegisterType<IRepository, Repository1>("repository1 settings1", new InjectionConstructor(new ResolvedParameter<ISettings>("settings1")));
        container.RegisterType<IRepository, Repository1>("repository1 settings2", new InjectionConstructor(new ResolvedParameter<ISettings>("settings2")));
        container.RegisterType<IRepository, Repository1>("repository2 settings1", new InjectionConstructor(new ResolvedParameter<ISettings>("settings1")));
        container.RegisterType<IRepository, Repository1>("repository2 settings2", new InjectionConstructor(new ResolvedParameter<ISettings>("settings2")));

        container.RegisterType<IBusinessLogic, BusinessLogic1>("logic1 settings1 repository1", new InjectionConstructor(new ResolvedParameter<ISettings>("settings1"), new ResolvedParameter<IRepository>("repository1 settings1")));
        container.RegisterType<IBusinessLogic, BusinessLogic1>("logic1 settings2 repository1", new InjectionConstructor(new ResolvedParameter<ISettings>("settings2"), new ResolvedParameter<IRepository>("repository1 settings1")));

        container.RegisterType<IBusinessLogic, BusinessLogic1>("logic1 settings1 repository2", new InjectionConstructor(new ResolvedParameter<ISettings>("settings1"), new ResolvedParameter<IRepository>("repository2 settings2")));
        container.RegisterType<IBusinessLogic, BusinessLogic1>("logic1 settings2 repository2", new InjectionConstructor(new ResolvedParameter<ISettings>("settings2"), new ResolvedParameter<IRepository>("repository2 settings2")));

        container.RegisterType<IBusinessLogic, BusinessLogic2>("logic2 settings1 repository1", new InjectionConstructor(new ResolvedParameter<ISettings>("settings1"), new ResolvedParameter<IRepository>("repository1 settings1")));
        container.RegisterType<IBusinessLogic, BusinessLogic2>("logic2 settings2 repository1", new InjectionConstructor(new ResolvedParameter<ISettings>("settings2"), new ResolvedParameter<IRepository>("repository1 settings1")));

        container.RegisterType<IBusinessLogic, BusinessLogic2>("logic2 settings1 repository2", new InjectionConstructor(new ResolvedParameter<ISettings>("settings1"), new ResolvedParameter<IRepository>("repository2 settings2")));
        container.RegisterType<IBusinessLogic, BusinessLogic2>("logic2 settings2 repository2", new InjectionConstructor(new ResolvedParameter<ISettings>("settings2"), new ResolvedParameter<IRepository>("repository2 settings2")));

        string inputParam1 = "logic1";
        string inputParam2 = "settings2";
        string inputParam3 = "repository1";

        var instance = container.Resolve<IBusinessLogic>(inputParam1 + " " + inputParam2 + " " + inputParam3);
    }
}

编辑:

我找到了替代方法,但我必须明确指定依赖关系图中的每个覆盖

class Program
{
    static void Main(string[] args)
    {
        IUnityContainer container = new UnityContainer();
        container.RegisterType<ISettings, Settings1>("settings1");
        container.RegisterType<ISettings, Settings2>("settings2");

        container.RegisterType<IRepository, Repository1>("repository1");
        container.RegisterType<IRepository, Repository2>("repository2");

        container.RegisterType<IBusinessLogic, BusinessLogic1>("logic1");
        container.RegisterType<IBusinessLogic, BusinessLogic2>("logic2");

        string inputParam1 = "logic1";
        string inputParam2 = "settings1";
        string inputParam3 = "repository1";

        var instance = container.Resolve<IBusinessLogic>(inputParam1, new DependencyOverrides()
            {
                {typeof(ISettings),container.Resolve<ISettings>(inputParam2)},
                {typeof(IRepository),container.Resolve<IRepository>(inputParam3, new DependencyOverrides() 
                    { 
                        {typeof(ISettings),container.Resolve<ISettings>(inputParam2)} 
                    })
                 },
            });
    }
}

我真正需要的是告诉容器 - 解析IBusinessLogic并在依赖关系图中的任何地方使用“settings1”命名实例,您需要ISettings实例。这可能吗?

1 个答案:

答案 0 :(得分:1)

以下是如何做到这一点:

IUnityContainer container = new UnityContainer();
container.RegisterType<ISettings, Settings1>("settings1");
container.RegisterType<ISettings, Settings2>("settings2");

container.RegisterType<IRepository, Repository1>("repository1");
container.RegisterType<IRepository, Repository2>("repository2");

container.RegisterType<IBusinessLogic, BusinessLogic1>("logic1");
container.RegisterType<IBusinessLogic, BusinessLogic2>("logic2");

string inputParam1 = "logic1";
string inputParam2 = "settings1";
string inputParam3 = "repository1";

var result = container.Resolve<IBusinessLogic>(inputParam1,
    new DependencyOverride<ISettings>(new ResolvedParameter<ISettings>(inputParam2)),
    new DependencyOverride<IRepository>(new ResolvedParameter<IRepository>(inputParam3)));