示例#1
0
        /// <summary>
        /// 获取指定属性的值。
        /// </summary>
        /// <param name="property">实体属性。</param>
        /// <returns></returns>
        public virtual PropertyValue GetValue(IProperty property)
        {
            if (property == null)
            {
                return(PropertyValue.Empty);
            }

            var hasValue = InnerEntry.Has(property.Name);
            var value    = PropertyValue.Empty;

            if (hasValue)
            {
                value = InnerEntry[property.Name].GetCurrentValue();
            }
            else if (property.Type.IsValueType)
            {
                value = PropertyValue.NewValue(property.Type.GetDefaultValue(), property.Type);
            }

            //关联属性
            if (!hasValue && property is RelationProperty)
            {
                value = ProcessSupposedProperty(property);
            }

            return(EntityUtility.CheckReturnValue(property, value));
        }
示例#2
0
        /// <summary>
        /// 通过一个 <see cref="MemberInitExpression"/> 表达式将属性值绑定到实体对象中。
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="creator"></param>
        /// <returns></returns>
        public static IEntity InitByExpression(this IEntity entity, LambdaExpression creator)
        {
            if (creator.Body is NewExpression)
            {
                throw new InvalidOperationException(SR.GetString(SRKind.InvalidExpressionInit));
            }

            if (creator.Body is MemberInitExpression initExp)
            {
                if (initExp.NewExpression.Arguments.Count > 0)
                {
                    throw new InvalidOperationException(SR.GetString(SRKind.InvalidExpressionInit));
                }

                foreach (var bind in initExp.Bindings)
                {
                    if (bind as MemberAssignment == null)
                    {
                        continue;
                    }

                    var exp = PartialEvaluator.Eval((bind as MemberAssignment).Expression);
                    if (exp is ConstantExpression constExp)
                    {
                        entity.SetValue((bind as MemberAssignment).Member.Name, PropertyValue.NewValue(constExp.Value, (bind as MemberAssignment).Member.GetMemberType()));
                    }
                }
            }

            return(entity);
        }
示例#3
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);
            }
        }
示例#4
0
        private PropertyValue GetSupportedValue(bool isNull, PropertyMapping mapper, DataRow row)
        {
            if (mapper.Property.Type == typeof(Guid))
            {
                return(isNull ? new Guid?() : new Guid(row[mapper.Index].ToString()));
            }

            var typeCode = Type.GetTypeCode(mapper.Property.Type.GetNonNullableType());

            switch (typeCode)
            {
            case TypeCode.Boolean:
                return(isNull ? new bool?() : Convert.ToBoolean(row[mapper.Index]));

            case TypeCode.Char:
                return(isNull ? new char?() : Convert.ToChar(row[mapper.Index]));

            case TypeCode.Byte:
                return(isNull ? new byte?() : Convert.ToByte(row[mapper.Index]));

            case TypeCode.Int16:
                return(isNull ? new short?() : Convert.ToInt16(row[mapper.Index]));

            case TypeCode.Int32:
                return(isNull ? new int?() : Convert.ToInt32(row[mapper.Index]));

            case TypeCode.Int64:
                return(isNull ? new long?() : Convert.ToInt64(row[mapper.Index]));

            case TypeCode.Decimal:
                return(isNull ? new decimal?() : Convert.ToDecimal(row[mapper.Index]));

            case TypeCode.Double:
                return(isNull ? new double?() : Convert.ToDouble(row[mapper.Index]));

            case TypeCode.String:
                return(isNull ? string.Empty : Convert.ToString(row[mapper.Index]));

            case TypeCode.DateTime:
                return(isNull ? new DateTime?() : Convert.ToDateTime(row[mapper.Index]));

            case TypeCode.Single:
                return(isNull ? new float?() : Convert.ToSingle(row[mapper.Index]));

            default:
                return(PropertyValue.NewValue(row[mapper.Index], null));
            }
        }
