Provides dynamic configuration options management with runtime updates, type safety, and FluentValidation support. Enables applications to modify configuration settings without restarts.
$ dotnet add package maple-Configuration.OptionsUpdateableThe Configuration.OptionsUpdateable namespace provides a set of classes and interfaces that facilitate the management and updating of configuration options in a flexible and dynamic manner. This namespace is designed to help developers easily modify configuration settings at runtime without requiring application restarts.
Code coverage for Configuration.OptionsUpdateable is automatically collected with each build:
Dynamic Updates: Allows for real-time updates to configuration options, enabling applications to adapt to changing requirements without downtime.
Type Safety: Utilizes strong typing to ensure that configuration options are valid and consistent throughout the application.
Validation Support: Integrates with validation mechanisms to ensure that updated configuration options meet predefined criteria. Functionality is based on FluentValidation library
To use the Configuration.OptionsUpdateable namespace, you typically start by defining your configuration options as a class. You can then register this class with the dependency injection container and use the provided interfaces to update the options as needed.
Working example can be found here:
using System;
using Configuration.OptionsUpdateable;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
// Define your configuration options class
public class MyConfigOptions : IConfigModel<MyConfigOptions>
{
public string SettingA { get; set; }
public int SettingB { get; set; }
public Position DeepClone()
{
// Create a deep copy of the current instance
// This is a simple example; for complex objects, ensure all nested objects are also cloned
return this with { };
}
}
// Register the options in the service collection
string configFolder = Path.Combine( System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData), "MyApp/Config");
builder.ConfigureUpdateableOptions(new UpdateableOptionsConfig
{
ConfigurationFolder = configFolder,
ValidateAtStartup = true,
Validators =
[
new PositionValidator()
]
});
// Inject IUpdateableOptions to update options at runtime
public class MyService
{
private readonly IUpdateableOptions<MyConfigOptions> _updateableOptions;
public MyService(IUpdateableOptions<MyConfigOptions> updateableOptions)
{
_updateableOptions = updateableOptions;
// _updateableOptions.Value returns updated information when callback is invoked
// (information is also updated without registering a callback)
_updateableOptions.RegisterUpdateNotification((updated) =>
{
Console.WriteLine($"Updated Position: {updated}");
HasReceivedUpdate = true;
});
}
public void UpdateSettings(string newSettingA, int newSettingB)
{
// local copy that can be modified
var options = _updateableOptions.Value;
// Update the settings
options.SettingA = newSettingA;
options.SettingB = newSettingB;
// modifications are updated in the underlying storage
_updateableOptions.Update(options);
}
}