.Net Development


.Net Development02 Apr 2011 07:26 am

This is just a quick plug for a post on Jim Christopher’s blog, beefycode.com, that explains how to create a Unity Extension to support IoC Chaining in much the same way as Castle Windsor does. I am going to need this someday and don’t want to loose the link. Thanks Jim.

.Net Development24 Mar 2011 05:23 pm

This post grows out of my previous post on creating a C# equivalent to the Visual Basic With statement. I have since come up with a hand full of other lambda-accepting extension methods that do other handy things.

Improving on the With Statement

First off, the original with function can be extended to return values if need be as follows:

public static TReturn With<T,TReturn>(this T obj, Func<T, TReturn> action)
{
    return action(obj);
}

The As Method

The following As methods are very handy when you have a variable that is of one type but you need to perform some actions on it in the context of another type which the it also implements.

public static void As<T>(this object obj, Action<T> action)
{
    action((T) obj);
}
 
public static TReturn As<T,TReturn>(this object obj, Func<T, TReturn> action)
{
    return action((T) obj);
}

IfIs, the Conditional form of As

What if you are not sure if the type you have can be cast to the other type? The following IfIs methods are just like the As method but only execute the code if the cast can be made.

public static void IfIs<T>(this object obj, Action<T> action)
{
    if(obj is T)
        action((T) obj);
}
 
public static TReturn IfIs<T,TReturn>(this object obj, Func<T, TReturn> action)
{
    return obj is T ? action((T)obj) : default(TReturn);
}

Again I can see that this sort of syntactical sugar may drive some people nuts. Others may just not see the need. But for some of us, this sort of thing makes it easier to write expressive code.

.Net Development27 Dec 2010 07:13 am

I moved from Visual Basic to C# when I migrated to .Net back on ’04. I have never regretted this. C# is so much nicer a language in my opinion. One of the things I especially like about C# is its conciseness. I hate typing.

The one and only thing I missed from VB was the WITH statement. It drove me nuts that in C# for all its brevity, if I needed to do half a dozen things with Object.Child.Grandchild.GreatGrandchild, I had to write that whole thing out or put GreatGrandchild in a local variable like so:

Object.Child.Grandchild.GreatGrandchild.Property1 = 1;
Object.Child.Grandchild.GreatGrandchild.Property2 = 2;
Object.Child.Grandchild.GreatGrandchild.Property3 = 3;
Object.Child.Grandchild.GreatGrandchild.Property4 = 4;

The advent of object initializers in C# 3.0 alleviated the primary use case for the With statement. But object initializers don’t do much for the above case or any situation where you have a pre-existing instance.

I recently realized though that I can get 99% of the way there with a combination of two other C# 3.0 features, extension methods and lambda expressions. Putting the two together, I came up with the following method:

public static void With<T>(this T obj, Action<T> action)
{
    action(obj);
}

Which lets me rewrite the example code like this:

Object.Child.Grandchild.GreatGrandchild.With(x =>
    {
        x.Property1 = 1;
        x.Property2 = 2;
        x.Property3 = 3;
        x.Property4 = 4;
    });

Its not perfect and I can see that depending on your stylistic preferences this might drive you nuts, but I think its some very clean code.

Author’s Update:

I recently discovered that Anay Kamat posted a different solution on his blog a way back in ’09. His approach uses reflection and results in a different usage syntax. I recommend you take a look and use whichever approach you like best.

« Previous PageNext Page »