简体   繁体   中英

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

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?

You could use a Named parameter as follows:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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