What to do when you have generics

August 9, 2007 15:13 by Rob

So we upgrade a product to .Net 2.0 and we get a lot of cool stuff.

For example, we have a webservice which does little more than run a SQL stored procedure, and return the resulting recordset as a strongly typed collection. About 90% of the code consisted of mapping the DataReader's column to a property of an object. The refactored code is now simplied to (the class is simplified for clarity and intellectual property issues):

public class ProductInfo

 [Map("ProdNo")]
  public int ProductNumber;

   [Map("ProdDesc")]
public string Description; 

  [Map("CustPrice")]
public decimal Price;
}

So, to retreive a list of products I simply have to run the following code:

using(SqlConnection conn = db.GetConnection())

{

List<ProductInfo> products;

conn.Open();

using(SqlDataReader sqlDataReader = db.ExecuteProcedure(conn, "spGetProducts"))

{

Factory.ObjectFactory<ProductInfo> factory =

new Factory.ObjectFactory<ProductInfo>(sqlDataReader);

products = factory.CreateItems();

}

return products;

}


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:
Categories: C#
Actions: E-mail | Permalink | Comments (0) | Comment RSSRSS comment feed