A Data Access Layer to persist business objects using attributes and reflection - Part II
By
xicoloko

Persistance to business objects through attributes and reflection

[无常译]

下载源代码 - 3.4 Kb

目录:
第一部分
第二部分
第三部分

前言

上一篇文章中我已经介绍了用特性来声明business object的方法。在这篇文章中,我将要介绍怎样从类中提取这些声明信息。解释怎么在一个应用程序来根据使用在类的特性来生成创建数据表的SQL脚本。

工具

这是一个控制台程序。在参数中传递一个程序集的名字。这个工具就载入程序集,枚举出所有使用DataTable特性标记过的类,并且生成创建数据表的SQL脚本。

我们从载入程序集的代码开始。

public static void Main(string[] args)
{
    if (args.Length != 1)
    {
        Console.WriteLine("Usage: scriptgen [assembly path]");
        return;
    }

Assembly assembly = null;

try
    {
        assembly = Assembly.LoadFrom(args[0]);
    }
    catch (Exception e)
    {
        Console.WriteLine("Failed to load assembly [" + args[0] + "]");
        Console.WriteLine(e.Message);
    }

}

上面的代码试图载入参数中指定程序集。现在我们必需列举出程序集中所有使用DataTable属性声明的类。下面的代码来完成这个任务。

public void ParseAssembly(Assembly assembly)
{
    Type[] types = assembly.GetTypes();

foreach(Type type in types)
    {
        if (type.IsClass)
        {
            DataTableAttribute[] dataTable = (DataTableAttribute[]) 
                         type.GetCustomAttributes(typeof(DataTableAttribute), 
                                                  true);

if (dataTable.Length > 0)
            {
                Console.WriteLine("Found class '{0}'", type.ToString());
            }
        }
    }
}

上面的代码从程序集中取出所有的类型,然后再判断是否是一个类,并且这拥有DataTable特性。如果是则输出这个类的名称。我们需要取得所有的属性来映射到表中的列。正确的代码如下。

void ParseClass(Type type, DataTableAttribute dataTable)
{
    PropertyInfo[] properties = type.GetProperties();

// gets the key field
    foreach (PropertyInfo property in properties)
    {
        KeyFieldAttribute[] key = (KeyFieldAttribute[])
                       property.GetCustomAttributes(typeof(KeyFieldAttribute),
                                                    true);

if (key.Length > 0)
        {
            Console.WriteLine("Key = " + property.Name + " type is "
                        + property.PropertyType);
            break;
        }
    }

// gets the other fields
    foreach (PropertyInfo property in properties)
    {
        BaseFieldAttribute[] field = (BaseFieldAttribute[])
                      property.GetCustomAttributes(typeof(BaseFieldAttribute),
                                                   true);

if (field.Length > 0)
        {
            if (!(field[0] is KeyFieldAttribute))
            {
                Console.WriteLine("Property " + property.Name
                                + " [" + property.PropertyType + "] " +
                                + "maps to column [
                                + field[0].ColumnName + "]");
            }

}
    }
}

现在我们有了创建SQL脚本的所需要的信息。这个版本的工具只能创建符合以下2种条件的脚本:主键是int、自动增长类型;属性类型是string,int,decimal和DataTime。

源代码中包含以下的程序集:

  • DAL.dll: 包含特性类
  • Customer.dll: 包含business objects
  • scriptGen.exe: 生成SQL脚本的工具

后续内容

下一篇文章在,我将要创建一个完整的DAL,在运行时获得对象并在一个数据库中将其持久化。

