Wednesday, February 15, 2006

Are you tired of writing code similar to this?

 

Customer cust = new Customer();

 

cust.CustomerID = reader.GetInt32(“CustomerID”);

cust.LastName = reader.GetString(“LastName”);

cust.FirstName = reader.GetString(“FirstName”);

 

If the answer is yes, then here is a quick solution.

 

A while back I wrote a small algorithm that uses a bit of reflection that basically handles the mapping fields in the database to the entity’s (Business Objects) properties.

 

The end result will looks something like this:

 

SqlDataReader reader = command.ExecuteReader();

 

//Return List -- Could easily be modified for scalar queries

List<Contact> contactList = new List<Contact>();

while (reader.Read())

{

    Contact contact = new Contact();

    //Map record to object

    Contact.PopulateEntity<Contact>(contact, reader);

 

    contactList.Add(contact);

}

 

While my sample uses generics, you could easily make this compatible with .NET 1.? by using “object”. The magic happens in the following method.

 

public void PopulateEntity<T>(T entity, IDataRecord record)

{

if (record != null && record.FieldCount > 0)

      {

            Type type = entity.GetType();

 

            for (int i = 0; i < record.FieldCount; i++)

            {

                  if (DBNull.Value != record[i])

                  {                      

                        PropertyInfo property = type.GetProperty(record.GetName(i));

                        if (property != null)

                        {

                              property.SetValue(entity, record[property.Name], null);

                        }

                  }

            }

      }

}

 

I have included a small example of how it could be used. (Download)

 

In the sample, I use the IDataRecord interface (http://msdn2.microsoft.com/en-us/library/93wb1heh.aspx ). This is the individual record that data readers implement.

 

Enjoy!

 

P.S. Download uses the Adventure Works sample DB Windark.Samples.zip (40.52 KB)

2/15/2006 10:33:33 AM (Eastern Standard Time, UTC-05:00)  #    Comments [10]  | 
10/12/2010 3:29:02 AM (Eastern Standard Time, UTC-05:00)
I really enyojed this brilliant blog. Please keep them coming. Greets!!! I will be sure to be reading in the future.
5/11/2011 4:01:24 PM (Eastern Standard Time, UTC-05:00)
What a joy to find such clear thinking. Thanks for ponsitg!
5/29/2011 6:28:02 AM (Eastern Standard Time, UTC-05:00)
NFZiIB , [url=http://agbmwesvvkti.com/]agbmwesvvkti[/url], [link=http://wkybxssjdflf.com/]wkybxssjdflf[/link], http://esythhtjywfp.com/
5/30/2011 8:25:33 PM (Eastern Standard Time, UTC-05:00)
auto insurance rates >:-D company life insurance =-((( life insurance 83677
5/30/2011 9:03:35 PM (Eastern Standard Time, UTC-05:00)
buy domain tramadol 8PP can you buy propecia online 49104 propecia >:DD
5/31/2011 6:41:57 PM (Eastern Standard Time, UTC-05:00)
cheap business insurance vky health insurance ozshj homeowners insurance quotes =-[[[
6/2/2011 6:50:03 PM (Eastern Standard Time, UTC-05:00)
prednisone no prescription xshjb tramadol >:]
6/4/2011 8:53:11 PM (Eastern Standard Time, UTC-05:00)
colchicine =-[ priligy 314526 buy lexapro medication online cgdbja
6/5/2011 10:50:35 PM (Eastern Standard Time, UTC-05:00)
acomplia 20 mg htdi buying accutane 3291
6/7/2011 7:50:14 PM (Eastern Standard Time, UTC-05:00)
tramadol 162865 propecia rpvnmh zovirax 69684
Name
E-mail
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):

Theme design by Jelle Druyts