About Hasmukh patel

My Photo
Harrow, London, United Kingdom
Dot-Net developer with expertise in Web, WPF, Win-form applications. Have worked on Asp.net,mvc , WPF and Win-forms projects in c#.net language having Sql-Server/Oracle as database with service oriented architecture using test driven development. Having complete knowledge of SDLC and have successfully worked and implemented it on projects.

Inflate DataTable To Data Entity (Generic class)

Inflate DataTable To Data Entity (Generic class)

public class DataToEntity
{
#region inflate
///
/// Inflate data from dataset tot entity
///

///
public static TEntityCollection Inflate(DataSet ds)
where TEntityCollection : List, new()
where TEntity:new()
{
TEntityCollection entityCollection = new TEntityCollection();

DataTable dt = null;
if (ds.Tables.Count > 0)
{
if (ds.Tables.Count == 1)
dt = ds.Tables[0];
else
dt = ds.Tables[ typeof(TEntity).Name];
}
else
throw new Exception("invalid Dataset");

return Inflate(dt);

}

public static TEntityCollection Inflate(DataTable dt)
where TEntityCollection: List,new()
where TEntity : new()
{
TEntityCollection entityCollection =new TEntityCollection();

if (dt != null)
{
foreach (DataRow dr in dt.Rows)
{
TEntity tmpEntity = Inflate(dr);
entityCollection.Add(tmpEntity);
}
}
else
throw new Exception("No data found");

return entityCollection;

}


///
/// Inflate data from dataset tot entity
///

///
public static TEntity Inflate(DataSet ds)
where TEntity : new()
{
DataTable dt = null;
if (ds.Tables.Count > 0)
{
if (ds.Tables.Count == 1)
dt = ds.Tables[0];
else
dt = ds.Tables[typeof(TEntity).Name];
}
else
throw new Exception("invalid Dataset");

return Inflate(dt);

}


public static TEntity Inflate(DataTable dt)
where TEntity : new()
{
TEntity tmpEntity = new TEntity();

if (dt != null)
{
if (dt.Rows.Count > 0)
tmpEntity = Inflate(dt.Rows[0]);
else
throw new Exception("No data found");
}
else
throw new Exception("No data found");

return tmpEntity;
}

public static TEntity Inflate(DataRow dr)
where TEntity : new()
{
TEntity tmpEntity = new TEntity();

Inflate(dr, tmpEntity);

return tmpEntity;

}

public static void Inflate(DataRow dr, object tmpEntity)
{
if (dr != null && tmpEntity != null)
{
foreach (DataColumn dc in dr.Table.Columns)
{
PropertyInfo metadata = tmpEntity.GetType().GetProperty(dc.ColumnName);
if (metadata != null)
{
metadata.SetValue(tmpEntity, dr[dc], null);
}
}
}
else
throw new Exception("No data found or entity is null");
}

#endregion
}