/// <summary>
        /// 将一个 <see cref="IDataReader"/> 转换为一个 <typeparamref name="T"/> 的对象。
        /// </summary>
        /// <param name="database">当前的 <see cref="IDatabase"/> 对象。</param>
        /// <param name="reader">一个 <see cref="IDataReader"/> 对象。</param>
        /// <returns>由当前 <see cref="IDataReader"/> 对象中的数据转换成的 <typeparamref name="T"/> 对象实例。</returns>
        public virtual T Map(IDatabase database, IDataReader reader)
        {
            var value = RecordWrapper == null ? reader[0] :
                        RecordWrapper.GetValue(reader, 0);

            var converter = ConvertManager.GetConverter(typeof(T));

            return(converter != null ? (T)converter.ConvertFrom(value, reader.GetFieldType(0).GetDbType()) :
                   value.To <object, T>());
        }
Exemplo n.º 2
0
        private PropertyValue GetSupportedValue(bool isNull, PropertyMapping mapper, IDataReader reader)
        {
            try
            {
                if (mapper.Property.Type == typeof(Guid))
                {
                    return(isNull ? new Guid?() : new Guid(reader.GetValue(mapper.Index).ToString()));
                }

                var typeCode = Type.GetTypeCode(mapper.Property.Type.GetNonNullableType());
                switch (typeCode)
                {
                case TypeCode.Boolean:
                    return(isNull ? new bool?() : RecordWrapper.GetBoolean(reader, mapper.Index));

                case TypeCode.Char:
                    return(isNull ? new char?() : RecordWrapper.GetChar(reader, mapper.Index));

                case TypeCode.Byte:
                    return(isNull ? new byte?() : RecordWrapper.GetByte(reader, mapper.Index));

                case TypeCode.Int16:
                    return(isNull ? new short?() : RecordWrapper.GetInt16(reader, mapper.Index));

                case TypeCode.Int32:
                    return(isNull ? new int?() : RecordWrapper.GetInt32(reader, mapper.Index));

                case TypeCode.Int64:
                    return(isNull ? new long?() : RecordWrapper.GetInt64(reader, mapper.Index));

                case TypeCode.Decimal:
                    return(isNull ? new decimal?() : RecordWrapper.GetDecimal(reader, mapper.Index));

                case TypeCode.Double:
                    return(isNull ? new double?() : RecordWrapper.GetDouble(reader, mapper.Index));

                case TypeCode.String:
                    return(isNull ? string.Empty : RecordWrapper.GetString(reader, mapper.Index));

                case TypeCode.DateTime:
                    return(isNull ? new DateTime?() : RecordWrapper.GetDateTime(reader, mapper.Index));

                case TypeCode.Single:
                    return(isNull ? new float?() : RecordWrapper.GetFloat(reader, mapper.Index));

                default:
                    return(PropertyValue.NewValue(RecordWrapper.GetValue(reader, mapper.Index)));
                }
            }
            catch (Exception ex)
            {
                throw new RowMapperCastException(mapper.Property.Name, mapper.Property.Type, ex);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// 将一个 <see cref="IDataReader"/> 转换为一个 <typeparamref name="T"/> 的对象。
        /// </summary>
        /// <param name="reader">一个 <see cref="IDataReader"/> 对象。</param>
        /// <returns>由当前 <see cref="IDataReader"/> 对象中的数据转换成的 <typeparamref name="T"/> 对象实例。</returns>
        public virtual T Map(IDataReader reader)
        {
            var index = reader.FieldCount != 1 ? 1 : 0;
            var value = RecordWrapper == null ? reader[index] :
                        RecordWrapper.GetValue(reader, index);

            var converter = ConvertManager.GetConverter(typeof(T));

            if (converter != null)
            {
                return((T)converter.ConvertFrom(value, reader.GetFieldType(index).GetDbType()));
            }

            return(value.To <object, T>());
        }