Structr
Search…
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, XML file, Database, or something else.
You can create custom configuration 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 default implemented configuration provider from list:

JSON provider

Create JSON file with settings:
{
"Host": "smtp.example.com",
"Port": 25
}
Setup JSON configuration provider:
services.AddConfiguration()
.AddJson<SmtpEmailSettings>("path_to_json_file");

XML provider

Create XML file with settings:
<?xml version="1.0" encoding="utf-16"?>
<SmtpEmailSettings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Host>smtp.example.com</Host>
<Port>25</Port>
</SmtpEmailSettings>
Setup XML configuration provider:
services.AddConfiguration()
.AddXml<SmtpEmailSettings>("path_to_xml_file");

InMemory provider

Setup In-Memory configuration provider:
services.AddConfiguration()
.AddInMemory<SmtpEmailSettings>(new SmtpEmailSettings {
Host = "smtp.example.com",
Post = 25
});

Options

When you setup configuration provider you can configure provider options represents by SettingsProviderOptions.
SettingsProviderOptions properties:
Property name
Property type
Description
Cache
bool
Determines whether settings should be cached, true by default.
Example configure services:
services.AddConfiguration()
.AddJson<SmtpEmailSettings>("path_to_json_file", (serviceProvider, options) =>
{
options.Cache = true;
});
If Cache options was setting up to true then settings provider return cached settings while IsSettingsModified() returns false, otherwise every settings request invoke LoadSettings() method.

Usage

Structr.Configuration provides separate services for the get and set settings.
Also you can customize the settings members with special attribute.
Copy link
Edit on GitHub
On this page
Installation
Setup
JSON provider
XML provider
InMemory provider
Options
Usage