Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

T4MVC for Web.config <appSettings>

The good thing about T4MVC is that it allows you to get rid of literal/magic strings.

T4MVC is a T4 template for ASP.NET MVC apps that creates strongly typed helpers that eliminate the use of literal strings when referring the controllers, actions and views.

I'm just wondering if it'd be possible to have something similar when it comes to app settings inside the Web.config file:

<appSettings>
    <add key="SecurityGuardEmailFrom" value="[email protected]" />
    <add key="PasswordExpiresInDays" value="1" />
    <add key="NumberOfPasswordsToKeep" value="5" />
</appSettings>

So, instead of this:

private static readonly int PasswordExpiresInDays =
int.Parse(ConfigurationManager.AppSettings["PasswordExpiresInDays"]);

We'd have something like this:

MVC.Webconfig.PasswordExpiresInDays

or

MVC.AppSettings.PasswordExpiresInDays

This would help in compile time to check if the app setting is still there avoiding runtime errors.

Would this be even viable? If yes, do you know if there's already something similar to this implemented somewhere?

like image 205
Leniel Maccaferri Avatar asked Jan 16 '23 14:01

Leniel Maccaferri


1 Answers

Well, interesting enough, after I posted the question I Googled with different words and found something that got there: a T4 template that has nice error handling built-in... Here's the post:

T4 Template For AppSettings Access In Config Files

I had to change the T4 template code provided in the post to make it work in my current environment (VS 11 Beta + ASP.NET MVC 4 app).

How to use?

Download the AppSettings.tt T4 template file and place it in the root of your ASP.NET MVC for example. Include the file in your project and right-click it and select Run Custom Tool and you're done. A new class called AppSettings will be available with all your app settings. AWESOME! :)

These are the imports I'm using:

<#@ template language="C#" debug="true" hostspecific="true" #>
<#@ output extension=".cs" #>
<#@ Assembly Name="System.Core.dll" #>
<#@ assembly name="EnvDTE" #>
<#@ Assembly name="System.Configuration"#>
<#@ import namespace="System" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Diagnostics" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Collections" #>
<#@ import namespace="System.Collections.Specialized"#>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Configuration" #>

Some of the app settings in Web.config have a colon ( : ) in their names such as webpages:Version. This is what I did to make it work:

public static string  <#=setting.Replace(":", "")#>
{
    get
    {
        return getConfigSetting("<#=setting#>");
    }
}

Note the setting.Replace above.

If you wish you can also debug the T4 template. Just follow the steps described here:

Tiny Happy Features #1 - T4 Template Debugging in Visual Studio 2012

like image 181
Leniel Maccaferri Avatar answered Jan 25 '23 19:01

Leniel Maccaferri