Project: Dotnet Environment Settings

Back To Projects
Word Finder

Project Background

I fundamentally disagree with the way environment variables are handled in the .NET world. JSON files per environment doesn't make sense. There is no way for a new developer on the project to know what each of the settings is, what all the possible values are, what each value's type is at runtime, where it's applicable or how it changes between environments. Environment variables should be something that the environment provides. That is why they're called environment variables.

I'm sure part of the reason .NET uses this pattern is because they've always been meant for applications running on windows. Now that this isn't the case ( I'm running all my .NET websites on Linux), there needs to be a different and better way to pull in environment variables. Every .NET project I've worked on has recognized this and has rolled their own wrapper for accessing environment variables.

We could just write a simple wrapper that reads in environment variables raw. However, pulling in "magic values" at runtime without knowing anything about them, whether there should be a default value, if it's secret vs something that just happens to be different per environment, etc etc, is a gigantic problem.

I wanted to be able to attach meta data to each value to solve all these problems. With this library you can accomplish this by decorating enum values to get the following:

  • Default value (if there is one) selectively based on environment
  • Differentiate basic environment variables from secrets
  • Define whether the value should be logged whenever environment settings are logged
  • Throw an error if the application starts up and the environment didn't have the value set in a particular environment

There are also helper methods to read in values from a .env file and log all values that are safe to log. The structure of the library also facilitates easy unit testing via an environment settings interface. You can see this project being used in dotnet-react-sandbox

Check it out here: environment-settings-dotnet.

Mike Thompson

Interested in hiring me for your project?

Looking for an experienced full-stack developer to build your web app or ship your software product? To start an initial chat use the form on the contact page.