zoukankan      html  css  js  c++  java
  • 轻量级ORM开发系列:Model类相关信息的处理

            本文主要解决ORM开发过程中Model类在ORM层产生的信息,在开始这篇文章之前,你有必要了解这个轻量级ORM设置的类相关信息类以及属性相关信息类,如果你还不了解,请阅读一文轻量级ORM开发系列:Attribute准备一文。

            在ORM中,每个Model类都有相关的属性以及信息(如TableAttribute),每个属性又对应着与之相关的信息(如ColumnAttribute,IgnoreAttribute等等),这篇文章主要描述一下我的属性信息是怎么解决的。

           首先,我申明了一个EntityPropertyInfo类,这个类来存储与属性相关的信息。代码如下:

    代码
      /// <summary>
        
    /// 说明:若是一个被忽略的属性,则只有相关的Property,Type信息,此时IsIgnore为true
        
    /// </summary>
        public class EntityPropertyInfo
        {
            
    /// <summary>
            
    /// 对他所属于的EntityInfo的引用
            
    /// </summary>
            public EntityInfo EntityInfo { getset; }
            
    public String ColumnName { getset; }
            
    public Type Type { getset; }
            
    public PropertyInfo Property { getset; }
            
    public Boolean IsAutoIncrement { getset; }
            
    public Boolean IsIgnore { getset; }
            
    public ColumnAttribute ColumnAttribute { getset; }
            

            
    public static EntityPropertyInfo Get(PropertyInfo property, EntityInfo paraEntityInfo)
            {
                EntityPropertyInfo epi 
    = new EntityPropertyInfo();
                epi.Property 
    = property;
                epi.Type 
    = property.PropertyType;
                epi.EntityInfo 
    = paraEntityInfo;
                epi.IsIgnore 
    = property.IsDefined(typeof(IgnoreAttribute), false);
                
    if (!epi.IsIgnore)//
                {
                    epi.ColumnAttribute 
    = FoxReflection.GetAttribute(property, typeof(ColumnAttribute)) as ColumnAttribute;
                    
    //列名的处理
                    epi.ColumnName = epi.ColumnAttribute == null ? property.Name : epi.ColumnAttribute.Column;
                    
    if (epi.ColumnAttribute == null)
                    {
                        epi.ColumnAttribute 
    = new ColumnAttribute();
                    }
                    
    if (epi.ColumnName == null)
                    {
                        epi.ColumnName 
    = property.Name;//即使该属性有ColumnAttribute,但也不一定设定了列名
                        epi.ColumnAttribute.Column = property.Name;//同步到ColumnAttribute,防止以后引用导致出错
                    }
                    
    if (property.PropertyType is IList)
                    {
                       
    // epi.HasOne2ManyRelation = true;
                    }
                   
                }    
                
    return epi;
            }
        }

           这个类的Get方法还没有写完,如果你看到了这里,想进一步完善他的话可以与我取得联系获得源代码,我现在已经没有精力回头去研究这些东西了,这些博文也只是记录一下我在这段时期研究过哪些东西而已。如果你是为了学习的目的的话,我觉得还是会蛮有收获的,至少我觉得是这样。

           EntityPropertyInfo类就是对一个属性所有的信息做一个集合。以方便后面的处理,这里我们同样对我们的Model进行一个这样的封装,建立一个新的类,EntityInfo类来集合所有的 Model在ORM中需要用到的信息。


    代码
     [Serializable]
        
    public class EntityInfo
        {
            
    public TableAttribute Table{ get ;set ;}
           
            
    /// <summary>
            
    /// 标识是否已经对此类进行了CREAT_SQL语句生成
            
    /// </summary>
            public IList<PropertyInfo> PropertyList = new List<PropertyInfo>();
            
    public IList<EntityPropertyInfo> EntityPropertiesList = new List<EntityPropertyInfo>();
            
    public string TypeFullName { getset; }
            
    public string TableName { getset; }
            
    public string DataBaseName { getset; }
            
    /// <summary>
            
    /// 
            
    /// </summary>
            public EntityPropertyInfo PrimaryKey { getset; }

            
    /// <summary>
            
    /// 获取需要ORM操作的属性的个数
            
    /// </summary>
            
    /// <returns></returns>
            public int GetUsedCount()
            {
                
    int count = 0;
                
    foreach (EntityPropertyInfo epi in EntityPropertiesList)
                {
                    
    if (!epi.IsIgnore)
                    {
                        count
    ++;
                    }
                }
                
    return count;
            }
        }

         这里实现了一个IList<EntityPropertyInfo>的集合,这样一个类的所有属性就可以顺序的索引到。这位后面数据的赋值以及初始化等等操作都打下了基础。

           public string TypeFullName { get; set; }       //类型全名
           public string TableName { get; set; }          //映射的数据库表名
           public string DataBaseName { get; set; }    //数据库名,这意味着我们的程序可以跨数据库操作表。

        

           有了这两个类,这个轻量级ORM信息处理的即使就打好了,当然,在后面我们会看到怎么让Model类以及其属性变成相应的EntityInfo类以及IList<EntityPropertyInfo>集合,在ORM里面我们不可能所有的信息都通过反射临时来获取,反射可不是省油的灯,所以我们在看轻量级ORM的缓存设计的时候也会基于这里讲到的两个存储信息的类。这些提到的部分将会在后面一一补充。


    博客地址:Zealot Yin

    欢迎转载,转载请注明出处[http://creator.cnblogs.com/]

  • 相关阅读:
    android:id="@android:id/tabhost" 、android:id="@+id/llRoot" 、android:id="@id/llRoot" 之间的区别
    android:ellipsize的使用
    PopupWindow为什么要设置setBackgroundDrawable(new BitmapDrawable());
    android中LayoutInflater的3种使用以及getSystemService的API
    context 的理解
    Android 中this、getContext()、getApplicationContext()、getApplication()、getBaseContext() 之间的区别
    android Matrix 使用
    public private protected frientdly 作用域
    pyinstaller参数介绍以及总结
    MongoDB高级查询详细
  • 原文地址:https://www.cnblogs.com/Creator/p/1886648.html
Copyright © 2011-2022 走看看