Example #1
0
        /// <summary>
        /// 根据SelectedValuePath指定的值,获取目标属性值
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static object GetSelectedValue(this SelectionViewMeta rvi, Entity entity)
        {
            var selectedValuePath = rvi.SelectedValuePath ?? Entity.IdProperty;

            //如果是一个引用属性,则返回引用属性的 Id
            var refProperty = selectedValuePath as IRefProperty;
            if (refProperty != null)
            {
                var meta = refProperty.GetMeta(entity);
                return refProperty.Nullable ?
                    entity.GetRefNullableId(refProperty.RefIdProperty) :
                    entity.GetRefId(refProperty.RefIdProperty);
            }

            return entity.GetProperty(selectedValuePath);
        }
Example #2
0
        internal static void EntityToJson(EntityViewMeta evm, Entity entity, EntityJson entityJson)
        {
            var isTree = evm.EntityMeta.IsTreeEntity;

            foreach (var propertyVM in evm.EntityProperties)
            {
                var property = propertyVM.PropertyMeta;
                var mp = property.ManagedProperty;
                if (mp != null)
                {
                    //如果非树型实体,则需要排除树型实体的两个属性。
                    if (!isTree && (mp == Entity.TreeIndexProperty || mp == Entity.TreePIdProperty)) { continue; }

                    //引用属性
                    if (propertyVM.IsReference)
                    {
                        var refMp = mp as IRefProperty;
                        object value = string.Empty;
                        var id = entity.GetRefNullableId(refMp.RefIdProperty);
                        if (id != null) { value = id; }

                        var idName = refMp.RefIdProperty.Name;
                        entityJson.SetProperty(idName, value);

                        //同时写入引用属性的视图属性,如 BookCategoryId_Label
                        if (id != null && propertyVM.CanShowIn(ShowInWhere.List))
                        {
                            var titleProperty = propertyVM.SelectionViewMeta.RefTypeDefaultView.TitleProperty;
                            if (titleProperty != null)
                            {
                                var lazyRefEntity = entity.GetRefEntity(refMp.RefEntityProperty);
                                var titleMp = titleProperty.PropertyMeta.ManagedProperty;
                                if (titleMp != null)
                                {
                                    value = lazyRefEntity.GetProperty(titleMp);
                                }
                                else
                                {
                                    value = ObjectHelper.GetPropertyValue(lazyRefEntity, titleProperty.Name);
                                }

                                var name = EntityModelGenerator.LabeledRefProperty(idName);
                                entityJson.SetProperty(name, value);
                            }
                        }
                    }
                    //一般托管属性
                    else
                    {
                        var pRuntimeType = property.PropertyType;
                        var serverType = ServerTypeHelper.GetServerType(pRuntimeType);
                        if (serverType.Name != SupportedServerType.Unknown)
                        {
                            var value = entity.GetProperty(mp);
                            value = ToClientValue(pRuntimeType, value);
                            entityJson.SetProperty(mp.Name, value);
                        }
                    }
                }
                //一般 CLR 属性
                else
                {
                    var pRuntimeType = property.PropertyType;
                    var serverType = ServerTypeHelper.GetServerType(pRuntimeType);
                    if (serverType.Name != SupportedServerType.Unknown)
                    {
                        var value = ObjectHelper.GetPropertyValue(entity, property.Name);
                        value = ToClientValue(pRuntimeType, value);
                        entityJson.SetProperty(property.Name, value);
                    }
                }
            }
        }