Friday, April 23, 2010

Converting Expression Tree

In this articles, I'm going to divide my post into 3 sections: Problem, Design, Solution.
  • In Problem, I will describe about the problem that I got in my implementaion
  • In Design, I will show all method that I used in my implementation
  • Finally, I will show you my solution that I used for finishing my this article.
Now, I start with Problem section. In my solution, I built some entity and in the Facade layer I wanted to export my entity to UI layer, but in Design Principle, we shouldn't expose my entity to UI layer. We can figure out it same with the shell, with my Facade layer is a shell of application. And now, my problem appeared, if I try to expose the service with Expression Tree (maybe Expression<...<Entity>>) is a parametter. At here, I just want to expose the service with Expression<...<DTO>>. So, how do I do that? Maybe I must to convert the Expression Tree from my entity to my DTO (Data Transfer Object)

That is my problem, and now I will show you about my Design for solving it. The first thing, I can do for now is introduction about Expression Tree (ET) in .NET. Do you know about ET?
"In general, expression trees are special kind of binary trees. Binary tree is a tree in which all nodes contain zero, one or two children. Why expression trees have been implemented as binary trees? Mainly because binary trees allows you to quickly find what you are looking for. The upper limit of steps necessary to find required information in binary tree equals to log2N, where N denotes the number of all nodes in a tree."
Do you know the difference between Lambda Expression (LE) with the Expression Tree? All of people didn't know about it, so I will show you now.

LE is a wrapper of delegate function in .NET 2.0. And in the .NET 3.0 above, we wrap it with Func, Action, Predicate key word. So you can call the delegate function so easy and readable. Now we change to ET, as you saw in the definition above, did it make sense for you? ET is only a Data Structure contains the LE. You can figure out that you must transfer your object through the wire, and this is the place that ET participate in. Because ET is a Data Struture so when you transfered it to the UI layer, you must compile it into the LE and call LE to executing it. I hope you clear about this procedure. And the last thing I want to show you: "only using the ET when you want to transfer your object through the wire, maybe SOA architecture"


That definition about ET & LE is so far, I will turn back now. So what Solution will I use to make it working in my code? In my Application layer, I used AutoMapper for mapping my entity with my DTO, and all thing it worked normally. Only one thing, the ET wasn't converted.And this was a place that I must to working.

Go go, I start to introducing my code for you. My entities design is























And my entity code is


































The DTO's code is

















Now I must to writing some code for my Repository class.


















And in the implementation for Repository is





















Finally, I implemented the Application layer is




















Do you see the line: var lambda = LambdaExpressionHelper.Convert(condition, identifyColumn);
That is my work, we are to divide the helper into 3 classification:
  • x => x.Name == "name"
  • x => x.Id == Guid.NewGuid()
  • x => x.CreatedDate.CompareTo(DateTime.Now) == 0
So I will show you the detail of it at here:





























































And the finally, we shall wrap it into a helper function at here:

















In the unit testing, I executed unit testing for this and the result is:


























IMHO, Maybe have many method for this convert, but It's my work, and I really appreciate to anyone who comment for improving my post. Thanks!

You can reference to http://geekswithblogs.net/Martinez/articles/understanding-expression-trees.aspx for clear more about Expression Tree..
@Martinez: Really thank about your articles!

    2 comments:

    1. Hi there Thangchung,

      I was interested to know if you are still using the methods described in this article.

      Expression conversion has been something that I've been scratching my head about and search for for ages but haven't really found much out there.

      Are you writing your custom code or have you found a plugin that you can direct me to?

      Or approach and best practice based upon your experience using these methods in a SOA app.

      Thanks and best regards,

      Hugh

      ReplyDelete
    2. Thank you very much! Your article is what I was looking for long time!

      ReplyDelete