示例#1
0
        private IEnumerable <TResource> GetWhereWithInclude <TResource, TId>(IReadOnlyCollection <TId> ids, RelationshipAttribute[] relationshipsToNextLayer)
            where TResource : class, IIdentifiable <TId>
        {
            if (!ids.Any())
            {
                return(Array.Empty <TResource>());
            }

            ResourceContext  resourceContext  = _resourceContextProvider.GetResourceContext <TResource>();
            FilterExpression filterExpression = CreateFilterByIds(ids, resourceContext);

            var queryLayer = new QueryLayer(resourceContext)
            {
                Filter = filterExpression
            };

            List <ResourceFieldChainExpression> chains = relationshipsToNextLayer.Select(relationship => new ResourceFieldChainExpression(relationship))
                                                         .ToList();

            if (chains.Any())
            {
                queryLayer.Include = IncludeChainConverter.FromRelationshipChains(chains);
            }

            IResourceReadRepository <TResource, TId> repository = GetRepository <TResource, TId>();

            return(repository.GetAsync(queryLayer, CancellationToken.None).Result);
        }
        protected IncludeExpression ParseInclude(int?maximumDepth)
        {
            ResourceFieldChainExpression firstChain = ParseFieldChain(FieldChainRequirements.IsRelationship, "Relationship name expected.");

            List <ResourceFieldChainExpression> chains = firstChain.AsList();

            while (TokenStack.Any())
            {
                EatSingleCharacterToken(TokenKind.Comma);

                ResourceFieldChainExpression nextChain = ParseFieldChain(FieldChainRequirements.IsRelationship, "Relationship name expected.");
                chains.Add(nextChain);
            }

            ValidateMaximumIncludeDepth(maximumDepth, chains);

            return(IncludeChainConverter.FromRelationshipChains(chains));
        }
示例#3
0
        private IEnumerable <IQueryConstraintProvider> GetIncludeConstraints(IEnumerable <IEnumerable <RelationshipAttribute> > inclusionChains = null)
        {
            var expressionsInScope = new List <ExpressionInScope>();

            if (inclusionChains != null)
            {
                var chains            = inclusionChains.Select(relationships => new ResourceFieldChainExpression(relationships.ToArray())).ToList();
                var includeExpression = IncludeChainConverter.FromRelationshipChains(chains);
                expressionsInScope.Add(new ExpressionInScope(null, includeExpression));
            }

            var mock = new Mock <IQueryConstraintProvider>();

            mock.Setup(x => x.GetConstraints()).Returns(expressionsInScope);

            IQueryConstraintProvider includeConstraintProvider = mock.Object;

            return(includeConstraintProvider.AsEnumerable());
        }
        private IEnumerable <TResource> GetWhereAndInclude <TResource, TId>(IEnumerable <TId> ids, RelationshipAttribute[] relationshipsToNextLayer) where TResource : class, IIdentifiable <TId>
        {
            var resourceContext = _resourceContextProvider.GetResourceContext <TResource>();
            var idAttribute     = resourceContext.Attributes.Single(attr => attr.Property.Name == nameof(Identifiable.Id));

            var queryLayer = new QueryLayer(resourceContext)
            {
                Filter = new EqualsAnyOfExpression(new ResourceFieldChainExpression(idAttribute),
                                                   ids.Select(id => new LiteralConstantExpression(id.ToString())).ToList())
            };

            var chains = relationshipsToNextLayer.Select(relationship => new ResourceFieldChainExpression(relationship)).ToList();

            if (chains.Any())
            {
                queryLayer.Include = IncludeChainConverter.FromRelationshipChains(chains);
            }

            var repository = GetRepository <TResource, TId>();

            return(repository.GetAsync(queryLayer).Result);
        }