Skip to content

Instantly share code, notes, and snippets.

@configureappio
configureappio / KelvinMappers.cs
Created Jan 19, 2020
Interface and implementations to map temperature scales to Kelvin scale
View KelvinMappers.cs
private interface IKelvinMapper
{
decimal ToKelvins(decimal value);
}
private class CentigradeToKelvinMapper : IKelvinMapper
{
public decimal ToKelvins(decimal value) => value + 273.15M;
}
@configureappio
configureappio / StartupByConvention.cs
Created Apr 11, 2018
Example of Startup Class adding a logging provider by convention
View StartupByConvention.cs
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
@configureappio
configureappio / Interfaces.cs
Created Apr 3, 2018
An example of aggregating multiple interfaces to allow registration with interface segregation
View Interfaces.cs
public interface IAppSettings
{
string ApplicationName { get; }
}
public interface ISqlConnectionSettings
{
string SqlConnectionSting { get; }
}
@configureappio
configureappio / MyAppSettingsBridge.cs
Created Apr 3, 2018
Example of bridging settings class that takes a validator and and decryptor
View MyAppSettingsBridge.cs
public class MyAppSettingsBridge : IAppSettingsResolved
{
private readonly IOptions<MyAppSettings> _appSettings;
private readonly ISettingsDecrypt _decryptor;
public MyAppSettingsBridge(IOptionsSnapshot<MyAppSettings> appSettings, ISettingsDecrypt decryptor, ISettingsValidator validator) {
_appSettings = appSettings ?? throw new ArgumentNullException(nameof(appSettings));
_decryptor = decryptor ?? throw new ArgumentException(nameof(decryptor));
if (validator == null) throw new ArgumentNullException(nameof(validator));
@configureappio
configureappio / SettingsValidator.cs
Created Apr 3, 2018
Example of configuration settings validator
View SettingsValidator.cs
public class SettingsValidator : ISettingsValidator
{
public bool TryValidate(IAppSettingsStructure settings, out AggregateException validationExceptions)
{
if (settings == null) throw new ArgumentNullException(nameof(settings));
var exceptions = new List<Exception>();
if (settings.ApplicationName == null) exceptions.Add(new ArgumentNullException(nameof(settings.ApplicationName)));
if (string.IsNullOrWhiteSpace(settings.ApplicationName)) exceptions.Add(new ArgumentOutOfRangeException(nameof(settings.ApplicationName)));
@configureappio
configureappio / SettingsDecryptor.cs
Created Apr 2, 2018
Example of decrypting a dictionary with a hashed key
View SettingsDecryptor.cs
public class SettingsDecryptor : ISettingsDecrypt
{
private readonly ICryptoAlgorithm _crypto;
public SettingsDecryptor(ICryptoAlgorithm crypto)
{
_crypto = crypto ?? throw new ArgumentNullException(nameof(crypto));
}
public string Decrypt(string key, IDictionary<string, string> keyValues)
{
@configureappio
configureappio / ICryptoFactory.cs
Last active Apr 2, 2018
An example factory interface for creating a decryptor
View ICryptoFactory.cs
public interface ICryptoFactory
{
ICryptoAlgorithm Create<T>(string password, string salt) where T : SymmetricAlgorithm, new();
ICryptoAlgorithm Create(Type type, string password, string salt);
}
public interface ICryptoAlgorithm
{
string Decrypt(string text);
string Encrypt(string text);
@configureappio
configureappio / MyAppSettings.,cs
Created Apr 2, 2018
Example of class to read secrets dictionary
View MyAppSettings.,cs
public class MyAppSettings : IAppSettingsStructure
{
public string ApplicationName { get; set; }
public Dictionary<string, string> Secrets { get; set; } = new Dictionary<string, string>();
}
@configureappio
configureappio / appsettings.json
Created Apr 2, 2018
Hiding secrets in appsettings.json
View appsettings.json
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning"
}
},
"MyAppSettings": {
"ApplicationName": "My Cool Application",
"Secrets": {
@configureappio
configureappio / Program.cs
Created Apr 2, 2018
Example of using a PhysicalFileProvider to access a JSON file outside of the web site
View Program.cs
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args)
{
var webhost = WebHost.CreateDefaultBuilder(args);