读书笔记 黄忠诚 标签 --ORM

2-2-4  新思维,Attribute-Center Designing

    本节中将运用Attribute技术实现一个简单的ORM。什么是ORM呢?简单地说,就是将数据表对应成对象,ORM的对应方式有很多种,本节中采取Record Mapping(记录映像)的方式。在开始之前,得先想想要如何实现Record Mapping模式,既然是以Record为单位,那么想当然一笔Record就会对应至一个对象,该对象就像程序2-18这样。

程序2-18  Record Mapping

using System;

using System.ComponentModel;

using PowerORM;

namespace WindowsApplication5

{

  public class Customer

  {

    private string _customerID,_companyName,_contactName;

 

    public string CustomerID

    {

       get

       {

         return _customerID;

       }

       set

       {

         _customerID = value;

       }

    }

 

    public string CompanyName

    {

       get

       {

         return _companyName;

       }

       set

       {

         _companyName = value;

       }

    }

 

    public string ContactName

    {

       get

       {

         return _contactName;

       }

       set

       {

         _contactName = value;

       }

    }

 

    public Customer()

    {

    }

  }

}

 

    现在假设已由数据库读出一笔数据,那么该如何将这笔数据填入这个对象中呢?有两个方式可以达到,一种是使用Reflection技术枚举对象中的所有属性,再以属性名为依据取得同名的字段来填入数据,这种方式有个缺点,那就是在属性命名上会受到字段名称的限制;另一种方式是使用Attribute来为属性贴上一个字段对应标签,程序2-19是这个ColumnAttribute程序代码。

程序2-19  ColumnAttribute范例

using System;

using System.ComponentModel;

namespace PowerORM

{

  [AttributeUsage(AttributeTargets.Property,

                     Inherited = false,

                     AllowMultiple = false)]

  public class ColumnAttribute:Attribute

  {

    private string _columnName = null;

 

    public string ColumnName

    {

       get

       {

         return _columnName;

       }

    }

 

    public ColumnAttribute(string columnName)

    {

       _columnName = columnName;

    }

  }

}

 

    有了ColumnAttribute之后,用户就可以在属性上贴上一个标签,代表该属性所对应的字段,如程序2-20所示。

程序2-20  贴上ColumnAttribute类范例

[Column("CompanyName")]

public string CompanyName

 

    最后撰写填充数据的Helper对象,此对象会依据目标对象中的Attribute设定来填充数据,见程序2-21

程序2-21  简易ORMHelper Object范例

using System;

using System.Collections;

using System.ComponentModel;

using System.Reflection;

using System.Data;

using System.Data.Common;

namespace PowerORM

{

   public sealed class FillHelper

   {

     public static IList Fill(Type rowType,IDataReader reader)

     {

        ArrayList dataList = new ArrayList();

        while(reader.Read())

        {

           object item = Activator.CreateInstance(rowType,false);

           foreach(MemberInfo mi in rowType.GetMembers())

           {

             foreach(ColumnAttribute attr in
mi.GetCustomAttributes( typeof(ColumnAttribute),false))

             {

                int index = reader.GetOrdinal(attr.ColumnName);

                if(index != -1)

                {

                  if(mi.MemberType == MemberTypes.Field)

                     ((FieldInfo)mi).SetValue(

                                                 item,reader.GetValue(index));

                   else if(mi.MemberType == MemberTypes.Property)

                     ((PropertyInfo)mi).SetValue(

                                           item,reader.GetValue(index),null);

                }

              }

            }

            dataList.Add(item);

          }

          return dataList;

     }

 

     private FillHelper()

     {

     }

   }

}

 

    然而本节中的ORM程序只是一个开端,功能极其原始,不过已经足以说明AttributeORM上的用处了。

posted on 2008-08-25 21:22  simhare  阅读(171)  评论(0)    收藏  举报

导航