是否可以防止在Autofac中创建代理工厂?

时间:2014-01-06 22:57:24

标签: dependency-injection autofac

我正在更新使用Autofac的库,这样除了原始配置文件(通过Autofac注册)之外,它还可以选择使用一个函数来实现相同的目标(同样,通过Autofac注册)。原件是这样的:

public MyClass(ConfigFile config = null)
{
    this._activatorLoader = a => {
        // old config code here...
    }
}

我想要的更新版本是:

public MyClass(
    Func<Input, IList<Activator>> activatorLoader = null,
    ConfigFile config = null)
{
    if (activatorLoader != null)
    {
        this._activatorLoader = activatorLoader;
    }
    else
    {
        this._activatorLoader = a => {
            // old config code here...
        }
    }
}

问题在于Autofac正在查看我对某个列表的请求并始终提供该功能。我尝试切换到委托并得到同样的问题:

public delegate IList<Activator> ActivatorLoader(Input input);

public MyClass(
    ActivatorLoader activatorLoader = null,
    ConfigFile config = null)
{
    if (activatorLoader != null)
    {
        this._activatorLoader = activatorLoader;
    }
    else
    {
        this._activatorLoader = a => {
            // old config code here...
        }
    }
}

激活器的加载仍然必须延迟,我希望根据情况灵活地注册任何功能,并且旧代码(没有注册激活器加载器)仍然可以工作。有没有办法阻止Autofac自动生成Func


该类将通过另一个类中的依赖注入进行实例化。稍后,将触发激活器加载代码(如果需要)。

var myObject = conatiner.Resolve<MyClass>();

// time passes...

myObject.DoActivatorLoading();

主要目标是防止Autofac创建Func<Input, IList<Activator>>(如果未明确设置)。这可能吗?

1 个答案:

答案 0 :(得分:0)

也许,为激活器加载创建一个单独的类会更容易吗?

public class ActivatorsLoader
{
    public IList<Activator> Load(Input input)
    {
        ///
    }
}

///

public Class(
    ActivatorsLoader activatorsLoader = null,
    ConfigFile config = null)
{
    ///
}

但是看看你班级的用例会很好。