Archive for the ‘.net’ Category

Code Generation in .Net

July 3, 2009

When developers think of code generation they think generally of ORM tools like LinqToSql or Entity Framework. My feelings on these technologies are putting it midly, “crap”.

I was listening to an interesting podcast this morning on DotNetRocks with Peter Vogel (http://www.dotnetrocks.com/default.aspx?showNum=453) in which he explains other uses of code generation in .Net.

An interesting point he made (and which I think is very applicable) was avoiding to write redundant code. He was not referring to ORM but rather other functions like reading information from a web.config or app.config file. The example he gave was generating code for connection string settings in app.config file. Every time he saves an app.config file, his code generation tool kicks off and generates a ConfigurationManager class which generated code to the settings in the config file.

An example is having multiple connection strings in the config file. For instance, DevConnectionString and StagingConnectionString. His code generation tool will then allow him to access these properties using type safety and early binding. Thus, in code, he can call ConfigurationManager.ConnectionStrings.DevConnectionString or ConnectionStrings.StagingConnectionString and thus avoiding the annoying problems of miss spelling the connection string.

Maybe something to take a look at and invest in. He mentioned a variety of code generation tools in .Net including T4.

Advertisements

.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

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