Filling Data objects using Reflection

This article describes filling a "Data class" using a generic method. Reflection is used to find the property of any passed class dynamically and assign the value. The below approach will be very userful in large applications where hundreds of classes have to be filled throughout the flow.
 
Consider you have a data class called  "Person" which contains below listed properties
{Name, Address, City, State, Country} . If this data object has to be filled the usual way is to get a datareader or dataset and assign the values directly.
 
But this new strategy iterates through each and every property of any passed object and assigns value from datareader automatically.
 
UI
  1. Person person = new Person();  
  2. DAL dal = new DAL();  
  3. //Pass object as a parameter  
  4. dal.ExecuteDataReader(person, "a");  
  5. Response.Write("Name: " + person.Name + "<br>");  
  6. Response.Write("Address: " + person.Address + "<br>");  
  7. Response.Write("City: " + person.City + "<br>");  
  8. Response.Write("State: " + person.State + "<br>");  
  9. Response.Write("Country: " + person.Country + "<br>"); 

DAL

  1. /// <summary>  
  2. /// ExecuteClass is used to fill all properties any passed  data object  
  3. /// Author :Sridhar  Subramanian  
  4. /// </summary>  
  5. /// <param name="objectClass">Object which has to be filled</param  
  6. /// <param name="parameter1">SQL query parameter,you can use SP </param>  
  7. public void ExecuteClass(object objectClass, string parameter1)  
  8. {  
  9.     try  
  10.     {  
  11.         sqlConnection = new SqlConnection(@"Server=SridharDEV;Database=EntLibTest;Uid=Test;Pwd=Test;");  
  12.         sqlConnection.Open();  
  13.         sqlCommand = new SqlCommand("SELECT * FROM PERSON WHERE NAME='" + parameter1 + "'", sqlConnection);  
  14.         sqlDataReader = sqlCommand.ExecuteReader();  
  15.         Type theType = objectClass.GetType();  
  16.         PropertyInfo[] p = theType.GetProperties();  
  17.         object[] custAttr = null;  
  18.         while (sqlDataReader.Read())  
  19.         {  
  20.             foreach (PropertyInfo pi in p)  
  21.             {  
  22.                 try  
  23.                 {  
  24.                     custAttr = pi.GetCustomAttributes(true);  
  25.                     if ((sqlDataReader[pi.Name] != System.DBNull.Value) && (custAttr.Length == 0))  
  26.                         pi.SetValue(objectClass, sqlDataReader[pi.Name], null);  
  27.                 }  
  28.                 catch (System.IndexOutOfRangeException) { }  
  29.             }  
  30.         }  
  31.     }  
  32.     catch (Exception ex)  
  33.     {  
  34.         throw ex;  
  35.     }  
  36.     finally  
  37.     {  
  38.         if (sqlDataReader.IsClosed == false)  
  39.             sqlDataReader.Close();  
  40.         if (sqlConnection.State == ConnectionState.Open)  
  41.             sqlConnection.Close();  
  42.         sqlConnection.Dispose();  
  43.     }   
  44. } 


Recommended Free Ebook
Similar Articles