Beispiel #1
0
        private void WriteFields()
        {
            var properties = this.EntityMeta.EntityProperties;

            for (int i = 0, c = properties.Count; i < c; i++)
            {
                var property = properties[i];
                var mp       = property.ManagedProperty;

                //只为树状实体输出以下两个属性。
                if (mp == Entity.TreePIdProperty || mp == Entity.TreeIndexProperty)
                {
                    if (!_entityModel.isTree)
                    {
                        continue;
                    }
                }

                var serverType = ServerTypeHelper.GetServerType(property.PropertyType);
                if (serverType.Name == SupportedServerType.Unknown)
                {
                    continue;
                }

                var pName = property.Name;
                var field = new EntityField
                {
                    name    = pName,
                    type    = serverType,
                    persist = property.Runtime.CanWrite,
                };

                if (mp != null)
                {
                    //为外键添加一个视图属性
                    if (mp is IRefProperty)
                    {
                        _entityModel.fields.Add(field);

                        var refMp = mp as IRefProperty;
                        field = new EntityField
                        {
                            name    = LabeledRefProperty(pName),
                            type    = ServerTypeHelper.GetServerType(typeof(string)),
                            persist = false,
                        };
                    }
                    else
                    {
                        var v = mp.GetMeta(this.EntityMeta.EntityType).DefaultValue;
                        field.defaultValue = EntityJsonConverter.ToClientValue(property.PropertyType, v);
                    }
                }

                _entityModel.fields.Add(field);
            }
        }
Beispiel #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_Display
                        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);
                    }
                }
            }
        }