public static ColumnInfo FromMemberInfo(MemberInfo mi) { var ci = new ColumnInfo(); var attrs = Attribute.GetCustomAttributes(mi, true); var colAttrs = attrs.OfType<ColumnAttribute>(); var columnTypeAttrs = attrs.OfType<ColumnTypeAttribute>(); var ignoreAttrs = attrs.OfType<IgnoreAttribute>(); // Check if declaring poco has [ExplicitColumns] attribute var explicitColumns = mi.DeclaringType.GetCustomAttributes(typeof(ExplicitColumnsAttribute), true).Any(); var aliasColumn = (AliasAttribute) Attribute.GetCustomAttributes(mi, typeof(AliasAttribute), true).FirstOrDefault(); // Ignore column if declarying poco has [ExplicitColumns] attribute // and property doesn't have an explicit [Column] attribute, // or property has an [Ignore] attribute if ((explicitColumns && !colAttrs.Any()) || ignoreAttrs.Any()) { ci.IgnoreColumn = true; } // Read attribute if (colAttrs.Any()) { var colattr = colAttrs.First(); ci.ColumnName = colattr.Name ?? mi.Name; ci.ForceToUtc = colattr.ForceToUtc; ci.ResultColumn = colattr is ResultColumnAttribute; ci.VersionColumn = colattr is VersionColumnAttribute; if (ci.VersionColumn) { ci.VersionColumnType = ((VersionColumnAttribute) colattr).VersionColumnType; } ci.ComputedColumn = colattr is ComputedColumnAttribute; ci.ColumnAlias = aliasColumn != null ? aliasColumn.Alias : null; } else { ci.ColumnName = mi.Name; } if (columnTypeAttrs.Any()) { ci.ColumnType = columnTypeAttrs.First().Type; } return ci; }
public static ColumnInfo FromMemberInfo(MemberInfo mi) { // Check if declaring/reflected poco has [Explicit] attribute var a = mi.ReflectedType.GetCustomAttributes(typeof(ExplicitColumnsAttribute), true); bool ExplicitColumns = mi.DeclaringType.GetCustomAttributes(typeof(ExplicitColumnsAttribute), true).Any() || a.Length != 0 && (a[0] as ExplicitColumnsAttribute).ApplyToBase; // Check for [Column]/[Ignore] Attributes var ColAttrs = mi.GetCustomAttributes(typeof(ColumnAttribute), true); if (ExplicitColumns) { if (ColAttrs.Length == 0) return null; } else { if (mi.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 ?? mi.Name; ci.ForceToUtc = colattr.ForceToUtc; if ((colattr as ResultColumnAttribute) != null) ci.ResultColumn = true; } else { ci.ColumnName = mi.Name; ci.ForceToUtc = false; ci.ResultColumn = false; } var columnTypeAttr = mi.GetCustomAttributes(typeof(ColumnTypeAttribute), true); if (columnTypeAttr.Any()) { ci.ColumnType = ((ColumnTypeAttribute)columnTypeAttr[0]).Type; } return ci; }
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 ?? pi.Name; ci.ForceToUtc = colattr.ForceToUtc; if ((colattr as ResultColumnAttribute) != null) ci.ResultColumn = true; } else { ci.ColumnName = pi.Name; ci.ForceToUtc = false; ci.ResultColumn = false; } var columnTypeAttr = pi.GetCustomAttributes(typeof(ColumnTypeAttribute), true); if (columnTypeAttr.Any()) { ci.ColumnType = ((ColumnTypeAttribute)columnTypeAttr[0]).Type; } return ci; }
public static ColumnInfo FromMemberInfo(MemberInfo mi) { var ci = new ColumnInfo{MemberInfo = mi}; var attrs = ReflectionUtils.GetCustomAttributes(mi); var colAttrs = attrs.OfType<ColumnAttribute>(); var columnTypeAttrs = attrs.OfType<ColumnTypeAttribute>(); var ignoreAttrs = attrs.OfType<IgnoreAttribute>(); var complexMapping = attrs.OfType<ComplexMappingAttribute>(); var serializedColumnAttributes = attrs.OfType<SerializedColumnAttribute>(); var reference = attrs.OfType<ReferenceAttribute>(); var aliasColumn = attrs.OfType<AliasAttribute>().FirstOrDefault(); // Check if declaring poco has [ExplicitColumns] attribute var explicitColumns = mi.DeclaringType.GetTypeInfo().GetCustomAttributes(typeof(ExplicitColumnsAttribute), true).Any(); // Ignore column if declarying poco has [ExplicitColumns] attribute // and property doesn't have an explicit [Column] attribute, // or property has an [Ignore] attribute if ((explicitColumns && !colAttrs.Any() && !reference.Any() && !complexMapping.Any()) || ignoreAttrs.Any()) { ci.IgnoreColumn = true; } if (complexMapping.Any()) { ci.ComplexMapping = true; ci.ComplexPrefix = complexMapping.First().CustomPrefix; } else if (serializedColumnAttributes.Any()) { ci.SerializedColumn = true; } else if (reference.Any()) { ci.ReferenceType = reference.First().ReferenceType; ci.ReferenceMemberName = reference.First().ReferenceMemberName ?? "Id"; ci.ColumnName = reference.First().ColumnName ?? mi.Name + "Id"; return ci; } else if (PocoDataBuilder.IsList(mi)) { ci.ReferenceType = ReferenceType.Many; return ci; } else if (mi.GetMemberInfoType().IsAClass() && !colAttrs.Any()) { ci.ComplexMapping = true; } // Read attribute if (colAttrs.Any()) { var colattr = colAttrs.First(); ci.ColumnName = colattr.Name ?? mi.Name; ci.ForceToUtc = colattr.ForceToUtc; ci.ResultColumn = colattr is ResultColumnAttribute; ci.VersionColumn = colattr is VersionColumnAttribute; ci.VersionColumnType = ci.VersionColumn ? ((VersionColumnAttribute) colattr).VersionColumnType : ci.VersionColumnType; ci.ComputedColumn = colattr is ComputedColumnAttribute; ci.ComputedColumnType = ci.ComputedColumn ? ((ComputedColumnAttribute)colattr).ComputedColumnType : ComputedColumnType.Always; } else { ci.ColumnName = mi.Name; } ci.ColumnAlias = aliasColumn != null ? aliasColumn.Alias : null; if (columnTypeAttrs.Any()) { ci.ColumnType = columnTypeAttrs.First().Type; } return ci; }
private IEnumerable<PocoMemberPlan> GetPocoMembers(MapperCollection mapper, ColumnInfo[] columnInfos, List<MemberInfo> memberInfos, string prefix = null) { var capturedMembers = memberInfos.ToArray(); var capturedPrefix = prefix; foreach (var columnInfo in columnInfos) { if (columnInfo.IgnoreColumn) continue; var memberInfoType = columnInfo.MemberInfo.GetMemberInfoType(); if (columnInfo.ReferenceType == ReferenceType.Many) { var genericArguments = memberInfoType.GetGenericArguments(); memberInfoType = genericArguments.Any() ? genericArguments.First() : memberInfoType.GetTypeWithGenericTypeDefinitionOf(typeof(IList<>)).GetGenericArguments().First(); } var childrenPlans = new PocoMemberPlan[0]; TableInfoPlan childTableInfoPlan = null; var members = new List<MemberInfo>(capturedMembers) { columnInfo.MemberInfo }; if (columnInfo.ComplexMapping || columnInfo.ReferenceType != ReferenceType.None) { if (capturedMembers.GroupBy(x => x.GetMemberInfoType()).Any(x => x.Count() >= 2)) { continue; } var childColumnInfos = GetColumnInfos(memberInfoType); if (columnInfo.ReferenceType != ReferenceType.None) { childTableInfoPlan = GetTableInfo(memberInfoType, childColumnInfos, members); } var newPrefix = JoinStrings(capturedPrefix, columnInfo.ReferenceType != ReferenceType.None ? "" : (columnInfo.ComplexPrefix ?? columnInfo.MemberInfo.Name)); childrenPlans = GetPocoMembers(mapper, childColumnInfos, members, newPrefix).ToArray(); } MemberInfo capturedMemberInfo = columnInfo.MemberInfo; ColumnInfo capturedColumnInfo = columnInfo; var accessors = GetMemberAccessors(members); var memberType = capturedMemberInfo.GetMemberInfoType(); var isList = IsList(capturedMemberInfo); var listType = GetListType(memberType, isList); var isDynamic = capturedMemberInfo.IsDynamic(); var fastCreate = GetFastCreate(memberType, mapper, isList, isDynamic); var columnName = GetColumnName(capturedPrefix, capturedColumnInfo.ColumnName ?? capturedMemberInfo.Name); var memberInfoData = new MemberInfoData(capturedMemberInfo); yield return tableInfo => { var pc = new PocoColumn { ReferenceType = capturedColumnInfo.ReferenceType, TableInfo = tableInfo, MemberInfoData = memberInfoData, MemberInfoChain = members, ColumnName = columnName, ResultColumn = capturedColumnInfo.ResultColumn, ForceToUtc = capturedColumnInfo.ForceToUtc, ComputedColumn = capturedColumnInfo.ComputedColumn, ComputedColumnType = capturedColumnInfo.ComputedColumnType, ColumnType = capturedColumnInfo.ColumnType, ColumnAlias = capturedColumnInfo.ColumnAlias, VersionColumn = capturedColumnInfo.VersionColumn, VersionColumnType = capturedColumnInfo.VersionColumnType, SerializedColumn = capturedColumnInfo.SerializedColumn }; pc.SetMemberAccessors(accessors); var childrenTableInfo = childTableInfoPlan == null ? tableInfo : childTableInfoPlan(); var children = childrenPlans.Select(plan => plan(childrenTableInfo)).ToList(); // Cascade ResultColumn down foreach (var child in children.Where(child => child.PocoColumn != null && pc.ResultColumn)) { child.PocoColumn.ResultColumn = true; } var pocoMember = new PocoMember() { MemberInfoData = memberInfoData, MemberInfoChain = members, IsList = isList, IsDynamic = isDynamic, PocoColumn = capturedColumnInfo.ComplexMapping ? null : pc, ReferenceType = capturedColumnInfo.ReferenceType, ReferenceMemberName = capturedColumnInfo.ReferenceMemberName, PocoMemberChildren = children, }; pocoMember.SetMemberAccessor(accessors[accessors.Count - 1], fastCreate, listType); return pocoMember; }; } }
protected virtual TableInfoPlan GetTableInfo(Type type, ColumnInfo[] columnInfos, List<MemberInfo> memberInfos) { var alias = CreateAlias(type.Name, type); var tableInfo = TableInfo.FromPoco(type); tableInfo.AutoAlias = alias; return () => { return tableInfo.Clone(); }; }
public IEnumerable <PocoMemberPlan> GetPocoMembers(ColumnInfo[] columnInfos, List <MemberInfo> memberInfos, string prefix = null) { var capturedMembers = memberInfos.ToArray(); var capturedPrefix = prefix; foreach (var columnInfo in columnInfos) { if (columnInfo.IgnoreColumn) { continue; } var memberInfoType = columnInfo.MemberInfo.GetMemberInfoType(); if (columnInfo.ReferenceType == ReferenceType.Many) { var genericArguments = memberInfoType.GetGenericArguments(); memberInfoType = genericArguments.Any() ? genericArguments.First() : memberInfoType.GetTypeWithGenericTypeDefinitionOf(typeof(IList <>)).GetGenericArguments().First(); } var childrenPlans = new PocoMemberPlan[0]; TableInfoPlan childTableInfoPlan = null; var members = new List <MemberInfo>(capturedMembers) { columnInfo.MemberInfo }; if (columnInfo.ComplexMapping || columnInfo.ReferenceType != ReferenceType.None) { if (capturedMembers.GroupBy(x => x.GetMemberInfoType()).Any(x => x.Count() >= 2)) { continue; } var childColumnInfos = GetColumnInfos(memberInfoType); if (columnInfo.ReferenceType != ReferenceType.None) { childTableInfoPlan = GetTableInfo(memberInfoType, childColumnInfos, members); } var newPrefix = JoinStrings(capturedPrefix, columnInfo.ReferenceType != ReferenceType.None ? "" : (columnInfo.ComplexPrefix ?? columnInfo.MemberInfo.Name)); childrenPlans = GetPocoMembers(childColumnInfos, members, newPrefix).ToArray(); } MemberInfo capturedMemberInfo = columnInfo.MemberInfo; ColumnInfo capturedColumnInfo = columnInfo; var accessors = GetMemberAccessors(members); var memberType = capturedMemberInfo.GetMemberInfoType(); var isList = IsList(capturedMemberInfo); var listType = GetListType(memberType, isList); var isDynamic = capturedMemberInfo.IsDynamic(); var fastCreate = GetFastCreate(memberType, Mapper, isList, isDynamic); var columnName = GetColumnName(capturedPrefix, capturedColumnInfo.ColumnName ?? capturedMemberInfo.Name); var memberInfoData = new MemberInfoData(capturedMemberInfo); yield return(tableInfo => { var pc = new PocoColumn { ReferenceType = capturedColumnInfo.ReferenceType, TableInfo = tableInfo, MemberInfoData = memberInfoData, MemberInfoChain = members, ColumnName = columnName, ResultColumn = capturedColumnInfo.ResultColumn, ForceToUtc = capturedColumnInfo.ForceToUtc, ComputedColumn = capturedColumnInfo.ComputedColumn, ComputedColumnType = capturedColumnInfo.ComputedColumnType, ColumnType = capturedColumnInfo.ColumnType, ColumnAlias = capturedColumnInfo.ColumnAlias, VersionColumn = capturedColumnInfo.VersionColumn, VersionColumnType = capturedColumnInfo.VersionColumnType, SerializedColumn = capturedColumnInfo.SerializedColumn, ValueObjectColumn = capturedColumnInfo.ValueObjectColumn, }; if (pc.ValueObjectColumn) { SetupValueObject(pc, fastCreate); } pc.SetMemberAccessors(accessors); var childrenTableInfo = childTableInfoPlan == null ? tableInfo : childTableInfoPlan(); var children = childrenPlans.Select(plan => plan(childrenTableInfo)).ToList(); // Cascade ResultColumn down foreach (var child in children.Where(child => child.PocoColumn != null && pc.ResultColumn)) { child.PocoColumn.ResultColumn = true; } var pocoMember = new PocoMember() { MemberInfoData = memberInfoData, MemberInfoChain = members, IsList = isList, IsDynamic = isDynamic, PocoColumn = capturedColumnInfo.ComplexMapping ? null : pc, ReferenceType = capturedColumnInfo.ReferenceType, ReferenceMemberName = capturedColumnInfo.ReferenceMemberName, PocoMemberChildren = children, }; pocoMember.SetMemberAccessor(accessors[accessors.Count - 1], fastCreate, listType); return pocoMember; }); } }
protected virtual ColumnInfo GetColumnInfo(MemberInfo mi, Type type) { return(ColumnInfo.FromMemberInfo(mi)); }
public static ColumnInfo FromMemberInfo(MemberInfo mi) { var ci = new ColumnInfo { MemberInfo = mi }; var attrs = ReflectionUtils.GetCustomAttributes(mi).ToArray(); var colAttrs = attrs.OfType <ColumnAttribute>().ToArray(); var columnTypeAttrs = attrs.OfType <ColumnTypeAttribute>().ToArray(); var ignoreAttrs = attrs.OfType <IgnoreAttribute>().ToArray(); var complexMapping = attrs.OfType <ComplexMappingAttribute>().ToArray(); var serializedColumnAttributes = attrs.OfType <SerializedColumnAttribute>().ToArray(); var reference = attrs.OfType <ReferenceAttribute>().ToArray(); var aliasColumn = attrs.OfType <AliasAttribute>().FirstOrDefault(); // Check if declaring poco has [ExplicitColumns] attribute var explicitColumns = mi.DeclaringType.GetTypeInfo().GetCustomAttributes(typeof(ExplicitColumnsAttribute), true).Any(); // Ignore column if declarying poco has [ExplicitColumns] attribute // and property doesn't have an explicit [Column] attribute, // or property has an [Ignore] attribute if ((explicitColumns && !colAttrs.Any() && !reference.Any() && !complexMapping.Any()) || ignoreAttrs.Any()) { ci.IgnoreColumn = true; } var complexMappingAttribute = mi.GetMemberInfoType().GetCustomAttribute <ComplexMappingAttribute>(); if (complexMapping.Any()) { ci.ComplexMapping = complexMapping.First().ComplexMapping; ci.ComplexPrefix = complexMapping.First().CustomPrefix; } else if (complexMappingAttribute != null) { ci.ComplexMapping = complexMappingAttribute.ComplexMapping; ci.ComplexPrefix = complexMappingAttribute.CustomPrefix; } else if (mi.GetMemberInfoType().GetInterfaces().Any(x => x == typeof(IValueObject))) { ci.ValueObjectColumn = true; } else if (serializedColumnAttributes.Any()) { ci.SerializedColumn = true; } else if (reference.Any()) { ci.ReferenceType = reference.First().ReferenceType; ci.ReferenceMemberName = reference.First().ReferenceMemberName ?? "Id"; ci.ColumnName = reference.First().ColumnName ?? mi.Name + "Id"; return(ci); } else if (PocoDataBuilder.IsList(mi)) { ci.ReferenceType = ReferenceType.Many; return(ci); } else if (mi.GetMemberInfoType().IsAClass() && !colAttrs.Any()) { ci.ComplexMapping = true; } // Read attribute if (colAttrs.Any()) { ci.ColumnName = colAttrs.FirstOrDefault(x => !string.IsNullOrEmpty(x.Name))?.Name ?? mi.Name; ci.ForceToUtc = colAttrs.All(x => x.ForceToUtc); ci.ExactColumnNameMatch = colAttrs.All(x => x.ExactNameMatch); var resultAttr = colAttrs.OfType <ResultColumnAttribute>().FirstOrDefault(); ci.ResultColumn = resultAttr != null; if (!ci.ResultColumn) { var versionAttr = colAttrs.OfType <VersionColumnAttribute>().FirstOrDefault(); ci.VersionColumn = versionAttr != null; ci.VersionColumnType = versionAttr?.VersionColumnType ?? ci.VersionColumnType; } if (!ci.VersionColumn && !ci.ResultColumn) { var computedAttr = colAttrs.OfType <ComputedColumnAttribute>().FirstOrDefault(); ci.ComputedColumn = computedAttr != null; ci.ComputedColumnType = computedAttr?.ComputedColumnType ?? ComputedColumnType.Always; } } else { ci.ColumnName = mi.Name; } ci.ColumnAlias = aliasColumn?.Alias; if (columnTypeAttrs.Any()) { ci.ColumnType = columnTypeAttrs.First().Type; } return(ci); }