Dependency injection 是否可以阻止在Autofac中创建代理工厂?
我正在更新一个使用Autofac的库,这样,除了原始配置文件(通过Autofac注册)之外,它还可以选择使用一个函数来实现相同的目标(同样,通过Autofac注册)。原文是这样的:Dependency injection 是否可以阻止在Autofac中创建代理工厂?,dependency-injection,autofac,Dependency Injection,Autofac,我正在更新一个使用Autofac的库,这样,除了原始配置文件(通过Autofac注册)之外,它还可以选择使用一个函数来实现相同的目标(同样,通过Autofac注册)。原文是这样的: public MyClass(ConfigFile config = null) { this._activatorLoader = a => { // old config code here... } } 我想要的更新版本是: public MyClass( Fun
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在未明确设置的情况下创建
Func
。这可能吗?也许,为activator加载创建一个单独的类会更容易些
public class ActivatorsLoader
{
public IList<Activator> Load(Input input)
{
///
}
}
///
public Class(
ActivatorsLoader activatorsLoader = null,
ConfigFile config = null)
{
///
}
公共类激活器加载程序
{
公共IList加载(输入)
{
///
}
}
///
公共课(
ActivatorsLoader ActivatorsLoader=null,
ConfigFile config=null)
{
///
}
但最好能看到您的类的用例。您的问题有点不清楚。。。您可以解释如何实例化和使用您的
类
,以及为什么应该延迟加载激活器?但我认为您可能需要使用Lazy
,因此Lazy activatorLoader
在这种情况下,当您访问activatorLoader.Value
时,Autofac只会创建Func
。在所有情况下(Lazy和Func),激活器的加载都会延迟,这没什么区别。我希望在不创建单独的类或接口的情况下解决这个问题。这两个用例是:(1)没有注册Func
的较旧代码,因此应该使用配置文件(因此在第二个和第三个示例中使用空检查)和(2)注册Func
的较新代码,并希望用它来代替配置文件。因为我找不到解决这个问题的方法,我认为这是正确答案。
var myObject = conatiner.Resolve<MyClass>();
// time passes...
myObject.DoActivatorLoading();
public class ActivatorsLoader
{
public IList<Activator> Load(Input input)
{
///
}
}
///
public Class(
ActivatorsLoader activatorsLoader = null,
ConfigFile config = null)
{
///
}