Autofac - 解决成分与参数动态 [英] Autofac - resolving component with parameters dynamically

查看:261
本文介绍了Autofac - 解决成分与参数动态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类,需要一个接口作为构造函数的参数。有此接口的两个实现和欲决定基于变量在运行时使用什么样的实施

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.

的问题是,上述类是一个对象深层次结构由Autofac解决,所以我不能在参数传递。

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像下面就是我想要的目的。

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?

更新:

要澄清,这是一种用于由做工精细现有应用程序的现有对象层次。此外,对象模型比在本实施例中所示的大得多。因此,我真的不希望有一个工厂向下传递到对象图中的每个构造函数的一类深在图表中使用。

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.

时那里得到不同的实现IInterface1的一种方式传递到Class没有Class2中却一无所知?

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

感谢

推荐答案

是,注入一个工厂,隐藏了怎样的类型选择:

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

您现在可以使用在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");
     }
}

注意:我有假定您意味着类2 应具有相同的接口实现不同的 IInterface1 注入。你的样品是有点混乱,因为你是显示一个实现不同的接口两班。

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.

这篇关于Autofac - 解决成分与参数动态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