Generic extensions methods to the rescue

[note: This is a repost from my previous blogspace. My previous blogspace has been out of air for a while because of technical issues and a lot of developers were never able to read the articles.]

Extension Methods

If you don't know extension methods yet, please have a look at this blogpost of our Mr. Guthrie which tells you all about what they are.

As soon as VS2008 RTM was out I've started to play with some of the nice new features of Framework 3.5 and one feature I particularly like is Extension Methods. They give you the power to extend any class you like to your willing. One of the first classes I decided to extend is the StateBag class.

ViewState

As a web developer I just cannot count the times I wrote these sort of lines anymore:

public int MyIntProperty
{
     get
     {
         object o = ViewState["MyIntProperty"];
         return (o == null) ? 0 : (int)o;
     }
     set
     {
         ViewState["MyIntProperty"]  = value;
     }
}

public bool MyBoolProperty
{
     get
     {
         object o = ViewState["MyBoolProperty"];
         return (o == null) ? false : (bool)o;
     }
     set
     {
         ViewState["MyBoolProperty"]  = value;
     }
}

I've built dozens of custom controls and thus dozens multiplied by a lot wrote these default pattern lines.

Extension Methods to the rescue

Nowadays we can extend the StateBag. Just have a simple look at the following code:

public static class ControlUtility
{
    public static T GetValue<T>(this StateBag viewState, string key, T defaultValue)
    {
        if (string.IsNullOrEmpty(key))
            throw new ArgumentNullException("key");

        T returnValue = defaultValue;

        object viewStateValue = viewState[key];
        if (viewStateValue != null)
            returnValue = (T)Convert.ChangeType(viewStateValue, typeof(T));

        return returnValue;
    }

    public static void SetValue(this StateBag viewState, string key, object value)
    {
        if (string.IsNullOrEmpty(key))
            throw new ArgumentNullException("key");

        if (value != null)
            viewState[key] = value;
    }
}

The best value of course is in the GetValue method. The SetValue method is purely there because I think it's good practice to create symetric code. Developers expect to have a SetValue method when there is a GetValue method.

How to use it

With these very few simple lines of code I can now change my properties to this:

public int MyIntProperty
{
     get
     {
         return ViewState.GetValue<int>("MyIntProperty", 0);
     }
     set
     {
         ViewState.SetValue("MyIntProperty", value);
     }
}

public bool MyBoolProperty
{
     get
     {
         return ViewState.GetValue<bool>("MyBoolProperty", false);
     }
     set
     {
         ViewState.SetValue("MyBoolProperty", value);
     }
}

Framework 2.0

The big fun part of this, is that we can actually use these extension methods in our Framework 2.0 projects as well! That's because extension methods are a compiler feature. So with VS2008 we can simply add a reference to our Framework 3.5 extension methods library in our Framework 2.0 projects and all of a sudden we have this great new feature in our Framework 2.0 projects at our fingertips!

And that's why I think generic extension methods are way to cool!

1 Comment

  • I like extension methods too :)

    Worth pointing out that this is the only C#3/VB9 feature that is *not* entirely a compiler trick and you do need additional code to make it work in .NET 2.0 projects (as per my blog post).

Comments have been disabled for this content.