65.9K
CodeProject 正在变化。 阅读更多。
Home

使用属性和反射持久化业务对象的 Datalayer - 第二部分

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.56/5 (9投票s)

2002年3月21日

2分钟阅读

viewsIcon

100677

downloadIcon

1328

通过属性和反射将业务对象持久化。

目录

引言

在之前的文章中,我介绍了用于描述业务对象的属性。 在本文中,我将展示如何从类中提取这些描述性信息。 为了解释将如何完成,我将编写一个应用程序,该应用程序根据类中应用的属性创建 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,IDENTITY。 允许的属性类型是:stringintdecimalDateTime

这里包含的源代码文件将创建以下程序集

  • DAL.dll:包含属性
  • Customer.dll:包含业务对象
  • scriptGen.exe:生成 sql 脚本的工具

接下来

在下一篇文章中,我将创建一个完整的 DAL,该 DAL 在运行时获取对象并将其持久化到数据库中,以及从数据提供程序中获取对象。

© . All rights reserved.