Ninject integration library for Cascade hierarchical configuration management. Provides seamless integration with Ninject dependency injection and real-time configuration updates.
$ dotnet add package CascadeConfig.NinjectNinject integration library for Cascade Configuration Management. This library provides seamless integration of Cascade's hierarchical configuration system with Ninject dependency injection.
ICascadeConfig<T>dotnet add package CascadeConfig.Ninject
using Cascade.Ninject;
using Ninject;
var kernel = new StandardKernel();
// Bind Cascade configurations
kernel.BindCascade(
apiKey: "your-cascade-api-key",
baseUrl: "http://localhost:5000", // Or your Cascade server URL
options =>
{
options.Bind<AppSettings>("my-app.production");
options.Bind<DatabaseConfig>("my-app.production.database");
});
public class AppSettings
{
public int Timeout { get; set; }
public string ApiEndpoint { get; set; }
public bool EnableDebug { get; set; }
}
public class DatabaseConfig
{
public string ConnectionString { get; set; }
public int MaxConnections { get; set; }
}
public class MyService
{
private readonly ICascadeConfig<AppSettings> _appConfig;
private readonly ICascadeConfig<DatabaseConfig> _dbConfig;
public MyService(
ICascadeConfig<AppSettings> appConfig,
ICascadeConfig<DatabaseConfig> dbConfig)
{
_appConfig = appConfig;
_dbConfig = dbConfig;
}
public void DoSomething()
{
// Access current configuration value
var timeout = _appConfig.Value.Timeout;
var connectionString = _dbConfig.Value.ConnectionString;
Console.WriteLine($"Timeout: {timeout}");
Console.WriteLine($"Connection: {connectionString}");
}
}
// Resolve your service
var myService = kernel.Get<MyService>();
myService.DoSomething();
Customize where configuration is cached locally:
kernel.BindCascade(
apiKey: "your-api-key",
baseUrl: "http://localhost:5000",
options =>
{
options.Bind<AppSettings>("my-app.production");
options.LocalCachePath = ".config/cascade";
});
Default: .cascade
If using Cascade Cloud, you can omit the base URL:
kernel.BindCascade(
apiKey: "your-api-key",
options =>
{
options.Bind<AppSettings>("my-app.production");
});
This uses https://cascade-cloud.com as the default base URL.
Cascade configurations automatically update when values change on the server:
public class MyService
{
private readonly ICascadeConfig<AppSettings> _config;
public MyService(ICascadeConfig<AppSettings> config)
{
_config = config;
// Subscribe to changes
_config.OnChange(newSettings =>
{
Console.WriteLine($"Configuration updated! New timeout: {newSettings.Timeout}");
});
}
public void CheckConfig()
{
// Always returns the current value, even after updates
var currentTimeout = _config.Value.Timeout;
}
}
You can bind multiple configuration types to different Cascade addresses:
kernel.BindCascade(
apiKey: "your-api-key",
baseUrl: "http://localhost:5000",
options =>
{
options.Bind<AppSettings>("my-app.production");
options.Bind<DatabaseConfig>("my-app.production.database");
options.Bind<CacheConfig>("my-app.production.redis");
options.Bind<LoggingConfig>("my-app.production.logging");
});
Each configuration type is independently resolved and updated.
Use different Cascade addresses for different environments:
var environment = Environment.GetEnvironmentVariable("ENVIRONMENT") ?? "development";
kernel.BindCascade(
apiKey: "your-api-key",
baseUrl: "http://localhost:5000",
options =>
{
options.Bind<AppSettings>($"my-app.{environment}");
options.Bind<DatabaseConfig>($"my-app.{environment}.database");
});
Cascade's hierarchical system allows child configurations to override parent values:
Parent config at "my-app":
{
"timeout": 30,
"retries": 3,
"enableDebug": false
}
Child config at "my-app.production":
{
"timeout": 60,
"enableDebug": false
}
Resolved config at "my-app.production":
{
"timeout": 60, // Overridden by child
"retries": 3, // Inherited from parent
"enableDebug": false // Overridden by child
}
The ICascadeConfig<T> interface provides:
public interface ICascadeConfig<T> where T : class, new()
{
// Current configuration value (always up-to-date)
T Value { get; }
// Subscribe to configuration changes
IDisposable OnChange(Action<T> onChange);
// Address this config is bound to
string Address { get; }
}
var subscription = _config.OnChange(newValue =>
{
Console.WriteLine("Config changed!");
});
// Unsubscribe when done
subscription.Dispose();
If the Cascade server is unavailable at startup, the library will:
If the connection is lost during runtime:
BindCascade() before binding other services that depend on configurationOnChange() for services that need to react to configuration updatesusing Cascade.Ninject;
using Ninject;
// Define configuration classes
public class AppSettings
{
public int Timeout { get; set; }
public string ApiEndpoint { get; set; }
}
public class MyService
{
private readonly ICascadeConfig<AppSettings> _config;
public MyService(ICascadeConfig<AppSettings> config)
{
_config = config;
}
public void Run()
{
Console.WriteLine($"Using endpoint: {_config.Value.ApiEndpoint}");
}
}
// Setup Ninject
var kernel = new StandardKernel();
// Bind Cascade
kernel.BindCascade(
apiKey: "cascade_asdsd5pWArZ26UGmECmFKVzIT2DHQxZKM",
baseUrl: "http://localhost:5000",
options =>
{
options.Bind<AppSettings>("my-app.production");
options.LocalCachePath = ".cascade-demo";
});
// Bind your services
kernel.Bind<MyService>().ToSelf().InSingletonScope();
// Resolve and run
var service = kernel.Get<MyService>();
service.Run();
_config.Value each time (not caching it locally)Default cache: .cascade/ in application root. Ensure this directory is:
MIT License - This library is part of the Cascade Configuration Management system.
For issues, questions, or contributions, please visit the main Cascade repository at http://cascade-cloud.com/