Ejemplo n.º 1
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)
        {
            var initExp = factory.Body as MemberInitExpression;

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

                    var exp      = PartialEvaluator.Eval(assign.Expression);
                    var constExp = exp as ConstantExpression;
                    if (constExp != null)
                    {
                        entity.SetValue(assign.Member.Name, PropertyValue.New(constExp.Value, assign.Member.GetMemberType()));
                    }
                }
            }

            return(entity);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 通过主键值使对象正常化。
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="keyValues">主键值数组。</param>
        /// <returns></returns>
        public static T Normalize <T>(this T entity, params object[] keyValues) where T : IEntity
        {
            var primaryKeys = PropertyUnity.GetPrimaryProperties(entity.EntityType).ToArray();

            if (primaryKeys.Length != 0 && keyValues == null ||
                primaryKeys.Length != keyValues.Length)
            {
                throw new Exception(SR.GetString(SRKind.DisaccordArgument, primaryKeys.Length, keyValues.Length));
            }

            var extend = entity as IEntityStatefulExtension;

            if (extend == null)
            {
                return(entity);
            }

            for (var i = 0; i < primaryKeys.Length; i++)
            {
                extend.InitializateValue(primaryKeys[i], PropertyValue.New(keyValues[i], primaryKeys[i].Type));
            }

            extend.SetState(EntityState.Modified);

            return(entity);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 根据映射特性设置属性的映射信息。
        /// </summary>
        /// <param name="mapping"></param>
        /// <param name="mapInfo"></param>
        private 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.New(mapping.DefaultValue, mapInfo.ReflectionInfo.PropertyType);
            }

            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;
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 获取指定属性的值。
        /// </summary>
        /// <param name="property">实体属性。</param>
        /// <returns></returns>
        public virtual PropertyValue GetValue(IProperty property)
        {
            var           hasValue = valueEntry.Has(property.Name);
            PropertyValue value    = null;

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

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

            return(EntityUtility.CheckReturnValue(property, value));
        }
Ejemplo n.º 5
0
 void IPropertyAccessorBypass.SetValue(IProperty proprty, object value)
 {
     base.SetValue(proprty, PropertyValue.New(value, proprty.Type));
 }
Ejemplo n.º 6
0
        public override PropertyValue GetValue(IProperty property)
        {
            var value = property.Info.ReflectionInfo.GetValue(this, null);

            return(PropertyValue.New(value, property.Type));
        }