从创建数据库的行对象对象、数据库

2023-09-04 12:53:31 作者:柒ぐ汐

比方说,我要建一个应用程序数据访问层。通常我有一个每种对象存储在数据库中的类定义。当然,实际数据访问检索一个DataReader的形式数据,打字或无类型的数据集,或类似的,通常用在结果创建每行的对象所需的数据。

Let's say I'm building a data access layer for an application. Typically I have a class definition for a each kind of object that is stored in the database. Of course, the actual data access retrieves data in the form of a datareader, typed or untyped dataset, or similar, usually with the data needed to create one object per row in the results.

你会如何去了解数据层中创建对象实例?本来接受一个数据行构造?如果是的话,你会如何做这种类型的安全?或者,你有你的构造函数列表出来,你要实例的每个字段的一个参数,即使可能有许多领域?你会庆祝这个构造函数内部?

How would you go about creating your object instances in the data layer? Would have a constructor that accepts a datarow? If so, how would you make that type-safe? Or would you have your constructor list out one parameter for each field you want to instantiate, even if there could be many fields? Would you mark this constructor 'internal'?

推荐答案

我强烈建议您使用ORM工具。即使是简单的项目,可以使用ORM的迅速且安静......尤其是看城堡的 ActiveRecord的工具(它位于NHibernate的顶部,以简化模型声明)。

I highly encourage you to use an ORM tool. Even simple projects can make use of ORM quickly and quietly... in particular, look at Castle's ActiveRecord tool (which sits on top of NHibernate to simplify model declaration).