Here's how I do it..
Extension methods for DataRow that lets me transform a Business Object to DataRow and vice-versa.
/// <summary>
/// Extension methods for DataRow.
/// </summary>
public static class DataRowExtensions
{
/// <summary>
/// Converts DataRow into business object.
/// </summary>
/// <typeparam name="TEntity">A type that inherits EntityBase.</typeparam>
/// <param name="dataRow">DataRow object to convert to business object.</param>
/// <returns>business object created from DataRow.</returns>
public static TEntity ToEntity<TEntity>(this DataRow dataRow)
where TEntity : EntityBase, new()
{
TEntity entity = new TEntity();
ExtensionHelper.TransformDataRowToEntity<TEntity, DataRow>(ref dataRow, ref entity);
return entity;
}
/// <summary>
/// Converts business object into DataRow.
/// </summary>
/// <typeparam name="TEntity">A type that inherits EntityBase.</typeparam>
/// <param name="dataRow">DataRow object to convert business object into.</param>
/// <param name="entity">Business object which needs to be converted to DataRow.</param>
public static void FromEntity<TEntity>(this DataRow dataRow, TEntity entity)
where TEntity : EntityBase, new()
{
ExtensionHelper.TransformEntityToDataRow<TEntity, DataRow>(ref entity, ref dataRow);
}
}
The actual helper methods that does the transform..
/// <summary>
/// Helper methods for transforming data objects into business objects and vice versa.
/// </summary>
/// <remarks>
/// <para>Most important implementation that takes care of universal transformation between business objects and data object.</para>
/// <para>Saves programmers from writing the same old code for every object in the system.</para>
/// </remarks>
public static class ExtensionHelper
{
/// <summary>
/// Transforms business object into DataRow.
/// </summary>
/// <typeparam name="TEntity">A type that inherits EntityBase.</typeparam>
/// <typeparam name="TDataRow">A type that inherits DataRow.</typeparam>
/// <param name="entity">business object which is transformed into DataRow object.</param>
/// <param name="dataRow">DataRow object which is transformed from business object.</param>
public static void TransformEntityToDataRow<TEntity, TDataRow>(ref TEntity entity, ref TDataRow dataRow)
where TDataRow : DataRow
where TEntity : EntityBase
{
IQueryable<DataField> entityFields = entity.GetDataFields();
foreach (DataColumn dataColoumn in dataRow.Table.Columns)
{
if (!dataColoumn.ReadOnly)
{
var entityField =
entityFields.Single(e => e.DataFieldMapping.MappedField.Equals(dataColoumn.ColumnName, StringComparison.OrdinalIgnoreCase));
if (entityField.Property.GetValue(entity, null) == null)
{
if (dataColoumn.AllowDBNull)
{
dataRow[dataColoumn] = System.DBNull.Value;
}
else
{
throw new Exception(dataColoumn.ColumnName + " cannot have null value.");
}
}
else
{
if (entityField.Property.GetType().IsEnum)
{
dataRow[dataColoumn] = Convert.ToByte(entityField.Property.GetValue(entity, null));
}
else
{
dataRow[dataColoumn] = entityField.Property.GetValue(entity, null);
}
}
}
}
}
/// <summary>
/// Transforms DataRow into business object.
/// </summary>
/// <typeparam name="TEntity">A type that inherits EntityBase.</typeparam>
/// <typeparam name="TDataRow">A type that inherits DataRow.</typeparam>
/// <param name="dataRow">DataRow object which is transformed from business object.</param>
/// <param name="entity">business object which is transformed into DataRow object.</param>
public static void TransformDataRowToEntity<TEntity, TDataRow>(ref TDataRow dataRow, ref TEntity entity)
where TDataRow : DataRow
where TEntity : EntityBase
{
IQueryable<DataField> entityFields = entity.GetDataFields();
foreach (var entityField in entityFields)
{
if (dataRow[entityField.DataFieldMapping.MappedField] is System.DBNull)
{
entityField.Property.SetValue(entity, null, null);
}
else
{
if (entityField.Property.GetType().IsEnum)
{
Type enumType = entityField.Property.GetType();
EnumConverter enumConverter = new EnumConverter(enumType);
object enumValue = enumConverter.ConvertFrom(dataRow[entityField.DataFieldMapping.MappedField]);
entityField.Property.SetValue(entity, enumValue, null);
}
else
{
entityField.Property.SetValue(entity, dataRow[entityField.DataFieldMapping.MappedField], null);
}
}
}
}
}
And here's how my sample Business-object looks. Please mind the Custom attributes..
/// <summary>
/// Represents User.
/// </summary>
[TableMapping("Users", "User", "Users")]
public class User : EntityBase
{
#region Constructor(s)
/// <summary>
/// Initializes a new instance of the User class.
/// </summary>
public User()
{
}
#endregion
#region Properties
#region Default Properties - Direct Field Mapping using DataFieldMappingAttribute
/// <summary>
/// Gets or sets the ID value of the AppUser object.
/// </summary>
[DataFieldMapping("UserID")]
[DataObjectFieldAttribute(true, true, false)]
[NotNullOrEmpty(Message = "UserID From UserDetails Table Is Required.")]
public override int Id
{
get;
set;
}
/// <summary>
/// Gets or sets the Username value of the User object.
/// </summary>
[DataFieldMapping("UserName")]
[Searchable]
[NotNullOrEmpty(Message = "Username Is Required.")]
public string UserName
{
get;
set;
}
/// <summary>
/// Gets or sets the FirstName value of the AppUser object.
/// </summary>
[DataFieldMapping("FirstName")]
[Searchable]
public string FirstName
{
get;
set;
}
/// <summary>
/// Gets or sets the LastName value of the AppUser object.
/// </summary>
[DataFieldMapping("LastName")]
[Searchable]
public string LastName
{
get;
set;
}
/// <summary>
/// Gets or sets the WebSite value of the AppUser object.
/// </summary>
[DataFieldMapping("WebSite")]
[ValidURL(Message = "Website is not in Proper Format.")]
public string WebSite
{
get;
set;
}
/// <summary>
/// Gets or sets the ContactNumber value of the AppUser object.
/// </summary>
[DataFieldMapping("ContactNumber")]
[Searchable]
public string ContactNumber
{
get;
set;
}
/// <summary>
/// Gets or sets a value indicating whether AppUser Object is active or inactive.
/// </summary>
[DataFieldMapping("IsActive")]
public bool IsActive
{
get;
set;
}
/// <summary>
/// Gets or sets the BirthDate value of the AppUser object.
/// </summary>
[DataFieldMapping("BirthDate")]
public DateTime? BirthDate
{
get;
set;
}
#region Derived Properties
/// <summary>
/// Gets the full name of the AppUser
/// </summary>
public string FullName
{
get { return this.FirstName + " " + this.LastName; }
}
/// <summary>
/// Gets the Age value of the AppUser
/// </summary>
public int Age
{
get { return this.BirthDate.HasValue ? this.BirthDate.Value.AgeInYears() : 0; }
}
#endregion
#endregion
}
And here's my client-code..
/// <summary>
/// Gets User object by user name.
/// </summary>
/// <param name="username">UserName of the user</param>
/// <returns>User Object</returns>
public static User GetUserByUsername(string username)
{
try
{
return Adapter.GetUserByUserName(username)[0].ToEntity<User>();
}
catch
{
return null;
}
}