Injectable ApplicationSetting with Autofac in .NET
Sometime that you want to create any class to wrapped the configuration that came from app/web.config this article will explain the easiest way that you can pass them via injection to any class that you want, that you can stub/mock through any interfaces, this article will explain with my chatbot application which you can imagine to use in your world!
Refactor from HelperClass to Injectable.
Almost developer will implement the static class could be helper or something like that (aka. ConfigHelper) that will read the configuration file like this;
seee it!, dependencies when you tried to the read the configuration, that mean when you want to test this function will be create the app.config on your test and more as more as you want.
Create Attributes is understandable
Then create the AppSettingAttribute that will place on properties with [AppSetting(…)] and create the reader this is main function to read the value from the configuration file
Create the model for any specific configuration
this sample model is for get the AppId and AppPassword that I registered my bot to the bot Skype framework.
Register to container with Autofac
Autofac is most of framework that I worked on DI, I registered the reader with configuration then use it to Load() configuration per configuration model that you want, I registered my bot configuration with IBotConfiguration and I this for any class con-structure that autofac have resolved