tags:

views:

68

answers:

1

I have a class that takes an interface as a constructor argument. There are two implementations of this interface and I want to decide what implementation to use at runtime based on a variable.

The problem is that the class above is deep in an object heirarchy that is resolved by Autofac and so I can't pass in the argument.

Somehing like below is what I am trying to achieve.

public interface IInterface1 {}
public interface IInterface2 {}

public class Class1 : IInterface2
{
    public Class1(IInterface1 interface1)
    {
    }
}

public class Class2
{
    public Class2(IInterface2 interface2)
    {
    }
}

public class Class3
{
    public void GetClass2Instance(string interface1ImplementationToChoose)
    {
        // want to change which implementation of IInterface1 is resolved based on the interface1ImplementationToChoose variable
        var class2 = container.Resolve<Class2>();
    }
}

Any ideas?

UPDATE:

To clarify, this is an existing object hierarchy that is used by existing applications that work fine. Also, the object model is much larger than the one shown in this example. As a result I don't really want to have to pass down a factory to each constructor in the object graph to be used by a class deep in that graph.

Is there a way of getting a different implementation of IInterface1 passed into Class1 without Class2 knowing anything about it?

Thanks

+3  A: 

Yes, inject a factory that hides how the types are chosen:

public class Class3
{
   private Func<string, Class2> _class2Factory;
   public Class3(Func<string, Class2> class2Factory)
   {
        _class2Factory = class2Factory;
   }

   public void GetClass2Instance(string interface1ImplementationToChoose)
   {
       var class2 = _class2Factory(interface1ImplementationToChoose);
   }
}

And then the container setup, something along these lines:

builder.RegisterType<Implementation1>().Named("imp1").As<IInterface1>();
builder.RegisterType<Implementation2>().Named("imp2").As<IInterface1>();
builder.Register<Func<string, Class2>>(c => 
    {
        var context = c.Resolve<IComponentContext>();
        return imp => new Class2(context.Resolve<IInterface1>(imp));
    });
builder.RegisterType<Class3>();

You can now use Class3 like this:

public class Class4
{
     public Class4(Class3 class3)
     {
         var class2with1 = class3.GetClass2Instance("imp1");
         var class2with2 = class3.GetClass2Instance("imp2");
     }
}

NOTE: I have assumed that you meant that Class2 should be injected with varying implementations of the same interface IInterface1. Your sample is a bit confusing since you are showing two classes that implements different interfaces.

Peter Lillevold
Looks good; also, c => new Class2(context.Resolve<IInterface1>(imp)) is enough (you don't need to resolve IComponentContext explicitly unless you're going to keep a reference around.)
Nicholas Blumhardt
I'm not only newing `Class2`, in this case I'm registering a `Func<string, Class2>` delegate, which will have to carry its own context. Right?
Peter Lillevold
@Nicholas - I forgot that I should specify the delegate type for Register call, perhaps this makes more sense now. I'm still under the impression that I would have to resolve a new context to "store" in the lambda...
Peter Lillevold
Ah I see now, thanks.
Nicholas Blumhardt