Provides application settings management for xPike.
xPike Settings is the recommended source for application settings.
It uses xPike Configuration as its configuration source by default, and other providers can be added. In addition, it adds a configurable layer of caching, as well as settings refresh capabilities.
Settings Providers are fully DI-managed objects (unlike Configuration Providers).
-
ISettingsService
=>SettingsService
-
ISettingsManager<>
=>SettingsManager<>
-
ISettings<>
=>SettingsLoader<>
-
IConfigurationSettingsProvider
=>ConfigurationSettingsProvider
-
IEnumerable<ISettingsProvider>
Add:IConfigurationSettingsProvider
Note: You should not inject this directly - only access it via ISettingsService. Injected collections are not thread-safe.
In ASP.NET Core:
public void ConfigureServices(IServiceCollection services)
{
services.AddXPikeDependencyInjection()
.AddXPikeSettings();
}
using XPike.Settings;
namespace MyLibrary
{
public class MySettings
{
public string CompanyName { get; set; }
public IList<DateTime> UpcomingHolidays { get; set; }
public IDictionary<string, string> LocationPhoneNumbers { get; set; }
}
}
xPike supports a "touchless" settings system.
Just inject ISettings<MySettings>
wherever you want.
xPike will search for a setting with a matching fully-qualified class name (eg "MyLibrary.MySettings"
).
The settings will be deserialized from a JSON string. Note: This may vary by Provider.
The value can come from any of the registered Configuration or Settings Providers. xPike Settings will handle caching and periodic refresh automatically.
The built-in Settings Manager allows you to specify a custom configuration key. You can also specify an action to be called upon load to further customize your settings.
container.RegisterSingleton<ISettingsManager<MySettings>>(provider =>
new SettingsManager(provider.ResolveDependency<ISettingsService>(),
"myCustomKey",
settings =>
{
// further customization of settings object
return settings;
});
For the most flexibility, you can specify your own Settings Manager.
using XPike.Settings;
namespace MyLibrary
{
public class MySettingsManager
: SettingsManagerBase<MySettings>
{
protected override string ConfigurationKey => $"{nameof(MyLibrary)}::{nameof(MySettings)}";
public MySettingsManager(ISettingsService settingsService)
: base(settingsService)
{
}
public override ISettings<TSettings> GetSettings()
{
var settings = new MySettings
{
CompanyName = GetValue("CompanyName"),
UpcomingHolidays = GetValue<IList<DateTime>>("UpcomingHolidays"),
LocationPhoneNumbers = string.Split(new[] {','},
GetValueOrDefault("LocationPhoneNumbers", "555-1212"))
};
return new Settings(ConfigurationKey, settings);
}
}
}
container.RegisterSingleton<ISettingsManager<MySettings>, MySettingsManager>();
Note: If you are using SimpleInjector for dependency injection, then you must be certain that any custom Settings Managers are registered before the Settings Package. Since a Package can load other Packages, this may require that you define them at the start of your top-level Package.
-
The
GetValue()
andGetValueOrDefault()
overloads onSettingsManager<TSettings>
prepend theConfigurationKey
to the specifiedkey
. -
The default
ConfigurationKey
is the full class and namespace of the Settings POCO. -
In this example, the call to
GetValue<IList<DateTime>>(...)
loads its value from a JSON string in theMyLibrary.MySettings::UpcomingHolidays
key.
Newtonsoft.Json
XPike.Configuration
XPike.IoC
Building from source and running unit tests requires a Windows machine with:
- .Net Core 3.0 SDK
- .Net Framework 4.6.1 Developer Pack
Issues are tracked on GitHub. Anyone is welcome to file a bug, an enhancement request, or ask a general question. We ask that bug reports include:
- A detailed description of the problem
- Steps to reproduce
- Expected results
- Actual results
- Version of the package xPike
- Version of the .Net runtime
See our contributing guidelines in our documentation for information on how to contribute to xPike.
xPike is licensed under the MIT License.