Ejemplo n.º 1
0
        private async Task HandleQuery(HttpContext context, ICqrsOptions options)
        {
            var path = options.GetQueryPath(context.Request.Path.Value);

            if (!_queryTypes.Value.ContainsKey(path))
            {
                throw new QueryNotFoundException($"Query '{path}' not found");
            }

            var info            = _queryTypes.Value[path];
            var exposeAttribute = info.QueryType.GetCustomAttribute <ExposeAttribute>();
            var formatter       = _registry.GetFormatter(exposeAttribute.Formatter);

            var deserializeMethod = _deserializeMethods.GetOrAdd(info.QueryType, (t) =>
            {
                var mi = CreateDeserializeLambdaMethodInfo.MakeGenericMethod(t);
                return(mi.Invoke(null, null));
            });

            dynamic query = await deserializeMethod(formatter, context.Request);

            dynamic handler = _container.GetInstance(info.QueryHandlerType);

            context.Items[ContextKey] = new CqrsContext(context.Request.Path.Value, path, CqrsType.Command, info.QueryHandlerType);

            dynamic result;

            if (info.IsAsync)
            {
                result = await handler.HandleAsync(query, context.RequestAborted);
            }
            else
            {
                result = handler.Handle(query);
            }

            var json = result is string?result : await formatter.SerializeAsync(result, context.Request);

            context.Response.ContentType = formatter.ContentType;
            context.Response.StatusCode  = (int)HttpStatusCode.OK;

            await HttpResponseWritingExtensions.WriteAsync(context.Response, json, context.RequestAborted);
        }
Ejemplo n.º 2
0
        private async Task HandleQuery(HttpContext context, ICqrsOptions options)
        {
            var path = options.GetQueryPath(context.Request.Path.Value);

            if (!_queryTypes.ContainsKey(path))
            {
                throw new QueryNotFoundException($"Query '{path}' not found");
            }

            // GET operations get their data through the query string, while POST operations expect a JSON
            // object being put in the body.
            var queryData = context.Request.Method == HttpMethod.Get.Method
                ? SerializationHelpers.ConvertQueryStringToJson(context.Request.QueryString.Value)
                : await new StreamReader(context.Request.Body).ReadToEndAsync();

            var info  = _queryTypes[path];
            var query = DeserializeQuery(queryData, info.QueryType);

            dynamic handler = _container.GetInstance(info.QueryHandlerType);

            context.Items[ContextKey] = new CqrsContext(context.Request.Path.Value, path, CqrsType.Command, info.QueryHandlerType);

            dynamic result;

            if (info.IsAsync)
            {
                result = await handler.HandleAsync(query, context.RequestAborted);
            }
            else
            {
                result = handler.Handle(query);
            }

            var json = result is string?result : JsonConvert.SerializeObject(result, _jsonSerializerSettings);

            context.Response.ContentType = ContentType;
            context.Response.StatusCode  = (int)HttpStatusCode.OK;

            await HttpResponseWritingExtensions.WriteAsync(context.Response, json, context.RequestAborted);
        }
Ejemplo n.º 3
0
        public QueryHandlerMiddleware(CqrsFormatterRegistry registry, ICqrsOptions options)
        {
            _registry = registry;
            _options  = options;

            _queryTypes = new Lazy <Dictionary <string, QueryInfo> >(() => options.GetQueryTypes()
                                                                     .SelectMany(x => x.HttpMethods, (ci, method) => new { QueryInfo = ci, HttpMethod = method })
                                                                     .ToDictionary(
                                                                         keySelector: (x) => $"{x.HttpMethod} {options.GetQueryPath(x.QueryInfo)}",
                                                                         elementSelector: x => x.QueryInfo,
                                                                         comparer: StringComparer.OrdinalIgnoreCase));
        }