views:

209

answers:

1

We have a mish-mash app with a legacy module that still uses DataSets, DataViews and DataTables however we have most of the the databases ORMed except the DB for this Module. I was wondering if someone could give me pointers as to how to go about building extensions like

/* generates a dataset called CustomerDS with 
DataTable called Customer uses property names as DataColumn name */
var dataset =_customer.AsDataSet(); 
/* Converts the dataset to required object or 
throws exception if its cant convert*/
 var customerEntity = _dataset.ToObject<Customer>();

I dont know when we will get time to work on other layers of the app and free it from DataSets. I might sound crazy but its just a thought. I get nightmares when i need to support/bug fix that app.

+1  A: 

You can use a reflection for example:

class Program {
     public static void Start( string[] args ) {
      var john = new Customer {
       CustomerID = Guid.NewGuid(),
       CustomerName = "John",
       CustomerCode = "J-O"
      };

      var tblJohn = john.ToDataTable();
      var clonedJohn = tblJohn.Rows[0].ToDataObject<Customer>();
     }
}

[AttributeUsage(AttributeTargets.Property)]
public class DataColumnAttribute : Attribute { }
public class Customer {
 [DataColumn]
 public Guid CustomerID { get; set; }

 [DataColumn]
 public string CustomerName { get; set; }

 [DataColumn]
 public string CustomerCode { get; set; }
}

public static class DataObjectExtensions {
 public static T ToDataObject<T>( this DataRow dataRow ) where T : new() {
  var dataObject = Activator.CreateInstance<T>();
  var tpDataObject = dataObject.GetType();

  foreach ( var property in tpDataObject.GetProperties() ) {
   var attributes = property.GetCustomAttributes( typeof( DataColumnAttribute ), true );
   if ( null != attributes && attributes.Length > 0 ) {
    if ( property.CanWrite ) {
     DataColumn clm = dataRow.Table.Columns[property.Name];
     if ( null != clm ) {
      object value = dataRow[clm];
      property.SetValue( dataObject, value, null );
     }
    }
   }
  }

  return dataObject;
 }
 public static DataTable ToDataTable( this object dataObject ) {
  var tpDataObject = dataObject.GetType();

  DataTable tbl = new DataTable();
  DataRow dataRow = tbl.NewRow();
  foreach ( var property in tpDataObject.GetProperties() ) {
   var attributes = property.GetCustomAttributes( typeof( DataColumnAttribute ), true );
   if ( null != attributes && attributes.Length> 0 ) {
    if ( property.CanRead ) {
     object value = property.GetValue( dataObject, null );
     DataColumn clm = tbl.Columns.Add( property.Name, property.PropertyType );
     dataRow[clm] = value;
    }
   }
  }

  tbl.Rows.Add( dataRow );
  tbl.AcceptChanges();
  return tbl;
 }
}
TcKs