Exemplo n.º 1
0
        private object GetValue(DTEntity e, Func <object, object> transformValue, bool isReadOnly)
        {
            var ve = e as DTEValue;

            if (ve != null)
            {
                return(transformValue(ve.Value));
            }

            var le = e as DTEList;

            if (le != null)
            {
                var list  = le.GetObjects();
                var value = list.Select((item) =>
                {
                    if (item.IsSingleValue)
                    {
                        return(item.GetValue());
                    }
                    return(item);
                }).ToArray();
                return(transformValue(value));
            }

            var oe = e as DTEObject;

            if (oe != null)
            {
                var value = DTOPool.CreateObject(oe, isReadOnly, oe.IsPinned);
                return(transformValue(value));
            }
            return(null);
        }
Exemplo n.º 2
0
        private void SetList(List <DTObject> items)
        {
            Items = DTOPool.CreateObjectList(this, this.IsPinned);

            if (items.Count == 0)
            {
                this.ItemTemplate = this.IsPinned ? DTObject.Create() : DTObject.CreateReusable();
            }
            else
            {
                if (items.Count == 1)
                {
                    DTObject item = items[0];
                    if (item.ContainsData())
                    {
                        Items.Add(item);
                    }
                    this.ItemTemplate = item.Clone();
                }
                else
                {
                    foreach (var item in items)
                    {
                        Items.Add(item);
                    }
                    this.ItemTemplate = Items[0].Clone();
                }
                this.ItemTemplate.ForceClearData();
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// 向集合追加一个成员
        /// </summary>
        /// <param name="findExp"></param>
        /// <param name="member"></param>
        public void Push(string findExp, DTObject member)
        {
            ValidateReadOnly();

            DTEList entity = FindEntity <DTEList>(findExp, false);

            if (entity == null)
            {
                var query = QueryExpression.Create(findExp);
                _root.SetEntity(query, (name) =>
                {
                    var dte  = DTOPool.CreateDTEList(this.IsPinned);
                    dte.Name = name;
                    return(dte);
                });
                entity = FindEntity <DTEList>(findExp, true);
            }
            ;
            if (member == null)
            {
                return;
            }

            entity.Push(member);
        }
Exemplo n.º 4
0
 public DTObjects GetObjects()
 {
     if (_objects == null)
     {
         _objects = DTOPool.CreateDTOjects(Items, this.IsPinned);
     }
     return(_objects);
 }
Exemplo n.º 5
0
 public static DTEObject Deserialize(string code, bool isReadOnly, bool isPinned)
 {
     if (string.IsNullOrEmpty(code))
     {
         return(DTOPool.CreateDTEObject(isPinned));
     }
     return(Deserialize(new StringSegment(code), isReadOnly, isPinned));
 }
Exemplo n.º 6
0
 public IEnumerable <DTEntity> GetSelfEntities()
 {
     if (_selfEntities == null)
     {
         var es = DTOPool.CreateDTEntities(this.IsPinned);
         es.Add(this);
         _selfEntities = es;
     }
     return(_selfEntities);
 }
Exemplo n.º 7
0
        public DTObject GetObject(string findExp, bool throwError)
        {
            var entity = this.FindEntity <DTEObject>(findExp, throwError);

            if (entity == null)
            {
                return(null);
            }
            return(DTOPool.CreateObject(entity, this.IsReadOnly, this.IsPinned));
        }
Exemplo n.º 8
0
        public DTObject GetObject(string findExp, DTObject defaultValue)
        {
            var entity = this.FindEntity <DTEObject>(findExp, false);

            if (entity == null)
            {
                return(defaultValue);
            }
            return(DTOPool.CreateObject(entity, this.IsReadOnly, this.IsPinned));
        }
Exemplo n.º 9
0
        private static DTObject CreateDTObject(DTEntity root, bool isReadOnly, bool isPinned)
        {
            var o = root as DTEObject;
            if (o != null) return DTOPool.CreateObject(o, isReadOnly, isPinned);

            var members = DTOPool.CreateDTEntities(isPinned);
            members.Add(root);

            o = DTOPool.CreateDTEObject(members, isPinned);
            return DTOPool.CreateObject(o, isReadOnly, isPinned);
        }
Exemplo n.º 10
0
        public Dictionary <string, object> GetDictionary(string findExp, bool throwError)
        {
            var entities   = this.FindEntities(findExp, throwError);
            var dictionary = DTOPool.CreateDictionary(this.IsPinned);

            foreach (var entity in entities)
            {
                var key   = entity.Name;
                var value = CreateEntityValue(entity);
                dictionary.Add(key, value);
            }
            return(dictionary);
        }
Exemplo n.º 11
0
        /// <summary>
        /// 设置成员,当成员存在时覆盖,成员不存在时新增
        /// </summary>
        /// <param name="findExp"></param>
        /// <param name="value"></param>
        public override void SetEntity(QueryExpression query, Func <string, DTEntity> createEntity)
        {
            var segment = query.Segment;

            var entity = _entities.FirstOrDefault((m) =>
            {
                return(m.Name.Equals(segment, StringComparison.OrdinalIgnoreCase));
            });

            if (entity != null)
            {
                if (query.Next == null)
                {
                    //覆盖,此处并没有改变name
                    var index = _entities.IndexOf(entity);
                    var e     = createEntity(segment);
                    e.Parent         = this;
                    _entities[index] = e;
                    Changed();
                }
                else
                {
                    //由于表达式还未完,所以在下个节点中继续执行
                    entity.SetEntity(query.Next, createEntity);
                }
            }
            else
            {
                //没有找到
                if (query.Next == null)
                {
                    //没有后续
                    var e = createEntity(segment);
                    e.Parent = this;
                    _entities.Add(e);
                    //OrderSelfMembers(); //重新排序
                    Changed();
                }
                else
                {
                    var next = DTOPool.CreateDTEObject(this.IsPinned);
                    next.Name   = segment;
                    next.Parent = this;
                    _entities.Add(next);
                    //OrderSelfMembers(); //重新排序
                    Changed();

                    next.SetEntity(query.Next, createEntity);
                }
            }
        }
Exemplo n.º 12
0
        private static DTEObject Deserialize(StringSegment code, bool isReadOnly, bool isPinned)
        {
            if (code.IsEmpty()) return DTOPool.CreateDTEObject(isPinned);
            var node = ParseNode(CodeType.Object, code);

            DTEObject result = null;
            using (var temp = DTOPool.EntitiesPool.Borrow())
            {
                List<DTEntity> entities = temp.Item;
                FillEntities(entities, node, isReadOnly, isPinned);
                result = entities.First() as DTEObject;
            }
            return result;
        }
Exemplo n.º 13
0
        /// <summary>
        /// 仅克隆结构
        /// </summary>
        /// <returns></returns>
        public override DTEntity Clone()
        {
            var copy = DTOPool.CreateDTEntities(this.IsPinned);

            for (var i = 0; i < _entities.Count; i++)
            {
                copy.Add(_entities[i].Clone());
            }

            var dte = DTOPool.CreateDTEObject(copy, this.IsPinned);

            dte.Name = this.Name;
            return(dte);
        }
Exemplo n.º 14
0
 /// <summary>
 /// 该构造函数仅供克隆时使用
 /// </summary>
 /// <param name="template"></param>
 /// <param name="items"></param>
 internal void InitByClone(string name, DTObject template, DTObjectList items, bool isPinned)
 {
     this.IsPinned     = isPinned;
     this.Name         = name;
     this.ItemTemplate = template;
     this.Items        = DTOPool.CreateObjectList(this, this.IsPinned);
     if (items.Count > 0)
     {
         foreach (var item in items)
         {
             this.Items.Add(item.Clone());
         }
     }
 }
Exemplo n.º 15
0
        public override IEnumerable <DTEntity> FindEntities(QueryExpression query)
        {
            if (query.IsSelfEntities)
            {
                return(this.GetSelfEntities());                      //*代表返回对象自己
            }
            List <DTEntity> list = DTOPool.CreateDTEntities(this.IsPinned);

            foreach (var e in Items)
            {
                var es = e.GetRoot().FindEntities(query);
                list.AddRange(es);
            }
            return(list);
        }
Exemplo n.º 16
0
        private DTEList CreateListEntity(string name, IEnumerable list)
        {
            var dte = DTOPool.CreateDTEList(this.IsPinned);

            dte.Name = name;

            foreach (var item in list)
            {
                dte.CreateAndPush((dto) =>
                {
                    dto.SetValue(item);
                });
            }
            return(dte);
        }
Exemplo n.º 17
0
        private static void FillEntities(List <DTEntity> entities, CodeTreeNode node, bool isReadOnly, bool isPinned)
        {
            var name = JSON.GetStringValue(node.Name.ToString());

            if (node.Type == CodeType.Object)
            {
                var members = DTOPool.CreateDTEntities(isPinned);
                //收集成员
                foreach (CodeTreeNode item in node.Childs)
                {
                    FillEntities(members, item, isReadOnly, isPinned);
                }

                var obj = DTOPool.CreateDTEObject(members, isPinned);
                obj.Name = name;
                entities.Add(obj);
            }
            else if (node.Type == CodeType.List)
            {
                var childs = DTOPool.CreateObjects(isPinned);
                using (var temp = DTOPool.EntitiesPool.Borrow())
                {
                    //收集成员
                    var tempChilds = temp.Item;
                    foreach (CodeTreeNode item in node.Childs)
                    {
                        FillEntities(tempChilds, item, isReadOnly, isPinned);
                    }

                    foreach (var e in tempChilds)
                    {
                        var item = CreateDTObject(e, isReadOnly, isPinned);
                        childs.Add(item);
                    }
                }

                var list = DTOPool.CreateDTEList(childs, isPinned);
                list.Name = name;
                entities.Add(list);
            }
            else
            {
                object value = node.Type == CodeType.StringValue ? JSON.GetStringValue(node.Value) : JSON.GetValue(node.Value);
                var    dte   = DTOPool.CreateDTEValue(name, value, isPinned);
                entities.Add(dte);
            }
        }
Exemplo n.º 18
0
        private DTEList GetOrCreateList(string findExp)
        {
            DTEList entity = FindEntity <DTEList>(findExp, false);

            if (entity == null)
            {
                var query = QueryExpression.Create(findExp);
                _root.SetEntity(query, (name) =>
                {
                    var dte  = DTOPool.CreateDTEList(this.IsPinned);
                    dte.Name = name;
                    return(dte);
                });
                entity = FindEntity <DTEList>(findExp, true);
            }
            return(entity);
        }
Exemplo n.º 19
0
        /// <summary>
        /// 如果不存在findExp对应的列表,那么创建
        /// </summary>
        /// <param name="findExp"></param>
        public void SetList(string findExp)
        {
            ValidateReadOnly();

            DTEList entity = FindEntity <DTEList>(findExp, false);

            if (entity == null)
            {
                var query = QueryExpression.Create(findExp);
                _root.SetEntity(query, (name) =>
                {
                    var dte  = DTOPool.CreateDTEList(this.IsPinned);
                    dte.Name = name;
                    return(dte);
                });
            }
            ;
        }
Exemplo n.º 20
0
        internal DTEntity[] FindEntities(string findExp, bool throwError)
        {
            var query = QueryExpression.Create(findExp);

            List <DTEntity> list = DTOPool.CreateDTEntities(this.IsPinned);
            var             es   = _root.FindEntities(query);

            list.AddRange(es);

            if (list.Count == 0)
            {
                if (throwError)
                {
                    throw new NotFoundDTEntityException("没有找到" + findExp + "对应的DTO实体!");
                }
                return(list.ToArray());
            }
            return(list.ToArray());
        }
Exemplo n.º 21
0
        public object GetValue(string findExp, bool throwError)
        {
            DTEntity entity = FindEntity(findExp, throwError);

            if (entity == null)
            {
                return(null);
            }
            switch (entity.Type)
            {
            case DTEntityType.Value:
            {
                var ev = entity as DTEValue;
                if (ev != null)
                {
                    return(ev.Value);
                }
            }
            break;

            case DTEntityType.Object:
            {
                var eo = entity as DTEObject;
                if (eo != null)
                {
                    return(DTOPool.CreateObject(eo, this.IsReadOnly, this.IsPinned));
                }
            }
            break;

            case DTEntityType.List:
            {
                var el = entity as DTEList;
                if (el != null)
                {
                    return(el.GetObjects());
                }
            }
            break;
            }
            return(null);
        }
Exemplo n.º 22
0
        private DTEntity CreateEntity(string name, object value)
        {
            if (IsList(value))
            {
                var list = value as IEnumerable;
                if (list != null)
                {
                    return(CreateListEntity(name, list));
                }
            }

            var dto = value as DTObject;

            if (dto != null)
            {
                var root = dto.GetRoot();
                root.Name = name;
                return(root);
            }
            else
            {
                return(DTOPool.CreateDTEValue(name, value, this.IsPinned));
            }
        }
Exemplo n.º 23
0
        private object GetValue(DTEntity entity)
        {
            switch (entity.Type)
            {
            case DTEntityType.Value:
            {
                var ev = entity as DTEValue;
                if (ev != null)
                {
                    return(ev.Value);
                }
            }
            break;

            case DTEntityType.Object:
            {
                var eo = entity as DTEObject;
                if (eo != null)
                {
                    return(DTOPool.CreateObject(eo, this.IsReadOnly, this.IsPinned));
                }
            }
            break;

            case DTEntityType.List:
            {
                var el = entity as DTEList;
                if (el != null)
                {
                    return(el.GetObjects());
                }
            }
            break;
            }
            return(null);
        }
Exemplo n.º 24
0
        //public Dictionary<string, T> GetDictionary<T>()
        //{
        //    return GetDictionary<T>(string.Empty, false);
        //}

        //public Dictionary<string, T> GetDictionary<T>(string findExp)
        //{
        //    return GetDictionary<T>(findExp, false);
        //}

        ///// <summary>
        ///// 本质上来说,json就是一组键值对,因此可以获取键值对形式的值
        ///// </summary>
        ///// <typeparam name="T"></typeparam>
        ///// <param name="findExp"></param>
        ///// <param name="throwError"></param>
        ///// <returns></returns>
        //public Dictionary<string, T> GetDictionary<T>(string findExp, bool throwError)
        //{
        //    var entities = this.FindEntities(findExp, throwError);
        //    var dictionary = new Dictionary<string, T>(entities.Length);
        //    foreach (var entity in entities)
        //    {
        //        var key = entity.Name;
        //        var value = CreateEntityValue(entity);
        //        if (value is T)
        //            dictionary.Add(key, (T)value);
        //    }
        //    return dictionary;
        //}

        private object CreateEntityValue(DTEntity entity)
        {
            switch (entity.Type)
            {
            case DTEntityType.Value:
            {
                var temp = entity as DTEValue;
                if (temp != null)
                {
                    return(temp.Value);
                }
            }
            break;

            case DTEntityType.Object:
            {
                var temp = entity as DTEObject;
                if (temp != null)
                {
                    return(DTOPool.CreateObject(temp, this.IsReadOnly, this.IsPinned));
                }
            }
            break;

            case DTEntityType.List:
            {
                var temp = entity as DTEList;
                if (temp != null)
                {
                    return(temp.GetObjects());
                }
            }
            break;
            }
            throw new DTOException("在CreateEntityValue发生未知的错误,entity类型为" + entity.GetType());
        }
Exemplo n.º 25
0
        /// <summary>
        /// 该方法用于更改成员的值
        /// </summary>
        /// <param name="dto"></param>
        /// <param name="findExp"></param>
        /// <param name="valueFindExp"></param>
        /// <param name="transformValue"></param>
        public void ChangeValue(DTObject dto, string findExp, string valueFindExp, Func <object, object> transformValue)
        {
            ArgumentAssert.IsNotNullOrEmpty(findExp, "findExp");
            ArgumentAssert.IsNotNullOrEmpty(valueFindExp, "valueFindExp");

            //1.找出需要赋值的目标成员
            var targets = dto.FindEntities(findExp, false);

            if (targets.Length == 0)
            {
                dto.SetValue(findExp, string.Empty);                      //如果没有成员,就自动生成
            }
            targets = dto.FindEntities(findExp, false);


            var valueExpression = _getValueExpression(valueFindExp);

            foreach (var target in targets)
            {
                var parent = target.Parent as DTEObject;
                if (parent == null)
                {
                    throw new DTOException("预期之外的错误," + valueExpression.FindExp);
                }

                var parentDTO = valueExpression.StartRoot ? dto : DTOPool.CreateObject(parent, dto.IsReadOnly, dto.IsPinned);

                //2.找出值,值是在目标成员所在的对象下进行查找的
                var entities = parentDTO.FindEntities(valueExpression.FindExp, false);
                if (entities.Length == 1)
                {
                    //获取值
                    var ve       = entities[0];
                    var newValue = GetValue(ve, transformValue, dto.IsReadOnly);
                    if (newValue == null)
                    {
                        throw new DTOException("预期之外的数据转换," + valueExpression.FindExp);
                    }


                    //目标值是唯一的,这个时候要进一步判断
                    var valueObjParent = ve.Parent.Parent as DTEList;         //这是值所在的对象的父亲
                    //if (valueObjFather != null && ve!=target)  //如果值所在的对象处在集合中,并且不是自身对自身赋值,那么还是要以集合形式赋值
                    if (valueObjParent != null && ve.Parent != target.Parent) //如果值所在的对象处在集合中,并且不是自身对象对自身对象赋值,那么还是要以集合形式赋值
                    {
                        //以集合赋值
                        SetValue(target, new object[] { newValue }, valueExpression.FindExp);
                    }
                    else
                    {
                        //赋单值
                        SetValue(target, newValue, valueExpression.FindExp);
                    }
                }
                else if (entities.Length > 1)
                {
                    //如果目标值是多个,那么是集合类型,这时候需要收集所有转换后的值,再赋值
                    List <object> values = new List <object>(entities.Length);
                    foreach (var e in entities)
                    {
                        var newValue = GetValue(e, transformValue, dto.IsReadOnly);
                        if (newValue == null)
                        {
                            throw new DTOException("预期之外的数据转换," + valueExpression.FindExp);
                        }
                        values.Add(newValue);
                    }

                    SetValue(target, values, valueExpression.FindExp);
                }
                else
                {
                    //值为0,需要判断是否为数组
                }
            }
        }
Exemplo n.º 26
0
 public void Init(DTEList owner, bool isPinned)
 {
     _owner        = owner;
     this.IsPinned = isPinned;
     _list         = DTOPool.CreateObjects(isPinned);
 }
Exemplo n.º 27
0
 /// <summary>
 /// 克隆
 /// </summary>
 /// <returns></returns>
 public override DTEntity Clone()
 {
     return(DTOPool.CreateDTEValue(this.Name, Clone(this.Value), this.IsPinned));
 }
Exemplo n.º 28
0
 public void Init(bool isPinned)
 {
     this.IsPinned = isPinned;
     this.SetMembers(DTOPool.CreateDTEntities(isPinned));
 }
Exemplo n.º 29
0
 public DTObject Clone()
 {
     return(DTOPool.CreateObject(_root.Clone() as DTEObject, this.IsReadOnly, this.IsPinned));
 }
Exemplo n.º 30
0
        /// <summary>
        /// 完整的创建方法
        /// </summary>
        /// <param name="code"></param>
        /// <param name="isReadOnly"></param>
        /// <param name="isPinned"></param>
        /// <returns></returns>
        private static DTObject CreateComplete(string code, bool isReadOnly, bool isPinned)
        {
            var root = EntityDeserializer.Deserialize(code, isReadOnly, isPinned);

            return(DTOPool.CreateObject(root, isReadOnly, isPinned));
        }