Latest Build: |
The purpose of the libarary is to provide a strongly typed, validated wrapper around the values exposed as key/value pairs by configuration providers in the Microsoft.Extensions.Configuration library.
The starting point is to define an interface through which to read your configuration. The interface must derive from IConfigurationObject. The interfaces must be public. For example:
public interface IEnvironment : IConfigurationObject
{
public string EnvironmentName { get; }
public string Version { get; }
}
public interface IMyConfig : IConfigurationObject
{
public IEnvironment Environment { get; }
public string ReadOnlyString { get; }
public string ReadWriteString { get; }
}
The next step is to register the interface as a service in
Startup.cs
.
At the same time the
IConfigurationRoot
object for the application must also be registered as a service.
public class Startup
{
private readonly IConfigurationRoot _configuration;
public Startup(IConfiguration configuration)
{
// Capture the configuration object passed in when the application is started.
_configuration = (IConfigurationRoot)configuration;
}
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddSingleton(_configuration);
services.AddConfigurationReader<IMyConfig>();
}
...
}
Later, when needed, the configuration reader is available as a service:
public MyConstructor(IMyConfig config)
{
var version = config.Environment.Version;
}
Any object returned implements the INotifyPropertyChanged interface, allowing for changes to properties (whether from the underlying configuration or from property changes made by code) to be detected.
Similary collections and dictionaries implement the INotifyCollectionChanged interface, similarly allowing changes to be detected, regardless of origin.
See "Events" for a more detailed review of the events and when they are raised.
Fine control over properties is provided by three attributes:
ConfigurationAttribute
- basic control over default value, loading and saving.PathAttribute
- the naming and path to the underlying configuration key.
The "Fine Control" article looks in more depth at how attributes can be used to control the way in which configuration data is used.
- API Documentation
- View package in nuget.org at: https://www.nuget.org/packages/OpenCollar.Extensions.Configuration/
Add a validator to the services collection using the
AddConfigurationObjectValidator
method to add
a class that implements the
IConfigurationObjectValidator
interface. The
Validate
method is called every time a batch of changes completes.
There are no hard dependencies on tooling, all that is required is the .NET Core 3.1 SDK which can be installed:
- From the SDK page;
- Or as part of Visual Studio 2019 (Versions >= 16.4.0) (the Community Edition is free).
- Visual Studio Extensions:
- CodeMaid - very handy for standardising formatting;
- EditorGuideLines - simple vertical lines in the VS text editor to help with alignment and line length;
- FxCop - static code analysis;
- GhostDoc - XML comment generation and editing;
- ILSpy - code decompilation;
- Markdown Editor - VS editor for Markdown files;
- SpecFlow - acceptance testing;
- Visual Studio Code Extensions:
- Jebbs PlantUML - UML diagram generation.
- Options Pattern in .NET Core;
- Tutorial: Express your design intent more clearly with nullable and non-nullable reference types;
- Mastering Markdown
- Coveralls GitHub Action
- Badges
- OpenCollar.Azure.ReliableQueue
- OpenCollar.Azure.Storage
- OpenCollar.Extensions
- OpenCollar.Extensions.ApplicationInsights
- OpenCollar.Extensions.Collections
- OpenCollar.Extensions.Configuraton
- OpenCollar.Extensions.Environment
- OpenCollar.Extensions.IO
- OpenCollar.Extensions.Logging
- OpenCollar.Extensions.Security
- OpenCollar.Extensions.SqlClient
- OpenCollar.Extensions.Threading
- OpenCollar.Extensions.Validation