[英]Autofac register specific config class in generic repository
我有一个通用的存储库,我想使用特定的配置进行注册,因此它的工作方式类似于策略模式。 这是存储库:
public class ConfigProvider<T> : IConfigProvider<T> where T : class
public ConfigProvider(IConfigFilePaths filePaths)
{
_filePaths = filePaths;
}
这是我的两个用于对ConfigProvider进行参数化的配置类
public interface IConfigFilePaths
{
...
}
public class DefaultContractTypePaths : IConfigFilePaths
{
...
}
public class TranslationPaths : IConfigFilePaths
{
...
}
因此,我想对依赖项配置执行以下操作:
builder.RegisterType<ConfigProvider<TranslationSet>>().As<IConfigProvider<TranslationSet>>();
builder.UseForType<ConfigProvider<TranslationSet>>().The<TranslationPaths>(); // this obviously does not work
有人知道如何告诉autofac它应该为ConfigProvider<TranslationSet>
使用TranslationPaths
而不引入新的接口来标记配置吗?
您可以使用Named参数 ,如下所示:
builder
.RegisterType<ConfigProvider<TranslationSet>>()
.As<IConfigProvider<TranslationSet>>()
.WithParameter("filePaths", new TranslationPaths());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.