Why Hello There, Expression Tree

by Jon Davis 6. October 2009 20:53

I sought some enlightenment on FreeNode IRC in the C# channel regarding how to have strong property references without invoking them, including without delegate implementation.

The problem I’ve been faced with is that nasty string-based property specifier in Gemli.Data’s DataModelQuery<TModel>:

var women = DataModel<Person>.NewQuery()   
    .WhereProperty["Gender"] == Gender.Female;   
women.ForEach( woman => Hug(woman) );  

I first asked the same IRC channel what initial thoughts were on the direction Gemli was going and that stupid string-based indexer was the first thing to come up. “It looks brittle.” Agreed. This is the part that bugs me the most about Gemli.Data at the moment—well, tied, along with the lack of individual field selection and aggregate function support.

But LINQ-to-SQL doesn’t have this problem, which is interesting because LINQ-to-SQL doesn’t have any proprietary, Microsoft-alone-can-see-it under-the-covers magic about it. Yet with LINQ you get strong property references, without invoking the properties as CLR property behavior.

var women = (from woman in People   
             where woman.Gender == Gender.Female   
             select woman).ToList();   
women.ForEach( woman => Feed(woman) );  

How? LINQ-to-SQL doesn’t even execute that binary comparison in the CLR, it converts it to T-SQL.

First I had recently assumed up until today that this was a lambda expression and your CLR object that reaches LINQ’s “where” expression must surely be a class inheritor that mocks its base with no literal implementation. I mentioned this to the IRC channel, though, and was immediately told it’s done with expression trees. I was given a sample of Expression<Func<T, U>>. What? I then came across this: http://www.interact-sw.co.uk/iangblog/2005/09/30/expressiontrees  .. among a bunch of other articles and blog posts.

Looks like I still have a lot of homework this week because I’ve never looked at expression trees in C# before. Makes me feel like I’m behind, but there’s joy in knowing that I’m catching up in this and once I get caught up it’ll open up all kinds of new doors for Gemli.

(I might follow up on this blog entry inline, I just wanted to get something mentioned now since I’m heading to bed now and don’t want to lose the thought.)

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , ,


Powered by BlogEngine.NET
Theme by Mads Kristensen

About the author

Jon Davis (aka "stimpy77") has been a programmer, developer, and consultant for web and Windows software solutions professionally since 1997, with experience ranging from OS and hardware support to DHTML programming to IIS/ASP web apps to Java network programming to Visual Basic applications to C# desktop apps.
Software in all forms is also his sole hobby, whether playing PC games or tinkering with programming them. "I was playing Defender on the Commodore 64," he reminisces, "when I decided at the age of 12 or so that I want to be a computer programmer when I grow up."

Jon was previously employed as a senior .NET developer at a very well-known Internet services company whom you're more likely than not to have directly done business with. However, this blog and all of jondavis.net have no affiliation with, and are not representative of, his former employer in any way.

Contact Me 

Tag cloud


<<  May 2021  >>

View posts in large calendar