Skip to content

Instantly share code, notes, and snippets.

@configureappio
Created April 2, 2018 15:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save configureappio/6de2917887f97e86edf5189ae24af701 to your computer and use it in GitHub Desktop.
Save configureappio/6de2917887f97e86edf5189ae24af701 to your computer and use it in GitHub Desktop.
Example of injecting a decyption service and settings validator into the settings bridge
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddOptions();
services.Configure<MyAppSettings>(Configuration.GetSection("MyAppSettings"));
services.AddSingleton(Configuration);
// In a real implementation, you would have a factory here to get the password and salt securely
// from somewhere such as Azure Key Vault, Environmental variable / another json setting (obfuscated in some way)
services.AddSingleton( x => new CryptoFactory().Create<AesManaged>("Password", "Salt"));
services.AddSingleton<ISettingsDecrypt, SettingsDecryptor>();
services.AddSingleton<ISettingsValidator, SettingsValidator>();
services.AddScoped<IAppSettingsResolved, MyAppSettingsBridge>();
// add the other interfaces implemented by MyAppSettingsBridge to allow for resolution by those interfaces (interface segregation)
services.AddScoped<IAppSettings>(provider => provider.GetService<IAppSettingsResolved>());
services.AddScoped<ISqlConnectionSettings>(provider => provider.GetService<IAppSettingsResolved>());
services.AddScoped<IOracleConnectionSettings>(provider => provider.GetService<IAppSettingsResolved>());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment