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

ASP.NET
DVNEWS 3.2 1013版免虚拟目录的安装方法,只要三个步骤
DataRow的序列化问题
VB.net基础:使用UDP发送和接收消息
采用HttpModules来重写URLs(实践篇)
要用到事务了
DataGrid中单元格的比较
动态引用WebService,建立WebService虚拟机
[初学VB.NET]数据绑定
使用ASP调用WebService时不能以Name为数据库中的字段
在VB.NET里操作文本文件
web下打印的办法
怎样把SQL_SERVER数据库里的(类型是image)图片显示在aspx页面里的image控件里
C#中字符串的加密
中小企业信息应用的利器:DAP-Dynamic Applications Platform
数据库系统概论学习笔记
体会到译者的艰辛,也发现了他们犯的错误
IISManager V1.1 是一个在线管理IIS,维护站点组件,安全稳定,最重要的是完全免费。
VS FlexGridPro 8.0如何在window98运行---有没有升级版本,急请高手指点
给立方体添加纹理
Wrox的C#高级编程第三版第一部分第一章(1~9页)

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 37 ::
收藏到网摘: 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("