A Data Access Layer to persist business objects using attributes and reflection - Part II [无常译]...相关推荐

  1. A Data Access Layer to persist business objects using attributes and reflection - Part III [无常译]...

    下载源代码 目录: 第一部分 第二部分 第三部分 前言 这是本系列最后一篇文章.在第一篇中我们知道了自给使用特性来给类添加声明信息.第二篇中我们已经知道如何使用System.Reflection na ...

  2. Generic Data Access Layer泛型的数据访问层

    http://www.codeproject.com/Articles/630277/Generic-Data-Access-Layer-GDA-Part-I http://www.codeproje ...

  3. Generic Data Access Objects -范型DAO类设计模式

    Generic Data Access Objects 普通数据访问对象,这个是Hibernate官方网站上面的一个DAO类的设计模式,基于JDK5.0范型支持,文章地址如下: http://www. ...

  4. SAP Hybris和ABAP Netweaver里的DAO(Data access object)

    DAO在Hybris里的定义: A DAO (Data Access Object) is an interface to the storage back end system. DAOs stor ...

  5. 使用基于 Eclipse 插件框架的 ODA(Open Data Access)进行自定义数据驱动开发

    ODA 之所以能够有如此强的灵活性,是因为: 它提供了一套完整的接口,开发者可以自己去实现数据源的访问逻辑,使得数据源对数据使用者变得透明.只要开发者遵循编程规范,就可以对任何数据进行驱动. 它基于 ...

  6. 感觉 Data Access Application Block(DAAB) 里也有可能写得不太好的地方

    昨天下载了博客园的代码,里面有一个 Data\SqlServer.cs 我不清楚是不是 MS DAAB 里的原样文件.不过前面有声明如下: // =========================== ...

  7. 开发自己的Data Access Application Block[下篇]

    上接:[原创] 我的ORM: 开发自己的Data Access Application Block - Part I 4. Database 下面来介绍重中之重:Database,绝大部分的Data  ...

  8. Enterprise Library: Data Access Application Block配置文件分析篇

    Enterprise Library: Data Access Application Block配置文件分析篇 Enterprise Library提供了Configuration Console配 ...

  9. Dao设计模式(Data Access Object)

    目    录(本篇字数:1858) 介绍 通用Dao 一.Dao泛型接口 二.JavaBean 三.Dao接口实现类 四.单元测试 五.反射工具类 介绍 Dao设计模式(Data Access Obj ...

最新文章

  1. VxWorks中信号量实现任务间通信与同步机制分析
  2. (周日赛)Sort the Array
  3. 移动pc常用Meta标签
  4. 栈的基本操作(数组/链表)
  5. iphone11返回上一级手势怎么设置_华为手机的这五种导航方式,你更习惯哪一种?怎么切换?...
  6. 2个linux机器怎么传文件(scp)
  7. Moon一个无视Linq,无视实体类的设计思路.(不要错过,看了之后, 让我们从此以后不再羡慕linq to entiy!)...
  8. (3)数据结构-线性表链式存储
  9. java常用工具类封装
  10. Ingress.exe——今天你被监控了吗?
  11. JMeter代理录制手机app
  12. 数据分析-数据平滑处理
  13. 量子化学计算机理,量子化学计算在反应机理确证中的应用
  14. “测验你左右脑分别有几岁”到底是个什么鬼?
  15. android hook 第三方app_【MiSRC】技术分享-浅谈android hook技术
  16. 区块链笔记 - 1、区块链的来龙去脉
  17. rk3328 rk3399使用fbtft驱动SPI LCD显示
  18. CS4398 Cirrus Logic的旗舰级音频解码芯片
  19. [万字长文]使用 React 重写学成在线前端项目 I 代码完整可运行,步骤有详解
  20. xml的基础格式详解

热门文章

  1. centos 编译 mysql_centos 编译安装mysql
  2. 办公室计算机网络使用情况,企事业单位办公网络的现状及解决方案.doc
  3. plc以太网端口号虚拟服务器,plc能像pc那样,一个端口号处理多个连接吗?
  4. 8.局部变量/全局变量global/内嵌函数/闭包nonlocal
  5. C++跨类调用——extern
  6. opencv中在图片上显示文本
  7. 【数学与算法】二部图、匈牙利匹配、稳定婚配
  8. 你真的了解Scrum吗?
  9. Java中Comparable和Comparator区别小结
  10. elasticsearch 第三篇(安装篇)