OpenCollar.Extensions.Configuration

    Support for automatic validation, update and strongly-typed access to configuration.

    NuGet Package

    Package and installation instructions at: https://www.nuget.org/packages/OpenCollar.Extensions.Configuration/

    Project

    Latest Build:
    • Source Code on GitHub
    • Issue Tracking on GitHub
    • Documentation on GitHub Pages

    Usage

    The primary 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 will be 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.

    Attributes

    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.

    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.

    Related Projects

    • 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.Threading
    • OpenCollar.Extensions.Validation
    • Improve this Doc
    Back to top
    Copyright © 2019-2020 Jonathan Evans
    Version: 0.2.232