zoukankan      html  css  js  c++  java
  • PetaPoco的默认映射 拓荒者

    PetaPoco的映射需要继承IMapper接口,该接口提供了四个方法:

    • TableInfo GetTableInfo(Type pocoType);
    • ColumnInfo GetColumnInfo(PropertyInfo pocoProperty);
    • Func<object, object> GetFromDbConverter(PropertyInfo TargetProperty, Type SourceType);
    • Func<object, object> GetToDbConverter(PropertyInfo SourceProperty);

    Brad提供了一个StandardMapper类来实现这个接口,完成PetaPoco中的默认映射。

    这个类实现了两个方法:GetTableInfo 和GetColumnInfo,后两个转换的方法均返回null值。

            /// <summary>
            /// Constructs a TableInfo for a POCO by reading its attribute data
            /// </summary>
            /// <param name="pocoType">The POCO Type</param>
            /// <returns></returns>
            public TableInfo GetTableInfo(Type pocoType)
            {
                return TableInfo.FromPoco(pocoType);
            }
    
            /// <summary>
            /// Constructs a ColumnInfo for a POCO property by reading its attribute data
            /// </summary>
            /// <param name="pocoProperty"></param>
            /// <returns></returns>
            public ColumnInfo GetColumnInfo(PropertyInfo pocoProperty)
            {
                return ColumnInfo.FromProperty(pocoProperty);
            }

    通过调用TableInfo的FromPoco静态方法生成一个新的TableInfo类,和通过调用ColumnInfo的FromProperty静态方法生成一个新的ColumnInfo类。接下来我们看看这两个方法:

    TableInfo.FromPoco

            /// <summary>
            /// Creates and populates a TableInfo from the attributes of a POCO
            /// </summary>
            /// <param name="t">The POCO type</param>
            /// <returns>A TableInfo instance</returns>
            public static TableInfo FromPoco(Type t)
            {
                TableInfo ti = new TableInfo();
    
                // Get the table name
                var a = t.GetCustomAttributes(typeof(TableNameAttribute), true);
                ti.TableName = a.Length == 0 ? t.Name : (a[0] as TableNameAttribute).Value;
    
                // Get the primary key
                a = t.GetCustomAttributes(typeof(PrimaryKeyAttribute), true);
                ti.PrimaryKey = a.Length == 0 ? "ID" : (a[0] as PrimaryKeyAttribute).Value;
                ti.SequenceName = a.Length == 0 ? null : (a[0] as PrimaryKeyAttribute).sequenceName;
                ti.AutoIncrement = a.Length == 0 ? false : (a[0] as PrimaryKeyAttribute).autoIncrement;
    
                return ti;
            }

    这个方法通过反射,得到TableInfo的TableName、PrimaryKey、AutoIncrement等信息。

    ColumnInfo.FromProperty

            /// <summary>
            /// Creates and populates a ColumnInfo from the attributes of a POCO property.
            /// </summary>
            /// <param name="pi">The property whose column info is required</param>
            /// <returns>A ColumnInfo instance</returns>
            public static ColumnInfo FromProperty(PropertyInfo pi)
            {
                // Check if declaring poco has [Explicit] attribute
                bool ExplicitColumns = pi.DeclaringType.GetCustomAttributes(typeof(ExplicitColumnsAttribute), true).Length > 0;
    
                // Check for [Column]/[Ignore] Attributes
                var ColAttrs = pi.GetCustomAttributes(typeof(ColumnAttribute), true);
                if (ExplicitColumns)
                {
                    if (ColAttrs.Length == 0)
                        return null;
                }
                else
                {
                    if (pi.GetCustomAttributes(typeof(IgnoreAttribute), true).Length != 0)
                        return null;
                }
    
                ColumnInfo ci = new ColumnInfo();
    
                // Read attribute
                if (ColAttrs.Length > 0)
                {
                    var colattr = (ColumnAttribute)ColAttrs[0];
    
                    ci.ColumnName = colattr.Name==null ? pi.Name : colattr.Name;
                    ci.ForceToUtc = colattr.ForceToUtc;
                    if ((colattr as ResultColumnAttribute) != null)
                        ci.ResultColumn = true;
                }
                else
                {
                    ci.ColumnName = pi.Name;
                    ci.ForceToUtc = false;
                    ci.ResultColumn = false;
                }
    
                return ci;
            }

    该方法通过反射得到一个属性所对应的字段的信息。

    通过这两个方法能够得到实体到数据表的映射关系,在类PocoData和PocoColumn中,将实际的数据通过映射得到具体的实体。

  • 相关阅读:
    Mybatis深入浅出之工作原理
    Mybatis深入浅出之缓存机制
    Error :Unable to access jarfile *.jar
    Mysql与JDBC版本兼容性问题
    找工作的正确方法
    关于制作云主机基准镜像
    笔记分享
    Android4.0.1找不到R.java
    android 反编译出错 can not merge I and Z
    [论文理解] Improving the imporved training of Wasserstesin GANS: A consistency term and its dual effect
  • 原文地址:https://www.cnblogs.com/youring2/p/2556440.html
Copyright © 2011-2022 走看看