开发者

Autofac - resolving component with parameters dynamically

开发者 https://www.devze.com 2023-01-10 18:34 出处:网络
I have a class that takes an interface a开发者_运维技巧s a constructor argument. There are two implementations of this interface and I want to decide what implementation to use at runtime based on a v

I have a class that takes an interface a开发者_运维技巧s 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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号