Last time I presented a factory for quickly creating IConfiguration objects from well known locations, like the app.config file. I also claimed that my approach was flexible enough to be extended without having to change any of the original factory code. Let’s see if I can demonstrate that, shall we?
This time I’ll demonstrate two extension methods on the IConfigurationFactory type that make it easy to read from JSON files. The first, named FromJson, promptly violates the design convention that I proposed in my previous article, because it includes a parameter for naming the JSON file to read from. I have to laugh at that because it shows how conventions always need to remain flexible in order to deal with real world demands. So, now we allow parameters in our IConfigurationFactory extension methods *cough*.
Anyway, the second method is named FromAppSettings and it doesn’t require a parameter, since we’ll assume the app settings file is always going to be named, well, appsettings.json. So, see? My convention does hold … Some of the time … *whistles*
Let’s look at the source code for the extension methods now:
public static class IConfigurationFactoryExtensions
public static IConfiguration FromJson(
this IConfigurationFactory factory,
var localConfiguration = factory.FromAppConfig();
var builder = new ConfigurationBuilder();
var jsonConfiguration = builder.Build();
public static IConfiguration FromAppSettings(
this IConfigurationFactory factory
var fileName = Path.Combine(
var jsonConfiguration = factory.FromJson(fileName);
var localConfiguration = factory.FromAppConfig();
The first thing we do in FromJson is read an IConfiguration object from the local app.config using the FromAppConfig method we covered in the previous article. The reason I do that is because I want all these additional factory methods to build on the local configuration (think, app.config) and include a copy of whatever is in the local app.config file with whatever else I return, no matter what. That prevents callers from having to make multiple factory calls in order to get a complete IConfiguration object – one that includes all configuration settings from all (standard) sources. At least, that’s my thinking. So, for this method, we’ll combine the IConfiguration from FromAppConfig with the IConfiguration we’ll create for the JSON file, and return both objects as a single IConfiguration instance.
The next thing we do is create a ConfigurationBuilder object. Then we call the AddJsonFile extension method, which is part of the Microsoft.Extensions.Configuration.Json namespace, to tell the builder that we want to read the JSON file as our configuration source. We also call the AddConfiguration extension method to add the IConfiguration we got from our FromAppConfig call. So, when we call the builder’s Build method, which we do next, it will read the JSON and combine it with our existing IConfiguration, and return a single, combined IConfiguration object. Then we simply return that.
The FromAppSettings extension method leverages everything I just covered in the FromJson method to return whatever is in the appsettings.json file. It starts by building a path to the file. Then it checks to see if the file exists, or not. If not, it simply returns the local IConfiguration object we get from the FromAppConfig call. If the file does exists then it return the IConfiguration from the call to FromJson, using the path we just built to the JSON file.
So, how would you use these methods in your project? Here’s a quick example:
IConfiguration cfg = ConfigurationFactory.Instance.FromAppSettings();
// Now use the cfg for, well, configuration stuff ...
It doesn’t get much easier than that.
I know what you’re thinking, what about change tokens and any other features not required by this nice little happy path example I’ve included. Well, I’ll no doubt be adding those things to these extension methods as I run across a need for them. I rarely, if ever, try to predict what future features I’ll need next year and include them now. As I’ve mentioned before, my ability to see into the future is pretty bad. But, keep checking back if there’s something you need, I may have added it by the time you read this article.
So what’s next? Well, I use the ability to easily read from either app.config or appsettings is nice, and certainly makes life easier, but, I use some conventions when I configure my applications and services and next time I’ll demonstrate how I go about using the configuration of my application to decide what the configuration of my application should be …
It will make more sense after the next article. I hope. :o)
See ya then!
The code for this article is taken from my CG.Configuration.Json NUGET package, which can both be downloaded for free at: https://github.com/CodeGator/CG.Configuration.Json
The source code for the CG.Configuration.Json project live on Github and can be obtained for free at:
Photo by Lorenzo Cafaro from Pexels