Whilst consulting at @OnTrees we ran into the rock and hard place .NET configuration situation again. The way we use the web.config or app.config via System.Configuration hasn't really changed since .NET framework 2.0. Like most .NET developers, I want the flexibility of appSettings with the benefits of using a strongly typed class, which you traditionally get from inheriting System.Configuration.ConfigurationSection .
Basically, I want all the advantages of appSettings and ConfigurationSection without any ceremony, friction, monkey casting or magic strings. Naturally, I want to use Json in my config files not Xml, but I still want to store my config with the rest of my config in my web.config/app.config files.
At the risk of reinventing the wheel, as I know that there are dozens of NuGet packages that solve these issues, but I have one more requirement, though: I need a solution that works with web.config, app.config and Azure Service Configuration .cscfg files.
Currently, I'm working on a Web Api project deployed to Azure. Whilst developing for speed, I don't use the Azure emulator, so I need my project to look for configuration in my web.config not the .cscfg file. Again, for the same reasons my integration test project needs to look for configuration in my app.config. When running the same projects in the Azure emulator or when deployed to Azure, I need them to look for configuration in Azure's .cscfg file, giving me all the benefits Azure service configuration offers.
This is my motivation for creating DalSoft.Configuration, yes I could've extended one of the many config projects out there to work with .cscfg files, but I wanted the simplest .NET configuration that could possibly work.
How easy is it to use DalSoft.Configuration?
Turns out it is extremely trivial to use!
Create a class that inherits from DalSoft.Configuration.JsonConfig:
public class MyAppConfig : JsonConfig<MyAppConfig>
{
public string Website { get; set; }
public string DatabaseConnectionString { get; set; }
}
Add the following appSettings to your app or web.config:
<appSettings>
<add key="MyAppConfig" value="
{
'Website': 'http://dalsoft.co.uk/',
'DatabaseConnectionString': 'Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;'
}" />
</appSettings>
Now you can access your configuration by simply calling GetSettings() on the class you just created, for example:
MyAppConfig.GetSettings();
MyAppConfig.GetSettings().Website; will return "http://dalsoft.co.uk/"
Where can I find DalSoft.Configuration?
You guessed right?
GitHubMore examples, documentation and source.
https://github.com/DalSoft/DalSoft.ConfigurationNuGet
PM> Install-Package DalSoft.Configuration