C# Autofac在通用存储库中注册特定的配置类
我有一个通用的存储库,我想用一个特定的配置来注册它,这样它就有点像一个策略模式。这是存储库:C# Autofac在通用存储库中注册特定的配置类,c#,dependency-injection,autofac,C#,Dependency Injection,Autofac,我有一个通用的存储库,我想用一个特定的配置来注册它,这样它就有点像一个策略模式。这是存储库: public class ConfigProvider<T> : IConfigProvider<T> where T : class public ConfigProvider(IConfigFilePaths filePaths) { _filePaths = filePaths; } 因此,我想在dependen
public class ConfigProvider<T> : IConfigProvider<T> where T : class
public ConfigProvider(IConfigFilePaths filePaths)
{
_filePaths = filePaths;
}
因此,我想在dependencies配置上执行如下操作:
builder.RegisterType<ConfigProvider<TranslationSet>>().As<IConfigProvider<TranslationSet>>();
builder.UseForType<ConfigProvider<TranslationSet>>().The<TranslationPaths>(); // this obviously does not work
有人知道如何告诉autofac,它应该为ConfigProvider使用TranslationPath,而不引入新的接口来标记Config吗?您可以使用如下方法:
builder
.RegisterType<ConfigProvider<TranslationSet>>()
.As<IConfigProvider<TranslationSet>>()
.WithParameter("filePaths", new TranslationPaths());
那是我作为一个临时工做的。但是这不是一个好的实践,因为您必须自己调用构造函数,并且在不终止配置的情况下,您无法更改参数名。想象一下,有20多个配置对象,您向构造函数添加了一个参数。然后你必须去改变那些20多个新电话:我同意这不是一个理想的解决方案,但我想对于更复杂的情况,你可能会注册一家工厂
builder
.RegisterType<ConfigProvider<TranslationSet>>()
.As<IConfigProvider<TranslationSet>>()
.WithParameter("filePaths", new TranslationPaths());