TopMenu

Beginners guide-Expression Trees in Csharp

Expressions and Func & Action are generic classes and often developers gets confused with them. E.g. which one to use and when. As most of C# developer are familiar with LINQ (Language Integrated Query) so they’re familiar with Lambda Expressions. In this post we’ll not be discussing Func<> and Action<>, Long time back I already wrote a post about LambdaExpression and Delegate templates.
We’ll be focusing on How Expressions is different from Delegate templates and what benefits you can get by learning and getting familiar with it.

Expression Tree

Expression and Expression<> are basically classes that can represent the CSharp code as Data. Really? Yeah really. Unlike Func<> or Action<> Expressions are non-compiled Data about the code. Most of LINQ Providers has been built using Expressions. Let’s get started with a sample Expression and try to understand it.

Walkthrough of a sample expression

Expression can be parsed, analyzed in the program. Have you wondered how Linq to Sql works? How those Linq statements get converted to Sql? How Linq is a deffered execution? Let’s not get into complex example. Let start with “Show me with Code” stuff:

Let’s create a simple Expression:

// A simple delegated operation which perform string join.

Func<string, string, string> StringJoin = (str1, str2) => string.Concat(str1, str2);

Now I want to parse it analyze it or may be doing some more but for that I need to treat this code as Data. Now let’s write an Expression for the above lambda expression:

Expression<Func<string, string, string>> StringJoinExpr = (str1, str2) => string.Concat(str1, str2);

The Expression Tree can be visualize as with it’s major properties:
clip_image002

Let’s analyze our Expression according to above figure, and see what values are populated to understand the break of Expression statement:

clip_image002[11]

So it’s parsed like a tree with NodeType, Left, Right and Body. Which means it can be written as:

Parameters(Left) -> (Root)NodeType -> (Right)Body

Now we have the data, so one can perform operation based on its values. That’s how Linq2Sql works. It analyzes the Expression, Convert the Expression Tree to SQL statements and prepare the query. Expression are major building blocks of any Linq providers.

Compile and Invoke the Expression

Unlike Func<> and Action<> expression are not compile time unit. They need to be compiled before executing the underlying instruction. So to execute the our Expression we must first compile it.

var func = StringJoinExpr.Compile();
 
var result = func("Smith", "Jones");

or you can write in single statement like:

var result = StringJoinExpr.Compile()("Smith", "Jones");

So we have used Expression to represent a Lambda and it’s body. We looked into the details of how it’s represented as Expression Tree. And how to compile and invoke the code represented by Expression tree.

In the next post we’ll see how to create a custom expression. And how you can build a Dynamic Linq query on the fly?

Attach process at runtime for debugging in Visual Studio

With Integration tests a common scenario comes into picture. Integration tests always depend on system components e.g. Database, Services etc. If the Integration test is failing un-expectedly then it might require to debug the test including that component which are usually part of the same VS solution. Recently I came across this SO question Question seemed legit because while debugging a test, attaching a process again and again to debug becomes really annoying.
To make dependent components like services, I usually invoke the process to launch e.g. ConsoleApplication project in same solution. Its like simply Add a helper class to invoke the process.

Now in the TestInitialize and TestCleanup methods (I’m using NUnit in this sample code so it would be TestFixtureSetup and TextFixtureTearDown methods) I would start the process and kill the respective process. Here the Integration test is a sample from one of my Github project WCFDynamicProxy. In this Test I’ll be invoking a Dummy service instance hosted in a Console application. Then completion of test just kill the process.

Now, Comes the part to attach this process for debugging. This is pretty tricky one. I found an VS Addin from Visual studio team to attach child process automatically to current debugger but it seems it only works with "F5" debugging.

Then I found this SO post and It really worked amazingly. I have done a little customization to get the process Id as argument:

Note: You need to add the the VS automation library EnvDTE from AddReference -> Extentions. Also the VS version might be different. I was using Visual Studio 2013 so it’s ‘VisualStudio.DTE.12.0’. Change it as per your need.

Now in the ProcessInvoker class Add the call to AttachDebugger utility class after the process launch statement.

