Skip to content

BetterConfig/BetterConfigClient-dotnet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

BetterConfig client for .NET

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

Build status NuGet Version

Getting Started

  1. Install NuGet package: BetterConfigClient
Install-Package BetterConfigClient
  1. Get your Project secret from BetterConfig.com portal: ProjectSecret

  2. Create a BetterConfigClient instance:

var betterConfigClient = new BetterConfig.BetterConfigClient("#YOUR-PROJECT-SECRET#");
  1. Get your config value:
var isMyAwesomeFeatureEnabled = betterConfigClient.GetValue("isMyAwesomeFeatureEnabled", false);

if(isMyAwesomeFeatureEnabled)
{
    //show your awesome feature to the world!
}
  1. On application exit:
betterConfigClient.Dispose();

Configuration

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.

Auto polling (default)

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.

Lazy loading

Client downloads the latest configuration only when it is not present or expired in the cache. Use CacheTimeToLiveSeconds parameter to manage configuration lifetime.

Manual polling

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:

Base configuration

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)

Auto polling

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

Lazy loading

PropertyName Description Default
CacheTimeToLiveSeconds Use this value to manage the cache's TTL. 60

Example - increase CacheTimeToLiveSeconds to 600 seconds

var clientConfiguration = new BetterConfig.Configuration.LazyLoadConfiguration
            {
                ProjectSecret = "#YOUR-PROJECT-SECRET#",
                CacheTimeToLiveSeconds = 600
            };

IBetterConfigClient betterConfigClient = new BetterConfigClient(clientConfiguration);

Example - OnConfigurationChanged

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!
}

Example - default value handling

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; }

Configuration with clientbuilder

It is possible to use BetterConfigClientBuilder to build BetterConfigClient instance:

IBetterConfigClient client = BetterConfigClientBuilder
	.Initialize("YOUR-PROJECT-SECRET")
	.WithLazyLoad()
	.WithCacheTimeToLiveSeconds(120)
	.Build();

Members

Methods

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

Events

Name Description
OnConfigurationChanged Only with AutoPolling policy. Occurs when the configuration changed

Lifecycle of the client

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).

Dispose

To ensure graceful shutdown of the client you should use .Dispose() method. (Client implements IDisposable interface)

Logging

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.

License

MIT

About

BetterConfigClient for .NET

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages