• ORM映射框架总结数据操作(二)


    1.TableInfo 封装实体特性类

    代码
     1 /**
     2  * 
     3  * 2009-4-11
     4  * 
     5  * 
     6  * 表实体的特性信息
     7  * */
     8 using System;
     9 using System.Collections.Generic;
    10 using System.Linq;
    11 using System.Text;
    12 using System.Reflection;
    13 
    14 namespace CommonData.Model
    15 {
    16     public class TableInfo
    17     {
    18         private TableAttribute table;
    19         /// <summary>
    20         /// 表特性
    21         /// </summary>
    22         public TableAttribute Table
    23         {
    24             get { return table; }
    25             set { table = value; }
    26         }
    27         private ColumnAttribute[] columns;
    28         /// <summary>
    29         /// 字段特性
    30         /// </summary>
    31         public ColumnAttribute[] Columns
    32         {
    33             get { return columns; }
    34             set { columns = value; }
    35         }
    36         private FieldInfo[] fields;
    37         /// <summary>
    38         /// 字段特性
    39         /// </summary>
    40         public FieldInfo[] Fields
    41         {
    42             get { return fields; }
    43             set { fields = value; }
    44         }
    45         private PropertyInfo[] properties;
    46         /// <summary>
    47         /// 属性特性
    48         /// </summary>
    49         public PropertyInfo[] Properties
    50         {
    51             get { return properties; }
    52             set { properties = value; }
    53         }
    54         private LinkTableAttribute[] linkTable;
    55         /// <summary>
    56         /// 主表特性
    57         /// </summary>
    58         public LinkTableAttribute[] LinkTable
    59         {
    60             get { return linkTable; }
    61             set { linkTable = value; }
    62         }
    63         private LinkTablesAttribute[] linkTables;
    64         /// <summary>
    65         /// 子表特性
    66         /// </summary>
    67         public LinkTablesAttribute[] LinkTables
    68         {
    69             get { return linkTables; }
    70             set { linkTables = value; }
    71         }
    72     }
    73 }

      该类封装每个实体类的特性信息,将每个类的实体特性封装为对象,在后面读取特性信息的时候就可以缓存该对象。每个实例对象对应一个实体类的所有特性信息。这里封装其实也对应了一张数据库表。

    2.EntityTypeCache 读取实体特性信息

    代码
      1 /**
      2  * 
      3  * 2009-4-11
      4  * 
      5  * 
      6  * 表实体的特性信息缓存,可以设置或取得特性信息
      7  * */
      8 using System;
      9 using System.Collections.Generic;
     10 using System.Linq;
     11 using System.Text;
     12 using System.Reflection;
     13 
     14 namespace CommonData.Model
     15 {
     16     public static class EntityTypeCache
     17     {
     18         private static Dictionary<Type, TableInfo> cache = null;
     19 
     20         /// <summary>
     21         /// 静态构造方法,确保该构造方法只执行一次,不会还原初始化值
     22         /// </summary>
     23         static EntityTypeCache()
     24         {
     25             cache = new Dictionary<Type, TableInfo>();
     26         }
     27 
     28         /// <summary>
     29         /// 更具type类型,将实体特性缓存
     30         /// </summary>
     31         /// <param name="type">实体类型</param>
     32         /// <param name="tableInfo">实体特性</param>
     33         public static void InsertTableInfo(Type type,TableInfo tableInfo)
     34         {
     35             if(cache.ContainsKey(type))
     36             {
     37                 return;
     38             }
     39             else
     40             {
     41                 cache.Add(type,tableInfo);
     42             }
     43         }
     44 
     45         /// <summary>
     46         /// 更具IEntity公共实体接口,将实体特性缓存
     47         /// </summary>
     48         /// <param name="entity">实体接口</param>
     49         /// <param name="tableInfo">实体特性</param>
     50         public static void InsertTableInfo(IEntity entity, TableInfo tableInfo)
     51         {
     52             Type type = entity.GetType();
     53             InsertTableInfo(type,tableInfo);
     54         }
     55 
     56         /// <summary>
     57         /// 更具实体类型获得特性信息
     58         /// </summary>
     59         /// <param name="type">实体类型</param>
     60         /// <returns></returns>
     61         public static TableInfo Get(Type type)
     62         {
     63             if (cache.ContainsKey(type))
     64             {
     65                 return cache[type];
     66             }
     67             else
     68             {
     69                 return null;
     70             }
     71         }
     72 
     73         /// <summary>
     74         /// 更具公共实体接口获得实体特性信息
     75         /// </summary>
     76         /// <param name="entity">公共实体接口</param>
     77         /// <returns></returns>
     78         public static TableInfo Get(IEntity entity)
     79         {
     80             Type type = entity.GetType();
     81             return Get(type);
     82         }
     83 
     84         /// <summary>
     85         /// 更具泛型类型获得实体特性信息
     86         /// </summary>
     87         /// <typeparam name="T">泛型类型</typeparam>
     88         /// <returns></returns>
     89         public static TableInfo Get<T>() where T:IEntity
     90         {
     91             Type type=typeof(T);
     92             if (cache.ContainsKey(type))
     93             {
     94                 return cache[type];
     95             }
     96             else
     97             {
     98                 return null;
     99             }
    100         }
    101     }
    102 }

       这个类主要运用的知识是反射,都知道反射可以说是C#语言的灵魂(自我感觉),因为它可以获得数据的原始信息,在Java中的几个框架中都大量运用了反射。这个类都是采用的static 修饰符,静态类静态变量。第一,静态类是在程序域加载的时候加载的,第二 静态类可以全局共享。因此在这里以后读取这些信息的时候不用每次都去反射,第一次反射之后就缓存了这些数据,以后直接用就可以了。这是在性能方面的体现。这个类中还运用了一个IEntity 接口,这个接口是所有实体类的公共接口。在下面将讲解到这个类。使用接口的好处在这里也能体现出来,因为我还不知道实体类的属性有哪些,实体名称都不知道。接口给了一个抽象的概念

      这个类有三个地方值得注意:

      1. private static Dictionary<Type, TableInfo> cache = null; 使用字典类型数据存储,根据键值对存储数据时非常方面查找的。

      2.static EntityTypeCache()
            {
                cache = new Dictionary<Type, TableInfo>();
            }

       构造方法使用静态static 修饰符。静态构造方法,确保该构造方法只执行一次,不会还原初始化值,这就保证了数据缓存全局共享的作用。如果是Web程序我们可能会想到用Application来存储数据,如果脱离了Web程序呢,因此我们需要有自己的缓存机制。

      3. public static TableInfo Get<T>() where T:IEntity
            {
                Type type=typeof(T);
                if (cache.ContainsKey(type))
                {
                    return cache[type];
                }
                else
                {
                    return null;
                }
            }

      这个方法是一个泛型方法,泛型不用多说了,.net平台中不可缺少了,大大增加了语言的灵活性。关键是这个方法后面的约束,使用此约束保证了我们使用的实体类是实现了IEntity 接口的实体类。这是一种很霸道的写法,当然这是笑话。这样可以保证程序的准确性

  • 相关阅读:
    PGA
    impdp导入job
    11g SQL Monitor
    Oracle buffer cache
    OGG异常处理
    Android中dp、dpi与px的关系
    android:gravity设置居中的问题
    Oracle触发器详解
    Android selector中的item的顺序
    selector在color和drawable目录下的区别
  • 原文地址:https://www.cnblogs.com/qingyuan/p/1637314.html
Copyright © 2020-2023  润新知