簡體   English   中英

Autofac在通用存儲庫中注冊特定的配置類

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM