对象到 DataView 或 DataSet 或 DataTable 并返回到对象

发布于 2024-08-15 00:19:40 字数 531 浏览 8 评论 0原文

我们有一个混合应用程序,其遗留模块仍然使用 DataSets、DataViews 和 DataTables,但是我们拥有除此模块的 DB 之外的大多数数据库 ORMed。我想知道是否有人可以指导我如何构建扩展,就像

/* 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>(); 

我不知道我们什么时候有时间在应用程序的其他层上工作并将其从数据集中释放出来一样。我可能听起来很疯狂,但这只是一个想法。当我需要支持/修复该应用程序的错误时,我会做噩梦。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

负佳期 2024-08-22 00:19:40

您可以使用反射,例如:

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;
    }
}

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;
    }
}
吃→可爱长大的 2024-08-22 00:19:40

您可以使用它从数据表中获取对象

 public static class Extensions
    {
        public static List<T> ToList<T>(this DataTable table) where T : new()
        {
            IList<PropertyInfo> properties = typeof(T).GetProperties().ToList();
            List<T> result = new List<T>();

            foreach (var row in table.Rows)
            {
                var item = CreateItemFromRow<T>((DataRow)row, properties);
                result.Add(item);
            }

            return result;
        }

        private static T CreateItemFromRow<T>(DataRow row, IList<PropertyInfo> properties) where T : new()
        {
            T item = new T();
            foreach (var property in properties)
            {
                if (property.PropertyType == typeof(System.DayOfWeek))
                {
                    DayOfWeek day = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), row[property.Name].ToString());
                    property.SetValue(item,day,null);
                }
                else
                {
                    property.SetValue(item, row[property.Name], null);
                }
            }
            return item;
        }
    }

并像这样使用它

List<Employee> lst = ds.Tables[0].ToList<Employee>();

You can use this to get object from data table

 public static class Extensions
    {
        public static List<T> ToList<T>(this DataTable table) where T : new()
        {
            IList<PropertyInfo> properties = typeof(T).GetProperties().ToList();
            List<T> result = new List<T>();

            foreach (var row in table.Rows)
            {
                var item = CreateItemFromRow<T>((DataRow)row, properties);
                result.Add(item);
            }

            return result;
        }

        private static T CreateItemFromRow<T>(DataRow row, IList<PropertyInfo> properties) where T : new()
        {
            T item = new T();
            foreach (var property in properties)
            {
                if (property.PropertyType == typeof(System.DayOfWeek))
                {
                    DayOfWeek day = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), row[property.Name].ToString());
                    property.SetValue(item,day,null);
                }
                else
                {
                    property.SetValue(item, row[property.Name], null);
                }
            }
            return item;
        }
    }

and use it like this

List<Employee> lst = ds.Tables[0].ToList<Employee>();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文