Sharing my knowledge

It’s better to automate, instead of checklists

In my day to day activities I have seen many checklists like

  1. Code review checklist
  2. Source control check-in checklist
  3. Developer checklist
All these are good because it helps to reduce failures but does everyone follow these all the time???. Sometimes I(or any developer) forgot to go through the checklist due to many reasons like time constraints, lack of concentration etc… and I don’t think we should blame anyone for missing this because - “We all are humans and we tends to forget”. Only way we could reduce these mistakes is to automate!!! wherever possible. In my current project, all the aspx page should have direction(dir) attribute in the html tag as part of the localization work. As usual an email checklist for localizing an aspx page was sent to all the developers, out of that one item was to include “dir” attribute whenever they add new aspx file. Everybody followed this in the initial stages but later everybody forgot about this requirement, which caused extra hours of effort to fix it in all the pages. It could have been avoided if we had a automated process which verifies this. In order to automate one way is to write a custom MSBuild task which could verify whether a aspx file has “dir” attribute, if it doesn’t fails build (this whole idea came from http://blogs.msdn.com/b/simonince/archive/2009/07/10/enforcing-unobtrusive-javascript.aspx). If you want to learn about writing a custom MSBuild task, I suggest  - http://msdn.microsoft.com/en-us/library/t9883dzc.aspx So below the code which creates this custom MS Build task


Logging execution time using AOP

Download the source code for this tutorial from  - http://www.rajeeshcv.com/download/ProfilingSample.zip

What happens if your client complains that your application is running very slow!!! or in your load/stress testing you found that some functionalities are very slow in executing than expected. This is the time where you go for profiling the execution, to analyse the root cause of these issues.

So how we could develop a profiler, where we don’t have to wrap our normal code in a profiling code.

Before going to create the profiler, we have to decide where to put the profiled information. In this tutorial, I am making use of Log4Net as underlying layer to store this information. If you have not used Log4Net before, I suggest you to read http://www.beefycode.com/post/Log4Net-Tutorial-pt-1-Getting-Started.aspx as a starting point.

With the help of AOP (Aspect-Oriented Programming) we could do the profiling task without interrupting the actual code.

AOP is a programming paradigm in which secondary or supporting functions are isolated from the main program's business logic

Source : Wikipedia

So in order bring the AOP functionality into this application, I am going to use a third party library PostSharp  which I believe this is one of the best that is available in the market. Please download it from http://www.sharpcrafters.com/postsharp/download.

So, now we have got the basic things to start with and now let’s start coding….

Start a new solution in visual studio and add a new console application project to it. Then add the below references to the newly created project

  1. Add reference to the Log4Net.dll
  2. Add reference to PostSharp.Laos.dll and PostSharp.Public.dll (Please read http://www.sharpcrafters.com/postsharp/documentation/getting-started to get the basic installation procedure)

Next, create a new attribute class called “ProfileMethodAttribute” – this class is responsible for doing the profiling work. Make sure that you have decorated this class with “Serializable” attribute


ASP.Net MVC – Conditional rendering Partial Views with Action<T> delegate

This is an update to my previous post regarding conditional rendering partial views, in that I used the internal implementation of the Html.RenderPartail(…) method to create the Html extension. Later I found a simple way to achieve the same using Action<T> delegate

<p>Partial rendering with Action Methods</p>        
<% Html.PartialIf(this.Model.Exists, html => html.RenderPartial("MyPartialView")); %>

If you look at the “PartialIf” implementation, it is simple, cleaner than the previous technique I have mentioned in my post.

public static void PartialIf(this HtmlHelper htmlHelper, bool condition, Action<HtmlHelper> action)
    if (condition)

That’s it :)

ASP.Net MVC - Conditional rendering Partial Views

Update : Later I found a cleaner and simple approach to do the same – read this post ASP.Net MVC – Conditional rendering Partial Views with Action<T> delegate

Following my previous post about Conditional Rendering, one of my colleague asked me how to render the partial view based on a condition.

Normal way of doing this is

<p>Normal partial rendering based on condition</p>           
<% if(this.Model.Exists)
 } %>

I am not sure about any other technique for rendering partial view conditionally other than this (correct me if I am wrong :) ).

Then I thought about copying the pattern I have used in my previous post and came up with this code which could conditionally render partial views and you could use the Html extension like below, which more clean than the previous

<% Html.PartialIf(this.Model.Exists, "MyPartialView"); %>


Comet a.k.a server pushing

Have you ever thought about how chatting in Gmail works? I think it works using a programming technique called “Comet”.

What is comet programming

In web development, Comet is a neologism to describe a web application model in which a long-held HTTP request allows a web server to push data to a browser, without the browser explicitly requesting it. Comet is an umbrella term for multiple techniques for achieving this interaction. All these methods rely on features included by default in browsers, such as JavaScript, rather than on non-default plugins - – Wikipedia

Last few days, I was reading about this technology and thought about sharing the information with you all. Please find the attached sample application which demonstrates how comet works using Asp.Net (Note: Right now it works only in firefox, fixes or patches to make it work in IE/Safari.. or all the browsers in this world are welcome J )

Please download it from here - http://www.rajeeshcv.com/download/Comet.zip