Exemplo n.º 1
0
        /// <summary>
        /// Processes the <b>Odata</b> <i>query</i> <see cref="HttpResponseMessage"/>.
        /// </summary>
        /// <typeparam name="T">The entity <see cref="Type"/>.</typeparam>
        /// <param name="response">The <see cref="HttpResponseMessage"/>.</param>
        /// <param name="queryArgs">The <see cref="ODataArgs"/>.</param>
        /// <param name="queryAggregator">The <see cref="ODataQueryAggregator"/>.</param>
        /// <param name="coll">The collection to add to.</param>
        /// <returns>The <see cref="Task"/>.</returns>
        internal async Task ProcessQueryResponse <T>(HttpResponseMessage response, ODataArgs queryArgs, ODataQueryAggregator queryAggregator, ICollection <T> coll)
        {
            EnsureSuccessStatusCodeForResponse(response);

            var          type   = typeof(T);
            IODataMapper mapper = null;
            PropertyInfo pi     = null;

            switch (queryAggregator.SelectClause.Selector.NodeType)
            {
            case System.Linq.Expressions.ExpressionType.MemberAccess:
                mapper = queryArgs.Mapper;
                var me = (System.Linq.Expressions.MemberExpression)queryAggregator.SelectClause.Selector;
                pi = (PropertyInfo)me.Member;
                break;

            default:
                mapper = queryArgs.Mapper.SrceType == type ? queryArgs.Mapper : ODataAutoMapper.GetMapper(type);
                break;
            }

            int?count = null;

            using (var s = await response.Content.ReadAsStreamAsync().ConfigureAwait(false))
                using (var sr = new StreamReader(s))
                    using (var jr = new JsonTextReader(sr))
                    {
                        var json = await JObject.LoadAsync(jr).ConfigureAwait(false);

                        count = json.Value <int?>("@odata.count");

                        foreach (var jt in json.GetValue("value"))
                        {
                            var obj = mapper.MapFromOData(jt, Mapper.OperationTypes.Get);
                            if (pi == null)
                            {
                                coll.Add((T)obj);
                            }
                            else
                            {
                                coll.Add((T)pi.GetValue(obj));
                            }
                        }
                    }

            if (count.HasValue && queryArgs.Paging != null && queryArgs.Paging.IsGetCount)
            {
                queryArgs.Paging.TotalCount = count.Value;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Sets up (creates) and auto-maps the <paramref name="args"/>.
        /// </summary>
        /// <typeparam name="T">The <see cref="Type"/>.</typeparam>
        /// <param name="args">The <see cref="ODataArgs"/>.</param>
        /// <returns>An updated or created <see cref="ODataArgs"/>.</returns>
        internal ODataArgs SetUpArgs <T>(ODataArgs args)
        {
            var oa = args ?? new ODataArgs();

            if (oa.Mapper == null)
            {
                oa.Mapper = ODataAutoMapper.GetMapper(typeof(T));
            }
            else if (oa.Mapper.SrceType != typeof(T))
            {
                throw new ArgumentException($"The Entity Type is '{typeof(T).Name}' and the Mapper Type is '{oa.Mapper.SrceType.Name}'; these must be the same.");
            }

            args = oa;
            return(args);
        }