HamedStack.Configuration.Database
1.0.0
dotnet add package HamedStack.Configuration.Database --version 1.0.0
NuGet\Install-Package HamedStack.Configuration.Database -Version 1.0.0
<PackageReference Include="HamedStack.Configuration.Database" Version="1.0.0" />
paket add HamedStack.Configuration.Database --version 1.0.0
#r "nuget: HamedStack.Configuration.Database, 1.0.0"
// Install HamedStack.Configuration.Database as a Cake Addin #addin nuget:?package=HamedStack.Configuration.Database&version=1.0.0 // Install HamedStack.Configuration.Database as a Cake Tool #tool nuget:?package=HamedStack.Configuration.Database&version=1.0.0
Library Overview
This library enables applications to dynamically load and update configurations from a relational database. It supports automatic refresh intervals and manual reloading of settings, ensuring that your application can adapt to configuration changes on-the-fly without restarting or redeploying the application.
Features
- Dynamic Configuration Loading: Load key/value pairs from a relational database to manage application settings.
- Automatic Refresh: Configure a reload timer to refresh settings automatically at specified intervals.
- Manual Reload Option: Manually trigger a reload of settings to ensure the application can quickly adapt to configuration changes.
- Customizable Schema: Flexibly define the database schema, table, and column names to fit your existing database structure.
Preparing Your Database
First, ensure your database includes a table for storing configuration key/value pairs. The default expected table structure is as follows:
CREATE TABLE Settings (
Key VARCHAR(255) NOT NULL,
Value VARCHAR(255),
PRIMARY KEY (Key)
);
Library Setup
Basic Configuration
Establish Database Connection: Begin by creating a
DbConnection
instance using your database connection string. This connection will be used to load settings from the database.var connectionString = builder.Configuration.GetConnectionString("DefaultConnection"); var dbConnection = new SqlConnection(connectionString);
Add Database Configuration to Your Application: Integrate the database settings into your application's configuration system.
builder.Configuration.AddDatabase(dbConnection);
Advanced Configuration
For advanced scenarios, you can customize the default behavior by specifying additional options:
builder.Configuration.AddDatabase(dbConnection, option =>
{
option.Schema = "cfg"; // Default is empty.
option.Table = "Configurations"; // Default is "Settings".
option.KeyColumn = "K"; // Default is "Key".
option.ValueColumn = "V"; // Default is "Value".
option.AutoReload = new TimeSpan(0, 0, 0, 20); // Set auto-reload interval for every 20 seconds. Default is no auto reload (zero).
});
Using Configuration in Your Application
Register Configuration Classes
Define Configuration POCO: Create a class representing your configuration settings, mapping each property to a specific configuration key.
public sealed class Site { public int Name { get; init; } // Maps to Site:Name in the database }
Register and Set in DI: Register your configuration class with the Dependency Injection (DI) container and set it up for use within your application.
builder.Services.Configure<Site>(builder.Configuration.GetSection("Site"));
Accessing Configurations
Utilize
IOptionsSnapshot
to access the latest configuration values within your application components, ensuring you can react to changes in configuration dynamically.public class WeatherForecastController : ControllerBase { private readonly IOptionsSnapshot<Site> _settingsSnapshot; public WeatherForecastController(IOptionsSnapshot<Site> settingsSnapshot) { _settingsSnapshot = settingsSnapshot; } }
Access configuration values through the
_settingsSnapshot
instance. For example, if your database has a record withKey: Site:Name
andValue: www.google.com
, you can retrieve this value using_settingsSnapshot.Value.Name
.
Refreshing Configuration
- Automatic Refresh: If
ReloadConfigTime
is set, the library will automatically refresh configuration values from the database at the specified intervals. - Manual Refresh: Call
DatabaseConfigurationSource.Reload();
to manually trigger a configuration reload at any time.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
-
net6.0
- Dapper (>= 2.1.35)
- Microsoft.Extensions.Configuration (>= 8.0.0)
- Microsoft.Extensions.Configuration.Abstractions (>= 8.0.0)
- Microsoft.Extensions.DependencyInjection (>= 8.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.1)
-
net7.0
- Dapper (>= 2.1.35)
- Microsoft.Extensions.Configuration (>= 8.0.0)
- Microsoft.Extensions.Configuration.Abstractions (>= 8.0.0)
- Microsoft.Extensions.DependencyInjection (>= 8.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.1)
-
net8.0
- Dapper (>= 2.1.35)
- Microsoft.Extensions.Configuration (>= 8.0.0)
- Microsoft.Extensions.Configuration.Abstractions (>= 8.0.0)
- Microsoft.Extensions.DependencyInjection (>= 8.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.0 | 134 | 3/16/2024 |