当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > 使用属性和反射过渡从数据存取层到业务物件1

ASP.NET
在图片上加入图片版权信息
Peer-to-Peer (P2P) communication across middleboxes(翻译4)
今天完成了.net compact framework 加 web service的演练.
Cordbg, Dumpbin, Ildasm, 的一些教程。
asp和asp.net的session共用
VB连接SQL数据库的模块
消除图片在ie中缓存而无法更新的问题
说说使用static和const关键字
怎样解决thephile中的数据库由于排序造成的问题:对 text 数据类型不支持代码页转...
.net分布式事务例子
Internet Explorer 编程简述(二)
使用SqlParameter参数返回值时遇到的问题
vb可不可以实现虚拟中断
C#下Socket对象的BeginReceive方法,执行后竟然不调用AsyncCallback里的回调函数
坚持学asp.net:(十一)
[C#][正则表达式]寻找匹配的Groups的几种方法
面向服务的体系结构概述
Windows Form 和 UserControl
VB中類模塊實現與C++中類實現的比較(1)
下载Oracle数据库中的Blob二进制文件,实例!

ASP.NET 中的 使用属性和反射过渡从数据存取层到业务物件1


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 34 ::
收藏到网摘: n/a

索引I II III 简介当然,ado.net比起ado和oledb来说要简单多了.我所知道的是打开connection我可以通过dataset和datareader读取数据库.当然你也可以通过你的需要来恢复数据.我意识到dataset功能强大而且能节省我的时间,但是我还是愿意选择自己的方式了处理数据库程序.我喜欢用一个类来调用方法去更新数据. 因此当用数据库中的一条记录的时候我就会创建一个物件实例,设置它的属性和调用的的更新方法,就是这么简单.但是这样以来我不得不写老多的代码,业务物件类,数据库更新代码和数据库读取代码.开始的时候,我没有使用存储过程更新数据库,因此我要对没一个业务物件来写sql语句.在我修改数据库或业务物件的时候,这样的工作就显得特别的乏味.我的方案我的方案是以创建简单的类开始的,是我写较少的代码来更新物件. 所有我做得就是增加字段的名字,值和类,然后生成sql语句. 当我结束这一切的时候,我一周都沉醉在快乐之中…可是当我开始使用sqlServer代替access的时候,我的情绪变化了.我不能使用单纯的sql语句来更新我的物件,我不得不用存储过程.不幸又开始了…我不得不创建成打的sql语句来更新物件.枯燥的工作又开始了…我注意到我能写简单的类来生成参数,正如sql语句生成类一样.尽管这个方案能使我写少一点的代码,但是无论何时我的方案要是有所改动的话,我仍然要去检查我的更新代码.接着我有了在数据库中如何创建持续化类的想法.使用属性来描述数据库的表,这样属性就可以被映射到表中的字段了.这样我就可以仅仅修改业务物件类了.为了你更容易读懂,我把这篇文章分成了三部分.第一部分展示使用属性来描述一个业务类.第二部分展示我是如何采集信息,最后我将展示完整的方案.I - Attributesattributes是用来描述装配件,类,属性,方法和字段的.在.net 架构里已经有些地方使用了.但是你可以创建自己的属性.我使用attributes来描述一个类是如何被存储到数据库中的.在一个类中,我将指出那些属性应该被持续化,正如存储过程是如何更新数据库的.为了描述表里的列,我在类的属性里面使用了attributes. 列可以是一个简单的数据字段,唯一的键或外键.如何创建自己的attribute?相当容易,你可以创建一个继承于System.Attribute的类.至于命名习惯你可以加上个Attribut后缀.当你创建一个attribute,你要知道该attribute如何被用.是否应该被用在类?属性,或多个定义是允许的?现在是来看些代码的时候了.这些attributes被用来描述一个业务物件类: using System;using System.Data; namespace DAL{ [AttributeUsage(AttributeTargets.Property)] public class BaseFieldAttribute : Attribute { string columnName; public BaseFieldAttribute(string columnName) { this.columnName = columnName; } public string ColumnName { get { return columnName; } set { columnName = value; } } } [AttributeUsage(AttributeTargets.Property)] public class DataFieldAttribute : BaseFieldAttribute { DbType dbType = DbType.String; int size = 0; public DataFieldAttribute(string columnName) : base(columnName) { } public DbType Type { get { return dbType; } set { dbType = value; } } public int Size { get { return size; } set { size = value; } } }; [AttributeUsage(AttributeTargets.Property)] public class KeyFieldAttribute : BaseFieldAttribute { public KeyFieldAttribute(string columnName) : base(columnName) { } }; [AttributeUsage(AttributeTargets.Property)] public class ForeignKeyFieldAttribute : BaseFieldAttribute { public ForeignKeyFieldAttribute(string columnName) : base(columnName) { } }; [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)] public class DataTableAttribute : Attribute { string tableName; string updateStoredProcedure = ""; public DataTableAttribute(string tableName) { this.tableName = tableName; } public string TableName { get { return tableName; } set { tableName = value; } } public string UpdateStoredProcedure { get { return updateStoredProcedure; } set { updateStoredProcedure = value; } } }}正如你看到的,每个类的上面有个attributeusage attribute.它是来指示attribute是如何被使用的.我是如何使用这些 attribute来描述一个类的呢?假设你有一个应用来保存用户和联系的信息.在OO设计中,我们以一个Person来开始,那么联系信息就是一个person加上地址和信息.用户就是联系信息加上购买信息. 当然用户和别人是有依赖关系的.如果我在别的文章使用这些当然是相当愚蠢的.^_^这个类的代码如下:using System;using System.Data;using DAL; namespace TestApp{ public class Person { string name = ""; int age = 0; int id = 0; [KeyField("id")] public int Id { get { return id; } set { id = value; } } [DataField("