从创建数据库的行对象 [英] Create object from database row

查看:92
本文介绍了从创建数据库的行对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说,我要建一个应用程序数据访问层。通常我有一个每种对象存储在数据库中的类定义。当然,实际数据访问检索一个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的迅速且安静......尤其是看城堡的<一个HREF =htt​​p://www.castleproject.org/activerecord/相对=nofollow> 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).

这篇关于从创建数据库的行对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