What to do when you have generics

by Rob 9. August 2007 15:13

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:

C#

Powered by BlogEngine.NET 1.4.0.0
Theme by Mads Kristensen

About the author

Some text that describes me

Recent comments

Comment RSS

Page List

    Disclaimer

    The opinions expressed herein are my own personal opinions and do not represent my employer's view in  anyway.

    © Copyright 2008