Skip to content

open-collar/OpenCollar.Extensions.Configuration

Repository files navigation

Open Collar

OpenCollar.Extensions.Configuration

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;
}

Events

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 Contol

Fine control over properties is provided by three attributes:

The "Fine Control" article looks in more depth at how attributes can be used to control the way in which configuration data is used.

Validation

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.

Set-up Developer Environment

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).

Non-Essential Tools

  • 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:

Reference

Related Projects

About

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.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages