.Net 3.5 Extension Methods

June 30, 2009

Since the “dawn” of .Net 2.0 and 3.5 a while ago, life in programming has become significantly easier. The new language features have helped me in my philosophy of creating cleaner code.

An example of this has been using extension methods. In the old days you used to have a Utility library which had generic functions to help abstract various operations.

Once such operation (and which is widely used) is casting a string value (from some source) to a specified type (like int or double etc).

Old Way:

public class Utils {
public static int SafeInt(string val)
{
try
{
return (int)val;
}
catch (Exception ex)
{
//log error to error handler
ErrorGateway.LogError(ex.Message, ex);
//possible rethrow of exception
}
return 0;
}
}


You would have had to write one method for each type you want to cast. You would call this function using something like:

string cast = “123”;
int ret= Utilities.Utils.SafeInt(cast);


With the new .Net 3.5 you can rewrite it using the following method:

public static T SafeType(this object val)
{
try
{
var tempValue = Convert.ChangeType(val, typeof(T));
var value = (T)tempValue;
return value;

}
catch (Exception ex)
{
ErrorGateway.LogError(ex.Message, ex);
}
return default(T);
}

This is a form of an extension method in .Net 3.5. This provides numerous advantages:

  • Allows re-use of this method with any type. This of course leads to cleaner code.
  • Provides an additional method to a sealed type which makes calling easier.

Example usage:

string cast = “123”;
int ret = cast.SafeType();



This is just one example of how useful these new features can be as this reduces code and ease of use.

Hope this has been helpful.

–Lee

Enterprise Integration Patterns

June 30, 2009

Recently I have started working with Enterprise Integration at work. Although I have worked in the past with this, I have discovered a book by Martin Fowler called Enterprise Integration Patterns which covers Integration Patterns. This book has altered my view on integration and in software development in general.

This book can be found on his website at
http://www.martinfowler.com/books.html#eip

Anyone working in this field should read this book if they have not done so already. These types of books should be made as course material in any university or tertiary institute as this explains design problems and is technology agnostic.

In upcoming posts I will discuss some concepts and implementations which I find really interesting and has already helped me out of some serious issues.

Hello to the community

June 30, 2009

Hello fellow coders,

First blog, and obligated to introduce myself 😉

I am a .Net developer/architect/designer/etc and have worked at various types of industries including windows development, web development, enterprise integration and SharePoint. I love the Microsoft stack and will very rarely move away from it. I have recently started working with Messaging Systems and must say that I think I have found a passion for it.

I hope my posts will be helpful and please feel free to comment and give some ideas.

–Lee