DataTable 的每一行都作为一个类?
我有一门“位置”课。此类基本上保存地址,因此称为“位置”。我有一个数据表,它返回多个我想要作为“位置”的记录。
现在,我在“location”类中有一个“load”方法,它可以通过 ID 获取单个“位置”。但是,当我想要从多个数据表行中获取“位置”对象的集合时,我该怎么办?每一行都是一个“位置”。
出于显而易见的原因,我不想访问数据库中的每条记录。我是否只需创建位置类的新实例,为属性分配值,同时绕过“加载”方法循环遍历数据表中的行?这对我来说似乎合乎逻辑,但我不确定这是否是正确/最有效的方法。
I have a "location" class. This class basically holds addresses, hence the term "location". I have a datatable that returns multiple records which I want to be "locations".
Right now, I have a "load" method in the "location" class, that gets a single "location" by ID. But what do I do when I want to have a collection of "location" objects from multiple datatable rows? Each row would be a "location".
I don't want to go to the database for each record, for obvious reasons. Do I simply create a new instance of a location class, assigning values to the properties, while looping through the rows in the datatable bypassing the "load" method? It seems logical to me, but I am not sure if this is the correct/most efficient method.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这几乎就是(您的描述)数据行(或行集合)映射到 C# biz 对象的方式。但为了节省大量工作,您应该考虑使用现有的 ORM(对象关系映射器)框架之一,例如 NHibernate,实体Framework、Castle ActiveRecord 等。
大多数 ORM 实际上会生成所有行的样板代码和字段映射到您的 .NET 对象属性,反之亦然。 (是的,ORM 允许您添加、更新和删除数据库数据,就像检索和映射数据库数据一样轻松。)一定要看看 ORM。少量的学习(每个都有一些学习曲线)很快就会得到回报。 ORM 也正在变得相当标准,并且确实是任何涉及 RDBMS 的应用程序所期望的。
此外,这些链接可能令人感兴趣(与 ORM 相关):
关于 ORM 的维基百科文章
所以关于不同 ORM 的讨论
列出了许多不同的 .NET ORM
That is (your description) pretty much how a row (or a collection of rows) of data gets mapped to a C# biz object(s). But to save yourself a lot of work you should consider one of a number of existing ORM (object relational mapper) frameworks such as NHibernate, Entity Framework, Castle ActiveRecord etc.
Most ORMs will actually generate all the boilerplate code where rows and fields are mapped to your .NET object properties and vice-versa. (Yes, ORMs allow you to add, update and delete db data just as easily as retrieving and mapping it.) Do give the ORMs a look. The small amount of learning (there is some learning curve with each) will pay off very shortly. ORMs are also becoming quite standard and indeed expected in any application that touches an RDBMS.
Additionally these links may be of interest (ORM-related):
Wikipedia article on ORMs
SO Discussion on different ORMs
Many different .NET ORMs listed
您走在正确的道路上,一次访问数据库即可获取所需的所有位置,这在性能方面是最佳的。
为了使您的代码更简洁/更短,请为您的
Location
类创建一个采用DataRow
的构造函数,然后该构造函数将相应地设置您的属性。通过这样做,您可以将从列到属性的映射集中到代码库中的一个位置,这将易于维护。然后,循环访问数据表中的行并调用构造函数是完全可以接受的。
您还可以使用对象到关系映射器,例如实体框架 进行数据库交互。
You're on the right track, getting all the locations you need with one trip to the database would be best in terms of performance.
To make your code cleaner/shorter, make a constructor of your
Location
class that takes aDataRow
, which will then set your properties accordingly. By doing this, you'll centralize your mapping from columns to properties in one place in your code base, which will be easy to maintain.Then, it's totally acceptable to loop through the rows in your data table and call your constructor.
You could also use an object to relational mapper, like Entity Framework to do your database interaction.
创建一个返回 IEnumerable 的方法。在这个方法中执行数据库操作,我经常将 sqldatareader 传递到位置构造函数中。所以我会有这样的东西
,显然代码不会工作,但这只是为了给你一个想法。
然而,如果您有很多事情要做,ORM 映射器可以为您节省大量时间!
Create a method that returns an IEnumerable . In this method do your database stuff and I often pass in the sqldatareader into the location constructor. So I would have something like this
obviously that code won't work but it's just to give you an idea.
An ORM mapper could save you loads of time if you have lots to do however!