Becoming a software developer – episode XIV

Becoming a software developer – episode XIV

Welcome to the fourteenth episode of my course “Becoming a software developer” in which we will configure our application by using the appsettings.json file.

All of the materials including videos and sample projects can be downloaded from here.
The source code repository is being hosted on GitHub.


 

Scope

  • Settings

Abstract

Settings

Quite often, we’re in need to somehow configure our software in a way that can be both controlled and provided as an argument directly via the source code.

Such goal can be achieved easily, by mapping the appsettings.json file into the appropriate so-called options classes. Please note, that you can use IOptions interface available in ASP.NET Core directly, however I’d like to show you an alternative way that you might find also useful.

At first, let’s define the following convention – we will have classes named XyzSettings where Xyz can be anything and the Settings will be removed for the sake of clarity.
For the starters, let’s add a very simple GeneralSettings class, beware that you can have as many properties and unique settings classes as you wish.

Within appsettings.json file, add a new section named “general” that will be mapped into the “GeneralSettings” class.

It’s time to do a little bit of the magic, create a new SettingsExtensions class containing the following code:

Add a new Autofac module:

And register it inside the ConfigureServices() method that can be found in the Startup class:

And that’d be all. From that point on, you can inject directly GeneralSettings class instance that will have properly mapped properties based on the values taken from the appsettings.json configuration file.

Next

In the next episode, we will dive into the authentication using JWT, as well as storing encrypted user passwords in our application.

1 Comment Becoming a software developer – episode XIV

  1. Pingback: Dew Drop - April 27, 2017 (#2467) - Morning Dew

Leave A Comment

Your email address will not be published. Required fields are marked *