简体   繁体   English

Autofac在通用存储库中注册特定的配置类

[英]Autofac register specific config class in generic repository

I have a generic repository that I want to register with a specific config so it works kind of like a strategy pattern. 我有一个通用的存储库,我想使用特定的配置进行注册,因此它的工作方式类似于策略模式。 This is the repository: 这是存储库:

public class ConfigProvider<T> : IConfigProvider<T> where T : class 

public ConfigProvider(IConfigFilePaths filePaths)
        {
            _filePaths = filePaths;
        }

and those are my two config classes that are used to parametrize the ConfigProvider 这是我的两个用于对ConfigProvider进行参数化的配置类

public interface IConfigFilePaths
{
    ...
}

public class DefaultContractTypePaths : IConfigFilePaths
{
    ...
}

public class TranslationPaths : IConfigFilePaths
{
    ...
}

So what i would like to do on the dependencies config is something like this: 因此,我想对依赖项配置执行以下操作:

builder.RegisterType<ConfigProvider<TranslationSet>>().As<IConfigProvider<TranslationSet>>();
builder.UseForType<ConfigProvider<TranslationSet>>().The<TranslationPaths>(); // this obviously does not work

Does anybody know how to tell autofac that it should use TranslationPaths for the ConfigProvider<TranslationSet> without introducing a new interface to mark the configs? 有人知道如何告诉autofac它应该为ConfigProvider<TranslationSet>使用TranslationPaths而不引入新的接口来标记配置吗?

You could use a Named parameter as follows: 您可以使用Named参数 ,如下所示:

 builder
    .RegisterType<ConfigProvider<TranslationSet>>()
    .As<IConfigProvider<TranslationSet>>()
    .WithParameter("filePaths", new TranslationPaths());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM