Esempio n. 1
0
        public Task <IEnumerable <T> > QueryAsync <T>(string queryName, IEnumerable <QueryParameter> queryParameters, Dictionary <string, object> stepBagItems, IGraphRequestContext graphRequestContext) where T : class
        {
            Dictionary <string, object> collection = GetCollection <T>();
            var parameters = queryParameters.ToList();

            if (parameters.Count > 0)
            {
                return(Task.FromResult(_inMemoryComparerProvider.Query(queryParameters, collection.Values.ToList()).Select(x => (T)x)));
            }

            return(Task.FromResult(collection.Values.Select(x => (T)x).AsEnumerable()));
        }
        private QueryStep QuerySource(Action <QueryExecutionContext> mapper)
        {
            var queryStep = _source.NewQueryStep();

            var type         = typeof(TSource);
            var typeAccessor = TypeAccessor.Create(type);
            var members      = typeAccessor.GetMembers().ToList();

            // TODO: Support multiple keys
            var idMember = members.Single(x => x.GetAttribute(typeof(KeyAttribute), false) != null);

            queryStep.QueryParameters.Add(new QueryParameter
            {
                MemberModel = new ModelMember(type, typeAccessor, idMember, false),
                Rule        = new ContextValueSetRule {
                    ForceCreateContextValueIfNull = true
                }
            });

            queryStep.InMemoryFilterQueryParameters = GetInMemoryFilterQueryParameters();

            queryStep.StepMapper = (ctx) =>
            {
                var connectionEdges = ctx.Context.GetQueryResults <ConnectionEdge>();

                if (mapper != null)
                {
                    var items = connectionEdges.Select(x => JsonConvert.DeserializeObject(
                                                           x.MetaValue, Type.GetType(x.MetaType))).ToList();

                    if (ctx.QueryStep.InMemoryFilterQueryParameters != null &&
                        ctx.QueryStep.InMemoryFilterQueryParameters.Count > 0)
                    {
                        items = _inMemoryComparerProvider.Query(ctx.QueryStep.InMemoryFilterQueryParameters, items);
                        var metaValues = items.Select(JsonConvert.SerializeObject).ToList();
                        connectionEdges = connectionEdges.Where(c => metaValues.Contains(c.MetaValue)).ToList();
                    }

                    // Override result.
                    ctx.Context.SetQueryResult(items);

                    mapper(ctx.Context);
                }
                return(connectionEdges.Select(x => (object)x.SourceId).ToList());
            };
            return(queryStep);
        }