When I launched a test for debugging it worked like charm. The process was invoked, attached to VS and was able to debug other process code. You can verfiy it from menu –> Debug –> Attach to process –> Locate the process expected to be attached.

Checkout the working code here. specially WcfDynamicProxy.Tests in the solution. If you like this post please share, like to support this blog.

Let’s Try Again!! RetryPolicy using Microsoft Practices Enterprise Library

In today’s software development world almost all applications are connected applications. In some or many ways they’re connected to network based services. Internet is a one of such network and which sometimes is not reliable due to slow connections, timeouts and other connection un-availability issues. This is most important for any application to handle such situations in a graceful manner. Means these things happens and are mend to happen so how application should response to these scenarios without reducing “Reliability” factor.

An example scenario
Let’s take an example of an application which displays some information from an RSS/Atom feed. Now once in a while RSS/Atom host server become un-reachable for a short span of time (Say 10-20 sec.) due to above mentioned reasons. We have couple of options to deal with such situations, Try reading from feed and when there’s an error show the user that feed is not available and try again. User tries again and application successfully reads the feed and displays the information to user. Or Application can retry the fetch operation couple of times and when it’s available displays the information to the User without any user interaction. This depends on business scenario but it increases the Reliability.


A high level view of Retry mechanism

When to apply the Retry mechanism?
In most scenarios at least important read operation can be marked to engage the server with Retry operation for specific errors like network timeout, connection un-reachable etc. Another example that I assume everybody have seen while downloading something from Internet, and connection goes down, browser waits and retry sometime before actually aborting the download.

Implementation
To introduce this behavior I preferred TrasientFaultHandling from Microsoft Practice Enterprise library. It’s available as individual nuget package so you won’t need to install the whole Enterprise Library monster project. Although you can use any other utility which provides Retry mechanism or implement your own. IMO if something is already there, well tested and optimized then why wasting time re-inventing the wheel.

Installing Nuget package
Add the nuget package TransientFaultHandling.Core in your project to have the required library classes and method available for usage.

Implementing Transient Fault detection policy/strategy
Transient fault/exception are identified as temporary errors and probably can be fixed by retrying the operation. To introduce such types of fault/exception we need to implement the policy/strategy by implementing the interface ITransientErrorDetectionStrategy.

A straight forward usage of Retry utility -
Code Explanation –
The RetryPolicy is a class from TransientFaultHandling namespace.

this.retryPolicy = new RetryPolicy<DownloadFeedTrasientErrorDetectionStrategy>(new Incremental(3, TimeSpan.FromSeconds(0), TimeSpan.FromSeconds(1.5)) { FastFirstRetry = true });

The above snippet is interesting one. Here we’ve used the Generic constructor of RetryPolicy. The class Incremental is RetryStrategy which takes similar arguments as defined in RetryPolicy class constructor.

this.retryPolicy.ExecuteAction(() => this.DownloadFeed(feed_source));

The ablove line shows how the actual network based method call is wrapper with Retry logic. It takes simple void Action delegate which is represented as LambdaExpression.

This is a simplest usage of RetryPolicy. Really easy isn’t it? But this all you need to do to introduce Retry stuff in your application. Who else is using – Entity Framework, Windows Azure service and many other important products is using this library.

Year 2015 in review


A new journey started in 2015

There’s a whole new journey started and a lot happened in start of year 2015. Last year in Feb, 2015 I got married which is a important aspect of life. Now I have a great understanding, supporting wife as my family. Then in April, 2015 I moved to USA for a long term Assignment. So far I’m learning culture of USA and trying to connect with people in various ways(Friends in Office, Local UGs and online friends from Facebook/Twitter/Linkedin).

Coming to USA also gave me an opprotunity to meet my friends here. I met one of my old friend Chris Ergle with Mahesh and Menish during our c-sharpcorner chapter meet in NY. Later I got a chance to hangout with Jonathan J. Tower during his 48states project travel in NY. Later in 2015 I got a chance to visit Philadelphia for Azure cloud roadshow where I met Mahesh chand and many other people from Microsoft. Specially, I met first time with Jeff Fritz. Most of all me and wife got invitation from Mahesh to visit his house in Philly and it was first time I visited a friend’s home in USA. Leaving professional life behind we spent a great weekend together.

