ConventionalOptions is a convenience library for working with Microsoft's .Net Core configuration API.
The goal of ConventionalOptions is to simplify creation and use of POCO option types.
Install ConventionalOptions for the target DI container:
$> nuget install ConventionalOptions.DependencyInjection
Add Microsoft's Options feature and register option types:
services.AddOptions();
services.RegisterOptionsFromAssemblies(Assembly.GetExecutingAssembly());
Create an Options class:
public class OrderServiceOptions
{
public string StringProperty { get; set; }
public int IntProperty { get; set; }
}
Provide a corresponding configuration section (e.g. in appsettings.json):
{
"OrderService": {
"StringProperty": "Some value",
"IntProperty": 42
}
}
Inject the options into types resolved from the container:
public class OrderService
{
public OrderService(OrderServiceOptions options)
{
// ... use options
}
}
That's it!
For more examples, see the documentation or browse the specifications.