Exemple #1
0
        private void TrySetProperty(Entity e, string pName, JToken value)
        {
            //有些小写的客户端数据被传输到了服务端,需要被过滤掉。
            if (char.IsLower(pName[0]))
            {
                return;
            }

            var pm = _entityMeta.Property(pName) as PropertyMeta ??
                     _entityMeta.ChildrenProperty(pName);

            if (pm != null)
            {
                var mp = pm.ManagedProperty;
                if (mp is IListProperty)
                {
                    //todo: 此处的性能可能需要优化,聚合保存子列表时,重复的查询 Repository
                    var entityType = EntityMatrix.FindByList(pm.PropertyType).EntityType;
                    var repo       = RF.Find(entityType);

                    //列表属性的设置不能使用 SetProperty,否则,list.Parent 将会无值。
                    //但是也不能直接使用 LoadProperty,否则会导致调用 list.MarkOld,从而不会保存这个列表。
                    //所以只能先装载一个空列表,然后再把 json 中的数据转换为实体加入到这个列表中。
                    var list = repo.NewList();
                    e.LoadProperty(mp, list);

                    ListReader.JsonToEntity(value as JObject, repo, list);
                }
                else
                {
                    var rawValue = (value as JValue).Value;

                    rawValue = EntityJsonConverter.ToServerValue(pm.PropertyType, rawValue);

                    e.SetProperty(mp, rawValue, ManagedPropertyChangedSource.FromUIOperating);
                }
            }
            else
            {
                var rawValue = (value as JValue).Value;

                //如果没有找到一般的属性,则尝试查找外键属性
                for (int i = 0, c = _refIdProperties.Count; i < c; i++)
                {
                    var rip = _refIdProperties[i];
                    if (rip.Name == pName)
                    {
                        e.SetRefId(rip, rawValue);
                        break;
                    }
                }
                //只读属性。
                //if(notFound)
                //{
                //    throw new InvalidOperationException("没有在实体中找到这个属性:" + pName);
                //}
            }
        }
Exemple #2
0
        private EntityList DeserializeList(Type listType, JArray jArray)
        {
            var entityType = EntityMatrix.FindByList(listType).EntityType;
            var repo       = RF.Find(entityType);

            //先从数据库中找出所有提供了 Id 的实体。
            var idList = jArray.Cast <JObject>().Select(item => TryGetId(item))
                         .Where(id => id != null).ToArray();
            var list = repo.GetByIdList(idList);

            //依次反序列化数组中的实体:
            //如果有 Id,则在数据库中查询出的列表 list 中查找出对应的实体,然后反序列化值。否则,直接构造新实体。
            foreach (JObject jEntity in jArray)
            {
                var child = FindOrCreate(list, jEntity);
                DeserializeProperties(jEntity, child);
            }

            return(list);
        }