Zeeshan Hirani

  • Lazy Loading child entities in Entity Framework

    It's pretty interesting to see different implementations of lazy loading in both linq to SQL and entity framework. In linq to SQL, if you want to access the Orders collection of a particular customer, all you do is navigate to the Orders Collection Property and linq to SQL will run the SQL in the background and fetch the orders for that customer instance. The operation is pretty implicit. However in entity framework you have to explicitly tell that I intent to load the orders of the customer. The way you would do this is by calling Load on the Orders collection of the customer. If you forget to call load on the Orders Collection for the customer, you would end up with an empty collection of orders. This may be confusing and hard to troubleshoot bug and you would wonder why you have no orders for that customer when you could see orders in the database for the customer. Here is an example that illustrates this behavior.

  • Not All Lambdas can be converted to expression trees!

    It was surprising for me to discover that depending upon how you write your lambda syntax, it may not be able to convert to expression tree. If you are using a body expression in your lambda statements meaning you don't have an explicit return statement and no parentheses, than the lambda expression can be converted to expression tree. However lambda expression with statement body which requires an explicit return statement can only be converted to delegates. Expression trees are important because most providers of linq take expression trees and convert them to their domain specific language where as delegates gets compile into IL code which only Linq To Objects provider can understand. This is why it is important that your lambda statements are convertible to expression trees. Here is an example below which illustrates a lambda  statement that cannot be converted to expression tree and the error compiler raises in regards to that.

  • Different Ways of representing Lambdas

    Today I had some free time at work since my batch process was taking an hour to run. During this time, I was investigating the beauties of lambda expression. I have found lambda expressions to be very flexible and versatile. What I mean is, your lambda expression can be as long and as small as you need it. If you find that in certain portion of your code, it would be much more easier to read if there is less noise from language, than you can use type inference. Places where you feel the code would be easier to read if there is more explicit types declared, should have proper parentheses and return statement than by all means using statement syntax  form of lambda expression. If you find the lambda expressions getting polluted by too much code and thus making it harder to read than by all means point the lambda expression to a method that does all the complicated logic. Here are different ways I have been able to express lambda statements.

  • Compiling Extension Methods on .net 2.0 runtime

    Today, I had to work on a legacy .net 2.0 application and I found a neat little trick one of developers did to get extension methods to work on .net 2.0 runtime. I was pretty amazed because I thought extension methods was a feature available in System.Core assembly that is part of .net 3.5 framework.  In reality that is completely true and Extensions are available only in .net 3.5. However extension methods gets converted into the same IL code that is compatible with .net 2.0 runtime. So the only thing left is to satisfy the Visual Studio compiler that extension methods are valid statements for .net 2.0. On asking the developer who wrote the code, he said that in Visual studio .net 2.0 compiler only checks for an attribute called System.Runtime.CompilerService.ExtensionAttribute to be available for extension methods to work. So he basically added a dummy class of that type that inherits from Attribute class. After adding the ExtensionAttribute class, the compiler was satisfied and the project build fine.

  • Design Time Compilation in Vs 2008 Sp1

    I have been playing around with Vs 2008 sp1 bits and one of the very cool debugging improvements I have noticed is, you don't have to compile the project to see compilation errors. By default C# compiler would put squiggly lines below the code that is causing the problems and when you hover over the squiggly lines it will show the exact error that you would get if you were to compile the project. I think this feature offers a tremendous developer productivity. I cant imagine how many times have I actually compiled the project to see what error the compiler gives me. But now with this feature I will save so much time. All I have to do is hover over the lines that is causing the problem I will get the same error as if I was compiling the project. Hats off to C# team in pulling off this feature. Screen shot below shows this feature in action.

  • Paste XML as Element

    XML in Vb 9.0 has become a first class citizen. You can copy XML content and assign it to Xelment variable and compiler will not complain at all. You can even get intellisence on the XML in Vb if you import XML namespace. C# does not provide all these goodies. But recently when I had to do XML parsing from the data returned by Google mini, I came across this smart paste option that I had not discovered before. Basically you can copy some XML content, go to view menu and select paste XML as Element and it will generate all the XML element code needed to create that markup. This gives you a good start in building up the XML data that you desire. Below is a screen shot that outlines the code generated when I selected Paste XML as Elements in Visual Studio.

  • Source Outline PowerToy For Visual Studio

    I recently came across this power toy add in For Vs 2008 on CodePlex and I must say that its powerful and really makes navigating across methods, searching for methods fairly non trivial. Once you install PowerToy on your machine, you can open it by going to View Menu, Other Windows and clicking Source Outliner Power Toy. This will open up Source outliner as you can see in the screen shot. If you are on a class, that class will be selected by default on Source Outliner and by clicking up and down arrows you can see Visual studio moving the code editor to that method without a breeze. You also have the ability to search and Filter for a method in a class as well entire solution and Outliner will start populating the list as you type. I think Source Outliner is a great tool to navigate and find methods quickly. You can download the tool at

  • Finding All references of a method in Vs 2008

    Once again I am sharing some of the productivity features i have been discovering as I am starting to play more and more with Vs 2008. Not sure if this feature that i am about to discuss was already there is vs 2005 or is simply new to vs 2008. Anyway, in a large code base when I are modifying a method, I am always curious to know how many places in the code this method is being used and to be able to breeze through all the references for the method and get an idea of the context its being used. Pressing SHIFT + F12 will open up reference list window that show you all the reference links where this method is being called. Simply pressing F8 would allow you to navigate to each reference of the method in the list.  Below is a screen shot that shows the feature.

  • Code Definition Window in VS 2008

    Once again I came across a hidden gem inside vs 2008 called Code Definition window. It really is a slick window that lets me see the definition of the class without actually traveling or navigating to the class. Let's say you are inside of class that has method called SaveCustomer which takes customer object. Lot of the times, if I want to see the full definition of the customer class I end up going to the class, looking through it and than have to find my way back to the method I was originally trying to debug. With Code Definition window, I can stay at the method that I am interested and simply but my cursor on the Customer object parameter and in the code window I can easily see the entire definition of the class. Below is a screen shot that demonstrate this feature.

  • Auditing linq to SQL entity classes

    There are many ways to implement auditing on your entity classes. If you are not exposing linq to SQL entities to the presentation layer and prefer to use linq as a data access layer, than you can applying auditing on the linq entities in your business layer. The reason is, you have more control over the persistence and business layer is responsible for retrieving linq entities as well as saving them. However if you have considered that you can get away with using linq entities in your presentation layer because it provides partial classes where you can implement custom business logic than linq to SQL provides various easy ways to implement auditing features.