Beispiel #1
0
        public Page <T> FindAll <T>(PageRequest r, Criteria criteria = null, RelationshipOption rOption = null)
        {
            var meta = _context.GetMetaData(typeof(T));

            SetDefaultSortIfNotExist(r, meta);

            var query = CypherTranslator.FindAll(meta, r, criteria);

            using var session = NewSession();
            return(session.ReadTransaction(tx =>
            {
                var records = tx.Run(query.Items).ToList();
                var total = tx.Run(query.Count).Single()[0].As <long>();
                var loadRelationship = ShouldLoadRelationship(rOption);

                var items = EntityTransform.NewInstanceList(records, meta, iNode =>
                {
                    var entity = EntityTransform.NewInstance(iNode, meta);
                    if (!loadRelationship)
                    {
                        return entity;
                    }

                    FetchRelationshipByNodeId(meta, entity, tx, rOption.Depth, new Dictionary <long, object>());
                    return entity;
                }).As <IList <T> >();

                return new Page <T>(r)
                {
                    Items = items,
                    TotalItems = total
                };
            }));
        }
Beispiel #2
0
        public T FindById <T>(long id, RelationshipOption option = null)
        {
            var meta = _context.GetMetaData(typeof(T));

            using var session = NewSession();
            return(session.ReadTransaction(tx =>
            {
                var nodeRecord = tx.Run(CypherTranslator.FindNodeById(meta, id)).SingleOrDefault();
                if (nodeRecord == null)
                {
                    return default;
                }

                var entity = EntityTransform.NewInstance(nodeRecord, meta);
                if (ShouldLoadRelationship(option))
                {
                    FetchRelationshipByNodeId(meta, entity, tx, option.Depth, new Dictionary <long, object>());
                }

                return entity.As <T>();
            }));
        }
Beispiel #3
0
        private void FetchRelationshipByNodeId(Meta meta, object entity, ITransaction tx, int depth, IDictionary <long, object> loaded)
        {
            if (depth == 0)
            {
                return;
            }

            var id = meta.GetId(entity).As <long>();

            loaded.Add(id, entity);

            foreach (var rp in meta.RelationshipProperties)
            {
                var rMeta    = _context.GetMetaData(rp.EntityType);
                var iRecords = tx.Run(CypherTranslator.FindRelationshipNodesById(meta, id, rp)).ToList();
                if (iRecords.Count == 0)
                {
                    continue;
                }

                var relatedEntityList = EntityTransform.NewInstanceList(iRecords, rMeta, iRecord =>
                {
                    var iNode = iRecord[0].As <INode>();

                    // handle cycle relationship
                    if (loaded.TryGetValue(iNode.Id, out var exist))
                    {
                        return(exist);
                    }

                    var rEntity = EntityTransform.NewInstance(iNode, rMeta);
                    FetchRelationshipByNodeId(rMeta, rEntity, tx, depth - 1, loaded);
                    return(rEntity);
                });

                rp.SetValue(entity, rp.IsCollection ? relatedEntityList : relatedEntityList[0]);
            }
        }