Resolving named dependencies with Unity

前端 未结 2 1948
醉酒成梦
醉酒成梦 2021-01-12 08:49

I have a service with 2 dependencies: a Repository and a Gateway (sms).

I need to resolve 2 different versions of the service that only differ on one of the paramete

相关标签:
2条回答
  • 2021-01-12 09:07

    Still have no idea why your version doesn't work but this DOES work (as I've expected):

            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" ) ) ) );
    

    Note that I am using InjectionFactory instead of InjectionConstructor.

    Yet another version that works. This time I keep your way of registering services but I make them named and resolve by name:

            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();
    
    0 讨论(0)
  • 2021-01-12 09:12

    it can also be done using ParameterOverride as below

            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
    
    0 讨论(0)
提交回复
热议问题