7

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 parameters passed to the gateway.

The code is simplified to as follows

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;
    }
}

Failing test

[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

The wrong gateway is being passed in when the service is resolved, however if I resolve the gateway explicitly by name, it comes out correct. I think I'm missing something fundamental in how ResolvedParameter(string name) is working but I assumed that it looks for a type in the container with that name.

drch
  • 3,040
  • 1
  • 18
  • 29
  • A workaround could be to use InjectionFactory instead of InjectionConstructor so that you have an explicit code instead of ResolvedParameter. This should work but I can't answer why this doesn't work with your version. – Wiktor Zychla Apr 23 '13 at 19:07
  • @WiktorZychla I did try an injectionfactory but had similar results where in that context it resolved the wrong gateway even though I resolved it by name. I'll try again on the morning with this simplified example and update the test case with the results. – drch Apr 23 '13 at 19:20
  • Will try that tomorrow, too. It's 10pm here, so I will report in the morning. – Wiktor Zychla Apr 23 '13 at 19:47

2 Answers2

7

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();
Wiktor Zychla
  • 47,367
  • 6
  • 74
  • 106
  • Thanks - I had tried the injectionfactory earlier with the same results but I must have done something incorrect. – drch Apr 24 '13 at 08:23
  • *sigh* although this solved it in the test from the question, when I moved it over to my test against the actual (which was simplified to this exact setup using fakes) the InjectionFactory approach behaved the same as my original problem. I think I'm stuck with using names in .Resolve() as you pointed out as a second solution. Cheers. – drch Apr 24 '13 at 08:53
  • If I were you, I would open an issue on their codeplex site. Chances are this is an unexpected behavior and it could possibly be fixed somehow. Just looking at your code anyone would probably expect it to work while it doesn't. On the other hand, if they reject the submission, at least they will probably somehow explain what's wrong. – Wiktor Zychla Apr 24 '13 at 12:57
1

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
Hemendr
  • 673
  • 6
  • 12