The settings service is a library that provides a simlple interface to retrieve settings from different sources. The interface is designed for applications using the IoC (Inversion of Control) pattern, but it can also be used in applications. A setting can be of any type (although simple types like struct
s and string
are recommended) and is identified by an unique key.
The SettingsProvider is a Service motivated by Services in Modular applications. It was first created in a WPF Application, but it can be used in any .net Applications, since it does not require any classes outside the .net standard.
This package is listed in the official nuget.org feed. You can install the latest release version by typing
PM> Install-Package phirSOFT.SettingsService
To retrieve development versions please add the development feed at https://phirsoft.pkgs.visualstudio.com/phirSOFT.SettingsService/_packaging/phirSOFT.SettingsServer/nuget/v3/index.json to your feed list or install the package directly from that feed.
PM> Install-Package phirSOFT.SettingsService -Source https://phirsoft.pkgs.visualstudio.com/phirSOFT.SettingsService/_packaging/phirSOFT.SettingsServer/nuget/v3/index.json
There a some setting service implementations already, that partially allow integration in existing systems.
Storage format | NuGet package | Repository | Notes |
---|---|---|---|
Json | phirsoft/SettingsService.Json | ||
Windows Registry | phirsoft/SettingsService.Registry | Requires a custom RegistryAdapter for non primitive types | |
Ini file | not published yet | phirsoft/SettingsService.Ini | Requires a custom adapter for string serialization and deserialization |
ApplicationSetting | not published yet | phirSOFT/SettingsService.ApplicationSettingsService | Properties must be specified at compile time. Properties can be changed, but no new can be registered. |
Assume you are writing a WebClient for an application. You want the user to be able to configure a timeout.
public class MyWebClient
{
private readonly IReadOnlySettingsService _settingsService;
public MyWebClient(IReadOnlySettingsService settingsService)
{
_settingsService = settingsService;
}
public async Task DoWorkAsync()
{
TimeSpan timeout = _settingService.GetSettingAsync<TimeSpan>("Timeout");
// use the timeout
}
}
The above example retrieves the current timeout setting each time the web client has to perform some work. The settings service API is intentionally asynchronous, since there is a high propability, that retrieving a setting involves some io operation.
To make the example abovr work, you have to register the Timeout
property within the settings service. It's recommendet to perform the registration at the startup of the application. However you should register a property, before any component requires that property.
private static async Task InitializeSettingsAsync(ISettingsService settingsService)
{
await settingsService.RegisterPropertyAsync("Timeout", TimeSpan.FromSeconds(30));
}
If you have lots of properties, you may want to use some marker property that existence will tell you, wheter the settings service is initialized
private static async Task InitializeSettingsAsync(ISettingsService settingsService)
{
if(await settingsService.IsRegisteredAsync("Initialized")
await RegisterProperties(settingsService)
}
Contributions to this (and the backend implementations) are welcome. See How to Contribute for further information.