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

** 索引 ** ** **

  • I
  • II
  • III

** 简介 ** ** **

当然 ,ado.net 比起 ado 和 oledb 来说要简单多了 . 我所知道的是打开 connection 我可以通过 dataset 和 datareader 读取数据库 . 当然你也可以通过你的需要来恢复数据 .

我意识到 dataset 功能强大而且能节省我的时间 , 但是我还是愿意选择自己的方式了处理数据库程序 . 我喜欢用一个类来调用方法去更新数据 . 因此当用数据库中的一条记录的时候我就会创建一个物件实例 , 设置它的属性和调用的的更新方法 , 就是这么简单 .

但是这样以来我不得不写老多的代码 , 业务物件类 , 数据库更新代码和数据库读取代码 . 开始的时候 , 我没有使用存储过程更新数据库 , 因此我要对没一个业务物件来写 sql 语句 . 在我修改数据库或业务物件的时候 , 这样的工作就显得特别的乏味 .

** 我的方案 ** ** **

我的方案是以创建简单的类开始的 , 是我写较少的代码来更新物件 . 所有我做得就是增加字段的名字 , 值和类 , 然后生成 sql 语句 . 当我结束这一切的时候 , 我一周都沉醉在快乐之中 …

可是当我开始使用 sqlServer 代替 access 的时候 , 我的情绪变化了 . 我不能使用单纯的 sql 语句来更新我的物件 , 我不得不用存储过程 . 不幸又开始了 … 我不得不创建成打的 sql 语句来更新物件 . 枯燥的工作又开始了 …

我注意到我能写简单的类来生成参数 , 正如 sql 语句生成类一样 . 尽管这个方案能使我写少一点的代码 , 但是无论何时我的方案要是有所改动的话 , 我仍然要去检查我的更新代码 .

接着我有了在数据库中如何创建持续化类的想法 . 使用属性来描述数据库的表 , 这样属性就可以被映射到表中的字段了 . 这样我就可以仅仅修改业务物件类了 .

为了你更容易读懂 , 我把这篇文章分成了三部分 . 第一部分展示使用属性来描述一个业务类 . 第二部分展示我是如何采集信息 , 最后我将展示完整的方案 .

** I - Attributes **

attributes 是用来描述装配件 , 类 , 属性 , 方法和字段的 . 在 .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 加上地址和信息 . 用户就是联系信息加上购买信息 . 当然用户和别人是有依赖关系的 . 如果我在别的文章使用这些当然是相当愚蠢的 .^_^

Published At
Categories with Web编程
Tagged with
comments powered by Disqus