Configuration

Structr.Configuration package is intended to simplify work with application settings via mapping to classes to provide strongly typed access to groups of related settings.

Installation

Configuration package is available on NuGet.

dotnet add package Structr.Configuration

Setup

Create settings class.

public class SmtpEmailSettings
{
    public string Host { get; set; }
    public int Port { get; set; }
}

Configuration services uses different providers to get source settings. For example: JSON and XML file, Database, Consul KV, or something else.

You can create custom settings provider:

public class CustomConfigurationProvider<TSettings> : SettingsProvider<TSettings>
    where TSettings : class, new()
{
    public CustomConfigurationProvider(SettingsProviderOptions options) 
        : base(options) 
    {}
    
    protected override TSettings LoadSettings()
    {
        /* Do some logic here */
    }

    protected override void UpdateSettings(TSettings settings)
    {
        /* Do some logic here */
    }

    protected override bool IsSettingsModified()
    {
        /* Do some logic here */
    }

    protected override void LogFirstAccess()
    { 
        /* Do some logic here */
    }
}

And then setup configuration services:

services.AddConfiguration()
    .AddProvider(new CustomConfigurationProvider<SmtpEmailSettings>(new SettingsProviderOptions()));

Or you can use one of implemented settings provider from list.

Usage

Structr.Configuration provides separate services for the get and set settings.

Also you can customize the settings members with special attribute.

Last updated