使用Unity解析命名依赖项

时间:2013-04-23 16:35:33

标签: c# inversion-of-control unity-container

我有一个包含2个依赖项的服务:存储库和网关(短信)。

我需要解析2个不同版本的服务,这些版本只对传递给网关的其中一个参数有所不同。

代码简化如下

public interface IService
{
    string DoSomething();
}

public interface IServiceFoo
{
    string DoSomething();
}

public interface IServiceBar
{
    string DoSomething();
}

public interface IRepository { }
public class Repository : IRepository { }

public interface IGateway
{
    string Name { get; set; }
}

public class Gateway : IGateway
{
    public string Name { get; set; }
    public Gateway(string name)
    {
        this.Name = name;
    }
}

public class Service : IService, IServiceFoo, IServiceBar
{
    private readonly IGateway _gateway;
    private readonly IRepository _repo;
    public Service(IRepository repo, IGateway gateway)
    {
        _gateway = gateway;
        _repo = repo;
    }

    public string DoSomething()
    {
        return _gateway.Name;
    }
}

测试失败

[TestClass]
public class UnityTest
{
    [TestMethod]
    public void TestMethod1()
    {
        var container = new UnityContainer();
        container
            .RegisterType<IRepository, Repository>()
            .RegisterType<IGateway, Gateway>("FooGateway", new InjectionConstructor("I am foo"))
            .RegisterType<IGateway, Gateway>("BarGateway", new InjectionConstructor("I am bar"))
            .RegisterType<IServiceFoo, Service>(new InjectionConstructor(new ResolvedParameter<IRepository>(), new ResolvedParameter<IGateway>("FooGateway")))
            .RegisterType<IServiceBar, Service>(new InjectionConstructor(new ResolvedParameter<IRepository>(), new ResolvedParameter<IGateway>("BarGateway")));

        var barGateway = container.Resolve<IGateway>("BarGateway");
        var fooGateway = container.Resolve<IGateway>("FooGateway");

        var serviceBar = container.Resolve<IServiceBar>();
        var serviceBarGatewayName = serviceBar.DoSomething();

        var serviceFoo = container.Resolve<IServiceFoo>();
        var serviceFooGatewayName = serviceFoo.DoSomething();

        Assert.AreEqual("I am bar", barGateway.Name); // pass
        Assert.AreEqual("I am foo", fooGateway.Name); // pass


        Assert.AreEqual("I am bar", serviceBarGatewayName); // pass
        Assert.AreEqual("I am foo", serviceFooGatewayName); // FAIL

解析服务时传入错误的网关,但是如果我通过名称明确解析网关,则说明正确。我认为我在ResolvedParameter(字符串名称)的工作方式上缺少一些基本的东西,但我认为它在容器中查找具有该名称的类型。

2 个答案:

答案 0 :(得分:7)

仍然不知道为什么你的版本不能正常工作但是这个工作(正如我所料):

        var container = new UnityContainer();
        container
            .RegisterType<IRepository, Repository>()
            .RegisterType<IGateway, Gateway>( "FooGateway", new InjectionConstructor( "I am foo" ) )
            .RegisterType<IGateway, Gateway>( "BarGateway", new InjectionConstructor( "I am bar" ) )
            //.RegisterType<IServiceFoo, Service>( new InjectionConstructor( new ResolvedParameter<IRepository>(), new ResolvedParameter<IGateway>( "FooGateway" ) ) )
            //.RegisterType<IServiceBar, Service>( new InjectionConstructor( new ResolvedParameter<IRepository>(), new ResolvedParameter<IGateway>( "BarGateway" ) ) );
            .RegisterType<IServiceFoo>( new InjectionFactory( c => new Service( c.Resolve<IRepository>(), c.Resolve<IGateway>( "FooGateway" ) ) ) )
            .RegisterType<IServiceBar>( new InjectionFactory( c => new Service( c.Resolve<IRepository>(), c.Resolve<IGateway>( "BarGateway" ) ) ) );

请注意,我使用InjectionFactory代替InjectionConstructor

又一个有效的版本。这次我保留了注册服务的方式,但我将它们命名并按名称解析:

        var container = new UnityContainer();
        container
            .RegisterType<IRepository, Repository>()
            .RegisterType<IGateway, Gateway>( "FooGateway", new InjectionConstructor( "I am foo" ) )
            .RegisterType<IGateway, Gateway>( "BarGateway", new InjectionConstructor( "I am bar" ) )
            .RegisterType<IServiceFoo, Service>( "sf", new InjectionConstructor( new ResolvedParameter<IRepository>(), new ResolvedParameter<IGateway>( "FooGateway" ) ) )
            .RegisterType<IServiceBar, Service>( "sb", new InjectionConstructor( new ResolvedParameter<IRepository>(), new ResolvedParameter<IGateway>( "BarGateway" ) ) );
            //.RegisterType<IServiceFoo>( new InjectionFactory( c => new Service( c.Resolve<IRepository>(), c.Resolve<IGateway>( "FooGateway" ) ) ) )
            //.RegisterType<IServiceBar>( new InjectionFactory( c => new Service( c.Resolve<IRepository>(), c.Resolve<IGateway>( "BarGateway" ) ) ) );


        var barGateway = container.Resolve<IGateway>( "BarGateway" );
        var fooGateway = container.Resolve<IGateway>( "FooGateway" );

        var serviceBar = container.Resolve<IServiceBar>( "sb" );
        var serviceBarGatewayName = serviceBar.DoSomething();

        var serviceFoo = container.Resolve<IServiceFoo>( "sf" );
        var serviceFooGatewayName = serviceFoo.DoSomething();

答案 1 :(得分:1)

也可以使用ParameterOverride完成,如下所示

        container
            .RegisterType<IRepository, Repository>()
            .RegisterType<IGateway, Gateway>("FooGateway", new InjectionConstructor("I am foo"))
            .RegisterType<IGateway, Gateway>("BarGateway", new InjectionConstructor("I am bar"))
            .RegisterType<IServiceFoo, Service>(new InjectionConstructor(new ResolvedParameter<IRepository>(), typeof(IGateway)))
            .RegisterType<IServiceBar, Service>(new InjectionConstructor(new ResolvedParameter<IRepository>(), typeof(IGateway)));

        var barGateway = container.Resolve<IGateway>("BarGateway");
        var fooGateway = container.Resolve<IGateway>("FooGateway");

        var serviceBar = container.Resolve<IServiceBar>(new ParameterOverride("gateway", barGateway));
        var serviceBarGatewayName = serviceBar.DoSomething();

        var serviceFoo = container.Resolve<IServiceBar>(new ParameterOverride("gateway", fooGateway));
        var serviceFooGatewayName = serviceFoo.DoSomething();

        Assert.AreEqual("I am bar", barGateway.Name); // pass
        Assert.AreEqual("I am foo", fooGateway.Name); // pass


        Assert.AreEqual("I am bar", serviceBarGatewayName); // pass
        Assert.AreEqual("I am foo", serviceFooGatewayName); // pass