예제 #1
0
 private static FastCreate GetFastCreate(Type memberType, MapperCollection mapperCollection, bool isList, bool isDynamic)
 {
     return(memberType.IsAClass() || isDynamic
                ? (new FastCreate(isList
                     ? memberType.GetGenericArguments().First()
                     : memberType, mapperCollection))
                : null);
 }
예제 #2
0
        public static Func <object, object> GetConverter(MapperCollection mapper, PocoColumn pc, Type srcType, Type dstType)
        {
            Func <object, object> converter = null;

            // Get converter from the mapper
            if (mapper != null)
            {
                converter = pc?.MemberInfoData != null?mapper.Find(x => x.GetFromDbConverter(pc.MemberInfoData.MemberInfo, srcType)) : mapper.Find(x => x.GetFromDbConverter(dstType, srcType));

                if (converter != null)
                {
                    return(converter);
                }
            }

            if (pc != null &&
                pc.SerializedColumn)
            {
                converter = src => DatabaseFactory.ColumnSerializer.Deserialize((string)src, dstType);
                return(converter);
            }

            // Standard DateTime->Utc mapper
            if (pc != null &&
                pc.ForceToUtc &&
                srcType == typeof(DateTime) &&
                (dstType == typeof(DateTime) || dstType == typeof(DateTime?)))
            {
                converter = src => new DateTime(((DateTime)src).Ticks, DateTimeKind.Utc);
                return(converter);
            }

            //Todo:The following changes were done to NPoco

            #region "Changes"

            // DateTime->DateTimeOffset
            if (pc != null &&
                srcType == typeof(DateTime) &&
                (dstType == typeof(DateTimeOffset) || dstType == typeof(DateTimeOffset?)))
            {
                converter = src => new DateTimeOffset((DateTime)src);
                return(converter);
            }

            // DateTimeOffset --> DateTime
            if (pc != null &&
                srcType == typeof(DateTimeOffset) &&
                (dstType == typeof(DateTime) || dstType == typeof(DateTime?)))
            {
                converter = src => (DateTime)src;
                return(converter);
            }

            #endregion

            // Forced type conversion including integral types -> enum
            var underlyingType = UnderlyingTypes.Get(dstType, () => Nullable.GetUnderlyingType(dstType));
            if (dstType.GetTypeInfo().IsEnum ||
                (underlyingType != null && underlyingType.GetTypeInfo().IsEnum))
            {
                if (srcType == typeof(string))
                {
                    converter = src => EnumMapper.EnumFromString(underlyingType ?? dstType, (string)src);
                    return(converter);
                }

                if (IsIntegralType(srcType))
                {
                    converter = src => Enum.ToObject(underlyingType ?? dstType, src);
                    return(converter);
                }
            }
            else if (!dstType.IsAssignableFrom(srcType))
            {
                converter = src => Convert.ChangeType(src, underlyingType ?? dstType, null);
            }
            return(converter);
        }
예제 #3
0
 public PocoData(Type type, MapperCollection mapper) : this()
 {
     this.Type   = type;
     this.Mapper = mapper;
 }
예제 #4
0
 public PocoDataFactory(MapperCollection mapper)
 {
     this._mapper = mapper;
 }
예제 #5
0
 public PocoDataBuilder(Type type, MapperCollection mapper)
 {
     this.Type   = type;
     this.Mapper = mapper;
 }
예제 #6
0
        private IEnumerable <PocoMemberPlan> GetPocoMembers(MapperCollection mapper, ColumnInfo[] columnInfos, List <MemberInfo> memberInfos, string prefix = null)
        {
            MemberInfo[] capturedMembers = memberInfos.ToArray();
            string       capturedPrefix  = prefix;

            foreach (ColumnInfo columnInfo in columnInfos)
            {
                if (columnInfo.IgnoreColumn)
                {
                    continue;
                }

                Type memberInfoType = columnInfo.MemberInfo.GetMemberInfoType();
                if (columnInfo.ReferenceType == ReferenceType.Many)
                {
                    memberInfoType = memberInfoType.GetGenericArguments().First();
                }

                PocoMemberPlan[]  childrenPlans      = new PocoMemberPlan[0];
                TableInfoPlan     childTableInfoPlan = null;
                List <MemberInfo> 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;
                    }

                    ColumnInfo[] childColumnInfos = this.GetColumnInfos(memberInfoType);

                    if (columnInfo.ReferenceType != ReferenceType.None)
                    {
                        childTableInfoPlan = this.GetTableInfo(memberInfoType, childColumnInfos, members);
                    }

                    string newPrefix = JoinStrings(capturedPrefix, columnInfo.ReferenceType != ReferenceType.None ? "" : (columnInfo.ComplexPrefix ?? columnInfo.MemberInfo.Name));

                    childrenPlans = this.GetPocoMembers(mapper, childColumnInfos, members, newPrefix).ToArray();
                }

                MemberInfo capturedMemberInfo = columnInfo.MemberInfo;
                ColumnInfo capturedColumnInfo = columnInfo;

                List <MemberAccessor> accessors = this.GetMemberAccessors(members);
                Type           memberType       = capturedMemberInfo.GetMemberInfoType();
                bool           isList           = IsList(capturedMemberInfo);
                Type           listType         = GetListType(memberType, isList);
                bool           isDynamic        = capturedMemberInfo.IsDynamic();
                FastCreate     fastCreate       = GetFastCreate(memberType, mapper, isList, isDynamic);
                string         columnName       = this.GetColumnName(capturedPrefix, capturedColumnInfo.ColumnName ?? capturedMemberInfo.Name);
                MemberInfoData memberInfoData   = new MemberInfoData(capturedMemberInfo);

                yield return(tableInfo =>
                {
                    PocoColumn 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);

                    TableInfo childrenTableInfo = childTableInfoPlan == null ? tableInfo : childTableInfoPlan();
                    List <PocoMember> children = childrenPlans.Select(plan => plan(childrenTableInfo)).ToList();

                    // Cascade ResultColumn down
                    foreach (PocoMember child in children.Where(child => child.PocoColumn != null && pc.ResultColumn))
                    {
                        child.PocoColumn.ResultColumn = true;
                    }

                    PocoMember 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;
                });
            }
        }
예제 #7
0
 public FastCreate(Type type, MapperCollection mapperCollection)
 {
     this._type             = type;
     this._mapperCollection = mapperCollection;
     this.CreateDelegate    = this.GetCreateDelegate();
 }