Skip to content

Instantly share code, notes, and snippets.

View null_forgiving_CS8598_net_6_rc_1.cs
using System;
public class C {
public void M(string value)
if (value is null!) throw new ArgumentNullException(nameof(value));
// works in NET 5 (C#9) but fails in 6 RC1 (C#10) with error CS8598: The suppression operator is not allowed in this context
// I can see why as the null does not need a null foriving as string is not a declared string?
// so it is correct to flag as a code smell, but should the error CS8598 be an error or a warning when migrating NET5 code?
configureappio / KelvinMappers.cs
Created January 19, 2020 13:50
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 / StartupByConvention.cs
Created April 11, 2018 12:00
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 / Interfaces.cs
Created April 3, 2018 07:48
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 / MyAppSettingsBridge.cs
Created April 3, 2018 07:36
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 / SettingsValidator.cs
Created April 3, 2018 07:26
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 / SettingsDecryptor.cs
Created April 2, 2018 16:37
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 / ICryptoFactory.cs
Last active April 2, 2018 16:27
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 / MyAppSettings.,cs
Created April 2, 2018 16:13
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 / appsettings.json
Created April 2, 2018 16:08
Hiding secrets in appsettings.json
View appsettings.json
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning"
"MyAppSettings": {
"ApplicationName": "My Cool Application",
"Secrets": {