BetterConfig is a cloud based configuration as a service. It integrates with your apps, backends, websites, and other programs, so you can configure them through this website even after they are deployed. https://betterconfig.com
- Install NuGet package: BetterConfigClient
Install-Package BetterConfigClient
-
Get your Project secret from BetterConfig.com portal:
-
Create a BetterConfigClient instance:
var betterConfigClient = new BetterConfig.BetterConfigClient("#YOUR-PROJECT-SECRET#");
- Get your config value:
var isMyAwesomeFeatureEnabled = betterConfigClient.GetValue("isMyAwesomeFeatureEnabled", false);
if(isMyAwesomeFeatureEnabled)
{
//show your awesome feature to the world!
}
- On application exit:
betterConfigClient.Dispose();
Client supports three different caching policies to acquire the configuration from BetterConfig. When the client downloads the latest configuration, puts it into the internal cache and serves any configuration acquisition from cache. With these caching policies you can manage your configurations' lifetimes easily.
Client downloads the latest configuration and puts into a cache repeatedly. Use PollingIntervalSeconds
parameter to manage polling interval.
You can subscribe to the OnConfigurationChanged
event to get notification about configuration changes.
Client downloads the latest configuration only when it is not present or expired in the cache. Use CacheTimeToLiveSeconds
parameter to manage configuration lifetime.
With this mode you always have to invoke .ForceRefresh()
method to download a latest configuration into the cache. When the cache is empty (for example after client initialization) and you try to acquire any value you'll get the default value!
Configuration parameters are different in each mode:
PropertyName | Description | Default |
---|---|---|
ProjectSecret |
Project secret to access your configuration. | REQUIRED |
LoggerFactory |
Factory to create an ILogger instance for tracing. |
NullTrace (no default tracing method) |
PropertyName | Description | Default |
---|---|---|
PollIntervalSeconds |
Polling interval in seconds. | 60 |
MaxInitWaitTimeSeconds |
Maximum waiting time between the client initialization and the first config acquisition in seconds. | 5 |
PropertyName | Description | Default |
---|---|---|
CacheTimeToLiveSeconds |
Use this value to manage the cache's TTL. | 60 |
var clientConfiguration = new BetterConfig.Configuration.LazyLoadConfiguration
{
ProjectSecret = "#YOUR-PROJECT-SECRET#",
CacheTimeToLiveSeconds = 600
};
IBetterConfigClient betterConfigClient = new BetterConfigClient(clientConfiguration);
In Auto polling mode you can subscribe an event to get notification about changes.
var betterConfigClient = new BetterConfigClient(projectSecret);
betterConfigClient.OnConfigurationChanged += (s, a) =>
{
// Configuration changed. Update UI!
}
You can easily manage default values with this technique when you use your configuration in many locations in the code.
var betterConfigClient = new BetterConfig.BetterConfigClient("#YOUR-PROJECT-SECRET#");
bool isMyAwesomeFeatureEnabled = betterConfigClient.GetConfiguration(MyApplicationFeatureConfig.Default).MyNewFeatureEnabled;
if (isMyAwesomeFeatureEnabled)
{
//show your awesome feature to the world!
}
internal sealed class MyApplicationFeatureConfig
{
public static readonly MyApplicationFeatureConfig Default = new MyApplicationFeatureConfig
{
// set my default values here
MyNewFeatureEnabled = false
};
public bool MyNewFeatureEnabled { get; set; }
}
You can customize deserialization settings with Newtonsoft.Json.JsonProperty
:
[JsonProperty("My_New_Feature_Enabled")]
public bool MyNewFeatureEnabled { get; set; }
It is possible to use BetterConfigClientBuilder
to build BetterConfigClient instance:
IBetterConfigClient client = BetterConfigClientBuilder
.Initialize("YOUR-PROJECT-SECRET")
.WithLazyLoad()
.WithCacheTimeToLiveSeconds(120)
.Build();
Name | Description |
---|---|
GetValue<T>(string key, T defaultValue) |
Returns the value of the key |
ForceRefresh() |
Fetches the latest configuration from the server. You can use this method with WebHooks to ensure up to date configuration values in your application. (see ASP.Net sample project to use webhook for cache invalidation) |
GetConfigurationJsonString() |
Return configuration as a json string |
T GetConfiguration<T>(T defaultValue) |
Serialize the configuration to a passed T type. You can customize your T with Newtonsoft attributes |
Name | Description |
---|---|
OnConfigurationChanged |
Only with AutoPolling policy. Occurs when the configuration changed |
We're recommend to use client as a singleton in your application. Today you can do this easily with any IoC contanier (see ASP.Net sample project).
To ensure graceful shutdown of the client you should use .Dispose()
method. (Client implements IDisposable interface)
The client doesn't use any external logging framework. If you want to add your favourite logging library you have to create an adapter to ILogger
and setup a .LoggerFactory
in BetterConfigConfiguration
.