示例#5
0
        bool IDeserializeProcessor.SetValue(string name, object value)
        {
            if (EntityDeserializeProcessorScope.Current == null)
            {
                return(false);
            }

            var property = PropertyUnity.GetProperty(_entityType, name);

            if (property != null)
            {
                InitializeValue(property, PropertyValue.NewValue(value, property.Type));
                return(true);
            }

            return(false);
        }
示例#6
0
        private PropertyValue GetConvertedValue(PropertyMapping mapper, bool isNull, Func <int, object> funcGetValue)
        {
            var converter = ConvertManager.GetConverter(mapper.Property.Type);

            if (converter == null)
            {
                throw new NotSupportedException(SR.GetString(SRKind.UnableConvertComplexType, mapper.Property.Type));
            }

            if (isNull)
            {
                return(PropertyValue.Empty);
            }

            var dbType = mapper.Property.Info.DataType ?? DbType.String;

            return(PropertyValue.NewValue(converter.ConvertFrom(funcGetValue(mapper.Index), dbType)));
        }
示例#7
0
        /// <summary>
        /// 根据映射特性设置属性的映射信息。
        /// </summary>
        /// <param name="mapping"></param>
        /// <param name="mapInfo"></param>
        private static void InitMapInfo(PropertyMappingAttribute mapping, PropertyMapInfo mapInfo)
        {
            mapInfo.FieldName    = mapping.ColumnName;
            mapInfo.Description  = mapping.Description;
            mapInfo.GenerateType = mapping.GenerateType;

            if (mapping.GetFlag(PropertyMappingAttribute.SetMark.DataType))
            {
                mapInfo.DataType = mapping.DataType;
            }

            if (mapping.GetFlag(PropertyMappingAttribute.SetMark.IsPrimaryKey))
            {
                mapInfo.IsPrimaryKey = mapping.IsPrimaryKey;
            }

            if (mapping.GetFlag(PropertyMappingAttribute.SetMark.IsDeletedKey))
            {
                mapInfo.IsDeletedKey = mapping.IsDeletedKey;
            }

            if (mapping.DefaultValue != null)
            {
                mapInfo.DefaultValue          = PropertyValue.NewValue(mapping.DefaultValue, mapInfo.ReflectionInfo.PropertyType);
                mapInfo.DefaultValueFormatter = mapping.DefaultValueFormatter;
            }

            if (mapping.GetFlag(PropertyMappingAttribute.SetMark.Length))
            {
                mapInfo.Length = mapping.Length;
            }

            if (mapping.GetFlag(PropertyMappingAttribute.SetMark.Precision))
            {
                mapInfo.Precision = mapping.Precision;
            }

            if (mapping.GetFlag(PropertyMappingAttribute.SetMark.Scale))
            {
                mapInfo.Scale = mapping.Scale;
            }
        }
示例#8
0
        /// <summary>
        /// 通过一个 <see cref="MemberInitExpression"/> 表达式将属性值绑定到实体对象中。
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="factory"></param>
        /// <returns></returns>
        public static IEntity InitByExpression(this IEntity entity, LambdaExpression factory)
        {
            if (factory.Body is MemberInitExpression initExp)
            {
                foreach (var bind in initExp.Bindings)
                {
                    if (bind as MemberAssignment == null)
                    {
                        continue;
                    }

                    var exp = PartialEvaluator.Eval((bind as MemberAssignment).Expression);
                    if (exp is ConstantExpression constExp)
                    {
                        entity.SetValue((bind as MemberAssignment).Member.Name, PropertyValue.NewValue(constExp.Value, (bind as MemberAssignment).Member.GetMemberType()));
                    }
                }
            }

            return(entity);
        }
示例#9
0
        /// <summary>
        /// 获取属性值。
        /// </summary>
        /// <param name="property"></param>
        /// <returns></returns>
        public override PropertyValue GetValue(IProperty property)
        {
            var value = property.Info.ReflectionInfo.GetValue(this, null);

            return(value == null ? PropertyValue.Empty : PropertyValue.NewValue(value, property.Type));
        }