سؤال

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="info@email.net" />
    <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?

هل كانت مفيدة؟

المحلول

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

نصائح أخرى

Create a custom configuration section instead of putting your settings in <appSettings>.

http://msdn.microsoft.com/en-us/library/2tw134k3.aspx

You can extend ASP.NET configuration settings with XML configuration elements of your own. To do this, you create a custom configuration section handler. The handler must be a .NET Framework class that inherits from the System.Configuration.ConfigurationSection class. The section handler interprets and processes the settings that are defined in XML configuration elements in a specific section of a Web.config file. You can read and write these settings through the handler's properties.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top