带有简单注入器的遗留 .NET 应用程序中的选项模式、配置

Options pattern, configuration, in Legacy .NET Application with Simple Injector

这篇文章 https://medium.com/@dmitryzaets/legacy-net-applications-configuration-management-net-framework-4-5-1-68220335d9d8 描述了如何将选项模式与 Autofac 一起使用。我试图将其翻译为与 Simple Injector 一起使用。但我没有运气。 这是我的国际奥委会代码

public class IocBootstrap2
{
    private Container Container { get; }
    public IocBootstrap2()
    {
        Container = new Container();
        var configurationBuilder = new ConfigurationBuilder()
            .SetBasePath(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Configuration"))
            .AddJsonFile("settings.json", optional: false, reloadOnChange: true);
        var configuration = configurationBuilder.Build();
        //Register Options
        Container.Register(typeof(IOptions<>), typeof(OptionsManager<>));
        Container.Register(typeof(IOptionsMonitor<>), typeof(OptionsMonitor<>));
        Container.Register(typeof(IOptionsFactory<>), typeof(OptionsFactory<>));
        Container.Register(typeof(IOptionsMonitorCache<>), typeof(OptionsCache<>));
        // Register ConfigurationOptions
        Container.RegisterConfigurationOptions2<MailingOptions>(configuration.GetSection("mailing"));

    #if DEBUG
        Container.Verify();
    #endif   
 }
}

public static class ConfigurationSetupExtensions2
{
    public static void RegisterConfigurationOptions2<TOptions>(this Container container, IConfiguration config)
        where TOptions : class
    {
        container.Register(typeof(IOptionsChangeTokenSource<TOptions>),
            () => new ConfigurationChangeTokenSource<TOptions>(config), Lifestyle.Transient);
        container.Register(typeof(IConfigureOptions<TOptions>),
            () => new ConfigureFromConfigurationOptions<TOptions>(config), Lifestyle.Transient);
    }
}
 public class MailingOptions
{
    public MailingOptions()
    {
        BatchSize = 1;
    }
    public int BatchSize { get; set; }
    public int BatchDelay { get; set; }
}

settings.json

{
  "mailing": {
    "batchSize": 15, 
    "batchDelay": 1
  }
}

然后我将它注入到 ViewModel:s 构造函数中,像这样

  public class BlockViewModel 
    {
        private readonly MailingOptions _options;

        #region Constructor
        public BlockViewModel(IOptions<MailingOptions> options) 
        {
            _options = options.Value;
        }
        #endregion

    }

当我 运行 它时,我在 Container.Verify 中得到异常。

The constructor of type OptionsFactory<MailingOptions> contains the parameter with name 'setups' and type IEnumerable<IConfigureOptions<MailingOptions>> that is not registered. Please ensure IEnumerable<IConfigureOptions<MailingOptions>> is registered, or change the constructor of OptionsFactory<MailingOptions>. There is, however, a registration for IConfigureOptions<MailingOptions>; Did you mean to depend on IConfigureOptions<MailingOptions>?

堆栈跟踪:

在SimpleInjector.Container.ThrowParameterTypeMustBeRegistered(InjectionTargetInfo 目标)

我将如何注册 IEnumerable<IConfigureOptions<MailingOptions>>? 谁能告诉我我做错了什么,或者更准确地说,我不明白的地方是什么?

简短的回答是:不要将 IOptions<T> 注入到您的应用程序组件中。正如所解释的 here,这只会使您的组件、它们的单元测试以及您已经注意到的配置复杂化。

相反,让 BlockViewModel 直接依赖于 MailingOptions

public class BlockViewModel 
{
    private readonly MailingOptions _options;

    public BlockViewModel(MailingOptions options) 
    {
        _options = options ?? throw new ArgumentNullException(nameof(options));
    }
}

这使您可以将配置简化为以下内容:

Container = new Container();
var configutation = new ConfigurationBuilder()
  .SetBasePath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Configuration"))
    .AddJsonFile("settings.json", optional: false);
    .Build();

MailingOptions options = configuration.GetSection("mailing").Get<MailingOptions>();
Container.RegisterInstance<MailingOptions>(options);

// Register View Models
Container.Register<BlockViewModel>();

Container.Verify();