![]() |
Languages »
C# »
Generics
Intermediate
License: The Code Project Open License (CPOL)
An Easy Way to Populate Instances using GenericsBy Martin OlivaresA sample of using generics to simplify a Data Access Layer |
C# 2.0, Windows, .NET 2.0VS2005, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
With the insertion of Generics in the 2.0 Framework, developers have a tool that radically changes the way to develop. Some of the main advantages of using Generics are:
Collections, List, Queue, Stack, LinkedList) In almost all applications, it's necessary to persist the state of the instances of the objects that we use soon to be consulted and to regenerate these instances. This process of regeneration generally implies great amount of code, nontyped collections, that result in error sensible code and smaller performance. In this simplified example of a data access layer, we will see that it is possible to obtain an instance or list of instances of any entity with only two generic methods.
To fill one instance:
/// <SUMMARY>
/// Populates an instance of the entity.
/// </SUMMARY>
/// <PARAM name="entity">The instance of the entity to populate.</PARAM>
/// <PARAM name="record">The current Datareader record.</PARAM>
public static 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),
BindingFlags.IgnoreCase |
BindingFlags.Public | BindingFlags.Instance);
if (property != null)
{
property.SetValue(entity,
record[property.Name], null);
}
}
}
}
}
To fill an instance List:
/// <SUMMARY>
/// Populates a List of entities of type T.
/// </SUMMARY>
/// <PARAM name="dr">The DataReader with data of entities.</PARAM>
/// <RETURNS></RETURNS>
public static List<T> PopulateEntities<T>(IDataReader dr)
{
List<T> entities = new List<T>();
while (dr.Read())
{
T ent = Activator.CreateInstance<T>();
PopulateEntity<T>(ent, dr);
entities.Add(ent);
}
return entities;
}
If we need a single instance of one entity, invoke the method PopulateEntity passing as parameter the entity type, an empty instance and a record of a Datareader.
public static Contact getContactById(int id)
{
Contact contact = new Contact();
Database db = DatabaseFactory.CreateDatabase();
DbCommand dbCommand = db.GetStoredProcCommand("ContactById");
db.AddInParameter(dbCommand, "contactId", DbType.Int32, id);
IDataReader idr = db.ExecuteReader(dbCommand);
if (idr.Read())
{
EntityUtils.PopulateEntity<Contact>(contact, idr);
}
return contact;
}
If we need an instance collection of one entity, invoke the method PopulateEntities passing as parameter the entity type and Datareader.
public static List<Contact> getContacts()
{
Database db = DatabaseFactory.CreateDatabase();
DbCommand dbCommand = db.GetStoredProcCommand("ContactGetAll");
IDataReader idr = db.ExecuteReader(dbCommand);
return (EntityUtils.PopulateEntities<Contact>(idr));
}
Using generics List as return of PopulateEntities we have an instance collection with methods having great functionality to sort, find, iterate, manage in our business and user interface layers.
The sample uses the AdventureWorks database included in SQL Server 2005. Modify the connection string LocalSqlServer located in web.config and run the script StoredProcedures.sql located in the App_Data directory to add the necessary stored procedures.
The inclusion of Generics in the Framework produced a jump of maturity and productivity to this tool. Therefore I consider that is one of the aspects in which it is necessary to deepen knowledge, otherwise we will lose an easy and advantageous way to develop.
I'm an system engineer with 8 years of experience as a developer/architect in enterprise project solutions. I have always worked with Microsoft Platforms and technologies such as VB, SQL Server, .NET, MSF.
| You must Sign In to use this message board. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 20 Dec 2006 Editor: Deeksha Shenoy |
Copyright 2006 by Martin Olivares Everything else Copyright © CodeProject, 1999-2009 Web20 | Advertise on the Code Project |