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;
        }


本文转自齐师傅博客园博客,原文链接:http://www.cnblogs.com/youring2/archive/2012/06/20/2556440.html,如需转载请自行联系原作者
相关文章
|
Android开发 Shell
Android模拟点击的四种方式
导论 在Android中模拟一个点击事件有三种方式是通过模拟MotionEvent来实现;一种是通过ADB来实现;一种是通过Instrumentation测试框架来实现 第一种:模拟MotionEvent 通用方法如下: private void setSimulateClick(View ...
9017 0
|
存储 监控 关系型数据库
GTS解密--GTS的原理、架构与特点
全局事务服务(Global Transaction Service,简称 GTS)是阿里新推出的分布式事务处理方案,对其深入分析的资料相对匮乏。本文的目标是剖析GTS的技术路线,厘清其优势与约束。
23086 0
|
数据安全/隐私保护 Windows C语言
|
存储 弹性计算 文件存储
阿里云全面支持SMB协议服务,为数据访问赋能
阿里云NAS近日宣布,SMB(Server Message Block)文件系统访问协议服务正式商业化, 相比之前的公测版本,商业化后的SMB文件系统服务性能大幅提升,可同时支持容量型和性能型NAS文件系统,读写性能最大提升3倍,元数据操作(创建删除文件等)最大提升20倍。
4366 0
|
MySQL 关系型数据库 数据库
|
网络安全
Setting up a Multi-Cloud CI/CD Chain using Alibaba Tools
These days, having a continuous integration and delivery (CI/CD) pipeline in place is pretty much essential for any kind of software development.
2641 0