Skip to content

Notes

Ensuring your configuration is strongly typed and can be validated at application start

.NET Core comes with IConfiguration, `IOptions<T>` and `IOptionsMonitor<T>`. Combining these, one can create strongly typed configuration for every class/service whilst ensuring that valid config is provided at app start.

Posted on Friday, March 8, 2019

The Problem

.NET Core has the IConfiguration abstraction out of the box. It also has the ability to read configuration from multiple sources such as JSON files, Environment Variables etc and one can easily write a custom configuration provider for getting configuration from another source (such as an HTTP endpoint). However, passing IConfiguration around your code base as a dependency (injected via constructor) has the downside of violating principle of interface segregation and least surprise.

This is because a reader of the code that accepts IConfiguration via constructor cannot quite tell which keys the code really requires. They would need to look for all calls to IConfiguration.GetValue (or similar) to understand what configuration is required for the class.

Andrew Lock: