Exemple #1
0
        public async Task <TModel> Get <TModel>(Guid id)
        {
            ThrowIfDisposed();

            var model = Cache.Retrieve <TModel>(id);

            if (model != null)
            {
                return(model);
            }

            var resourceType = ModelRegistry.GetResourceType <TModel>();
            var include      = ModelRegistry.GetInclude <TModel>();
            var request      = HttpRequestBuilder.GetResource(id, resourceType, include);
            var response     = await HttpClient.SendAsync(request).ConfigureAwait(false);

            HttpResponseListener.GetResource(response.StatusCode, id, resourceType);
            if (response.StatusCode == HttpStatusCode.NotFound)
            {
                return(default(TModel)); // null
            }
            response.CheckStatusCode();

            var root = await response.GetContentModel <ResourceRootSingle>(JsonSettings);

            model = CreateResourceModel <TModel>(root.Data);
            Cache.Update(id, model);
            if (root.Included != null)
            {
                await Task.WhenAll(root.Included.Select(x => Task.Run(() =>
                {
                    var includedModel = CreateResourceModel(x);
                    Cache.Update(x.Id, includedModel);
                })));
            }
            return(model);
        }