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中,将实际的数据通过映射得到具体的实体。
本文作者:拓荒者IT
本文链接:https://www.cnblogs.com/youring2/archive/2012/06/20/2556440.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
📌做了个微信公众号【拓荒者IT】,分享各种技术干货,新内容首发到公众号,欢迎关注❤️
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
2011-06-20 Sql 根据时间段查询数据