Archive

Archive for June, 2009

Functional Type member resolution with C#

June 11th, 2009 Simon Segal 4 comments

An interesting problem cropped up today regarding reflection and how LINQ might make for a strongly typed solution. K. Scott Allen suggested a while back to use lambdas to remove the use of ‘magic’ strings in building Fetching Intentions used in NFetchSpec and it was suggested by my colleague Mark that a similar approach be used in resolving a types property name using strongly typed expressions. Given the following class:

public class Criteria
{
    private int _pageCounter;

    public int PageCounter
    {
        get { return _pageCounter; }
        set { _pageCounter = value; }
    }

    private string _levels;

    public string Levels
    {
        get { return _levels; }
        set { _levels = value; }
    }
}

I want to dynamically resolve any property name using an Expression such as:

Expression<Func<Criteria, string>> f = c => c.Levels;

After fumbling around for a while it occurred that functional programming was yet again the simplest way to approach this.

public class PropertyNameResolver<T>
{
    public static string Resolve<TPropertyType>
        (Expression<Func<T, TPropertyType>> expression)
    {
        Func<Expression<Func<T, TPropertyType>>, string>
            prop_name_function = l =>
            {
                int posDot = l.Body.ToString().LastIndexOf(“.”) + 1;
                return l.Body.ToString().Remove(0, posDot);
            };
        var property_name =
            typeof(T).GetProperty(prop_name_function(expression)).Name;
        return property_name;
    }
}

By supplying the expression and encapsulating the function that takes the expression as an argument, we get access to the body and parameters in the expression tree built by the original lambda and when be execute the outer function we can extrapolate the property name. Here’s an example of the usage:

var pc = PropertyNameResolver<Criteria>.Resolve<int>(c => c.PageCounter);
Console.WriteLine(pc);

Functional goodness in C#.

Share/Save/Bookmark

Categories: C#, LINQ Tags: ,

Using Ruby Naming Conventions in C# code? Is that madness?

June 9th, 2009 Simon Segal 6 comments

During my journey in learning Ruby / IronRuby, I have caught myself often naming things in C# with a Ruby convention or BDD test naming regime. To be extreme here’s an example:

var customer_to_make_preferred = Get_Customer_By_Id<ICustomerMakePrefered>();

//OR METHODS LIKE THIS

public void Group_By_Orders_And_Print_To_Console_Window()
{
    //…..etc
}

I really find a benefit in greater understanding when reading code when I take this approach. Now it’s true I have been programming in C# for some 10 years and I don’t know if this kind of behaviour will see me excommunicated from the circle of brethren or not but I would like to know what others think.

Opinions please! And yes I know this is gonna be like waving a red flag in front of a raging bull :). And for the Ruby people, I am only proposing the extended underscored naming convention and nothing else – it’s all about naming and that’s it – ya basta!

Share/Save/Bookmark

Categories: C#, IronRuby, Ruby Tags: , ,

Entity Framework Repository and Fetching Strategies code update!

June 5th, 2009 Simon Segal 3 comments

It recently came to my attention that the code for this series of posts got entangled in a brain freeze mix up when I uploaded some zip files recently. Therefore the code is now located in the subversion repository.

Part 1
Part 2
Part 3
Part 4
Part 5
Part 6
Part 7
Part 8
Part 9
Part 10
Part 11

The Code is here.

Share/Save/Bookmark

Creative Commons Attribution-ShareAlike 2.5 Australia
Creative Commons Attribution-ShareAlike 2.5 Australia