Community contributions
Here is a glimpse of community contribution which I made via blogs/forums/github.
- Blogging
I wrote total 21 posts including a series on securing WCF Http Rest services tutorial. Here is the blog posts list:

- QnA Technical forums
I was contributing on stackoverflow passively in past years but last year(2015) I actively participated in reviewing and answering questions on miscelleneous topics. Here are few stats:
Answers(in 2015)- ~200
Reputation earned(in 2015) - ~2500


- Miscellenous
I also contribute actively on c-sharpcorner for articles, blogs, news and forums. Here’s my c-sharpcorner profile. Including contributions on MSDN, I have also started participating in open source contribution on Github and worked on couple of repositories. One of them is an open source library WCFHttpBehaviorExtension which is available via Nuget package.

Awards
First day of 2016, I received email from C-sharpcorner that I have been awarded as Mindcracker MVP 4th time in a row. This was a great news to start a new year and another reason to stay motivated.

What Next (year 2016)
I will be continuing practicing on DDD, CQS and CQRS. These are the topics that I started with basics by reading from various sources including books, articles, session recording and google groups email listings. I have 3-4 unfinished reading of books from great authors(Eric Evans, Vaughn Vernon, Martin Fowler) that I chose to finish this year.
Another resolutions is to get expertise in a already known programming language(May be JavaScript) and learning basic of a new programming laguage(May be python or similar Functional language).
I’ll continue writing and sharing my thoughts and findings on technology here on my blog and on c-sharpcorner.
May be this year I’ll start writing a book (which is a great deal as you need a strict routine to follow to get steady progress).
Good luck everyone and my readers for year 2016. Wish everyone gets the best out of it.

T4 Templates to Generate classes from XML

Today I came across an issue in a UI Automation project where the Automation devleoper has to use some elements which were references to UI elements from an XML. Let’s see how the XML was looking like:
image_thumb[27]
Now this XML has tag <element> which has to be used in helper methods to find elements. it was something like below:
image_thumb[23]
The problem was that this type of lines were scattered across the project. I found this issue during the codereview and decided to find an approach how to get rid of such problem and then comes Ideas in my mind…

T4 Templates to rescue

I had this notion of T4 templates saw and did some fixes in EF generated TT templates but I have never create a template from scratch. To create a template from scratch its always handly to use tools like T4 editor and few more custom utilities which makes the life easy.
I knew about Tangible T4 template extension of Visual studio 2013. This extension have a free version which gives you more than you wanted features, but ofcourse there are many benefits to have pro licensed as well. But this was enough for me so what I got with Tengible T4 template.
The Editor for syntax highlighting :
image_thumb[4]
(Figure. 1)

This was the only feature I was looking for. But there was more A few VS File templates were also installed and couple of them were having sample code as well to get started which was great.

I used the Advanced T4 template to add new Template file which has generated a sample template like shown in Figure. 1
Now I had to change it to give a custom implementation so that I can get desired classes. Because the Editor gave a lot of help and everything was visible so it was easy to differentiate the local code and template code. So all I had to do was read my XML file iterate through it and genrate desired result as classes and constant fields.
Below is the complete code that I used:

image_thumb[11]
I have attached the screenshot and highlighted the code which would get generated. It’s really as simple as it seems. You can debug the code easily by placing a Debug breakpoint any where in the code and Right click on the Template and Select “Debug Template”
image_thumb[7]
With Tangible T4 template it allows to compile and run the T4 when you press Ctrl+S key(Save document). That’s it.
Here are the results that I generated using it:
image_thumb[16]
What we have achieved using it? Well see for yourself:
image_thumb[20]
For now everybody was happy. But this is not it, Next I’m gonna use T4 to generate Enums from values in Domain tables from database. Hope this would help someone looking for something like this.