Creating strongly typed wrappers around untyped dictionaries in ASP.NET using the Castle DictionaryAdapter

Often when writing web applications you find yourself writing strongly typed wrapper classes around untyped dictionaries, such as Session, QueryString or even Web.Config Application Settings.

This is quite a tedious task, but using the Castle DictionaryAdapter this is all wrapped up nicely, and all you need to do is create an interface.

Download source

Lets say we have the following values in our Web.Config Application Settings.

<appSettings>
    <add key="ApplicationId" value="123" />
    <add key="CallbackUrl" value="http://www.someurl.com" />
    <add key="IsProduction" value="false" />
</appSettings>

You may yourself writing a class that looks something like this.

public static class ApplicationSettings
{
    public static int ApplicationId
    {
        get
        {
            var returnValue = 0;
            int.TryParse(ConfigurationManager.AppSettings["ApplicationId"], out returnValue);
            return returnValue;
        }
    }

     public static string CallbackUrl
     {
         get
         {
             return ConfigurationManager.AppSettings["CallbackUrl"];
         }
     }

     public static bool IsProduction
     {
         get
         {
             var isProduction = false;
             bool.TryParse(ConfigurationManager.AppSettings["IsProduction"], out isProduction);
             return isProduction;
         }
     }
}

Writing methods like this and dealing with casting to the correct data type in quite time consuming.

Now let’s use the Castle DictionaryAdapter to do the same thing. Using NuGet add a libary reference to Castle.Core which is where the DictionaryAdapter lives.

All I need to do it create an interface that maps to my Application Settings.

public interface IApplicationSettings
{
    int ApplicationId { get; }
    string CallbackUrl { get; }
    bool IsProduction { get; }
}

I don’t have setters as you don’t want to be trying to set values in the Web.Config, but if you were using Session for example, you could get and set values in the dictionary.

Now to get a concrete object I can work with I need to use the DictionaryAdapterFactory.

var applicationSettings =
    new DictionaryAdapterFactory().GetAdapter<IApplicationSettings>(ConfigurationManager.AppSettings);

If I inspect the object you can see that it has the properties from my interface containing the values in the Web.Config.

This is all pretty cool, but what I really like about it is that I can use it with a DI container.

I’ve added Autofac and the Autofac MVC3 integration library to my project. You can find the information on how to set up Autofac for MVC3 here.

I can register the IApplicationSettings interface to resolve using the DictionaryAdapter by doing the following.

builder.Register(
    p => new DictionaryAdapterFactory().GetAdapter<IApplicationSettings>(ConfigurationManager.AppSettings)).SingleInstance();

Now any time I inject IApplicationSettings it uses the object wrapped around the Web.Config settings.

public class HomeController : Controller
{
    private readonly IApplicationSettings _applicationSettings;

    public HomeController(IApplicationSettings applicationSettings)
    {
        _applicationSettings = applicationSettings;
    }

    public ActionResult Index()
    {
        ViewBag.Message = "Welcome to ASP.NET MVC!";

        return View(_applicationSettings);
    }
}

The great thing about this is if that down the line I want to move my application settings elsewhere, such as in a database, I can create a new implementation of IApplicationSettings, change my registry and that’s it.

Download source

Posted on by Joe in ASP.NET, C#, IoC, MVC

Add a Comment