带有Simple Injector的旧版.NET应用程序中的选项模式、配置



本文,https://medium.com/@dmitryzaets/legacy-net-applications-configuration-management-net-framework-4-5-1-68220335d9d8,描述如何将Options模式与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的构造函数中,如

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

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

OptionsFactory<类型的构造函数;MailingOptions>包含名为"setups"且类型为IEnumerable<IConfigureOptions<未注册的MailingOptions>>。请确保IEnumerable<IConfigureOptions<MailingOptions>>已注册,或者更改OptionsFactory<邮件选项>。然而,存在IConfigureOptions<邮件选项>;您的意思是依赖IConfigureOptions<邮件选项>?

StackTrace:

在SimpleInjector.Container.ThrowParameterTypeMustBeRegistered(InjectionTargetInfo目标(

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

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

相反,让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();

相关内容

  • 没有找到相关文章

最新更新