Jul 23 2008

Using Lambdas To Write A Faster Factory

Category: .Net FrameworkAlexRobson @ 00:07

A while back, Scott Hanselman started a series of posts about leveraging lambdas as anonymous functions for a factory pattern in his BabySmash program. Recently I had to refactor a large portion of the persistence layer and realized that I needed a factory that would create SqlParameters with the correct SqlDbType given different sets of circumstances. First the example factory code:

   1:  private Dictionary<Type, Func<string, object, SqlParameter>> _parameterDictionary =
   2:              new Dictionary<Type, Func<string, object, SqlParameter>>
   3:                  {
   4:                      {
   5:                          typeof (short), (name, value) =>
   6:                                            {
   7:                                                SqlParameter p = new SqlParameter(name, value);
   8:                                                p.SqlDbType = SqlDbType.TinyInt;
   9:                                                return p;
  10:                                            }
  11:                      },
  12:                      {
  13:                          typeof (int), (name, value) =>
  14:                                            {
  15:                                                SqlParameter p = new SqlParameter(name, value);
  16:                                                p.SqlDbType = SqlDbType.Int;
  17:                                                return p;
  18:                                            }
  19:                      },
  20:                      {
  21:                          typeof (long), (name, value) =>
  22:                                            {
  23:                                                SqlParameter p = new SqlParameter(name, value);
  24:                                                p.SqlDbType = SqlDbType.BigInt;
  25:                                                return p;
  26:                                            }
  27:                      }
  28:  }


Obviously, in the actual production version there's a lot more to it, but that should give enough of an example. So let me attempt to explain what's going on incase that's not intuitive. The dictionary is a key-value pair where the key is the .Net type for which the factory is creating the parameter and the value is a delegate with the method signature (string, object) and returns a SqlParameter instance. The reason for all the crazy brackets is that I'm actually using lambdas as anonymous functions to provide the implementation for the delegate defined as Func<string, object, SqlParameter>. This means that, at runtime, I can use the dictionary in the following way:

string parameterName = "@howMuchIsTooMuch";
string parameterValue = "eleventy billion";
SqlParameter parameter = _parameterDictionary[parameterValue.GetType()].Invoke(parameterName, parameterValue);


Why a dictionary? Well, it's actually more performant to allow the type to act as a key than to write switch or complex if/else routines which is part of what Hanselman was avoiding in his post. I hope this implementation of Hanselman's approach leads to other fun refactoring.

 

Tags: ,

Comments

1.
trackback DotNetKicks.com says:

Trackback from DotNetKicks.com

Using Lambdas To Write A Faster Factory

2.
Scott Hanselman Scott Hanselman United States says:

Interesting. Since you're only changing the one line, the SqlDbType, perhaps you could have used a Dictionary<Type,SqlDbType> and skipped the whole lambda thing?

3.
AlexRobson AlexRobson United States says:

Wow. I need to check my comments more often : ) This is a watered down version of some things I'm doing in production, but that's a very good point. I'm actually doing something like you're suggesting with dictionaries of Dictionary<Type, SqlDbType> and Dictionary<Type, string> to correlate between both the SqlDbType and the T-SQL declaration equivalent for some code I have the creates T-SQL on the fly for my persistence layer.

Thanks for the feedback though. Pretty awesome that you stopped by assuming it's not someone using Scott Hanselman's name :p

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading