/// <summary>
        /// Attempts to get the entity from the cache.
        /// </summary>
        /// <param name="cache">The cache to perform the operation on.</param>
        /// <param name="key">The JSON API entity key to return the value for.</param>
        /// <param name="value">The value that was associated with the given key.</param>
        /// <returns>true if the an entity was found for the key, false if not.</returns>
        internal static bool TryGetValue(this IJsonApiEntityCache cache, JsonApiEntityKey key, out object value)
        {
            if (cache == null)
            {
                throw new ArgumentNullException(nameof(cache));
            }

            return(cache.TryGetValue(key.Type, key.Id, out value));
        }
Esempio n. 2
0
        /// <summary>
        /// Read the content as a JSONAPI response object.
        /// </summary>
        /// <param name="httpContent">The HTTP content to read the JSONAPI response from.</param>
        /// <param name="contractResolver">The resource contract resolver use to resolve types during deserialization.</param>
        /// <param name="cache">The entity cache to use for resolving existing instances in the object graph.</param>
        /// <returns>The JSONAPI response element that was read from the stream in the HTTP content.</returns>
        public static Task <TEntity> ReadAsJsonApiAsync <TEntity>(
            this HttpContent httpContent,
            IContractResolver contractResolver,
            IJsonApiEntityCache cache)
        {
            var serializer = new JsonApiSerializer(contractResolver, new DasherizedFieldNamingStrategy());

            return(httpContent.ReadAsJsonApiAsync <TEntity>(serializer, cache));
        }
Esempio n. 3
0
        /// <summary>
        /// Read the content as a JSONAPI response object.
        /// </summary>
        /// <param name="httpContent">The HTTP content to read the JSONAPI response from.</param>
        /// <param name="serializerOptions">The options to use for the serializer.</param>
        /// <param name="cache">The entity cache to use for resolving existing instances in the object graph.</param>
        /// <returns>The JSONAPI response element that was read from the stream in the HTTP content.</returns>
        public static Task <TEntity> ReadAsJsonApiAsync <TEntity>(
            this HttpContent httpContent,
            JsonApiSerializerOptions serializerOptions,
            IJsonApiEntityCache cache)
        {
            var serializer = new JsonApiSerializer(serializerOptions);

            return(httpContent.ReadAsJsonApiAsync <TEntity>(serializer, cache));
        }
        /// <summary>
        /// Attempts to adds an entity to the <see cref="T:Hypermedia.JsonApi.IJsonApiEntityCache" />.
        /// </summary>
        /// <param name="cache">The cache to add the entity to.</param>
        /// <param name="key">The item that defines the key for the entity to add.</param>
        /// <param name="value">The object to use as the value of the element to add.</param>
        /// <returns>true if the entity could be added, false if it could not be added.</returns>
        public static bool TryAdd(this IJsonApiEntityCache cache, JsonObject key, object value)
        {
            if (cache == null)
            {
                throw new ArgumentNullException(nameof(cache));
            }

            string type;
            string id;

            if (TryCreateKey(key, out type, out id) == false)
            {
                return(false);
            }

            return(cache.TryAdd(type, id, value));
        }
Esempio n. 5
0
 /// <summary>
 /// Gets a list of entities.
 /// </summary>
 /// <typeparam name="TEntity">The element type.</typeparam>
 /// <param name="contractResolver">The contract resolver.</param>
 /// <param name="cache">The entity cache to use for resolving existing instances in the object graph.</param>
 /// <returns>The list of JSON API entities.</returns>
 public IEnumerable <TEntity> GetMany <TEntity>(IContractResolver contractResolver, IJsonApiEntityCache cache)
 {
     return(GetMany <TEntity>(new JsonApiSerializer(contractResolver), cache));
 }
Esempio n. 6
0
 /// <summary>
 /// Gets a single entity.
 /// </summary>
 /// <typeparam name="TEntity">The element type.</typeparam>
 /// <param name="serializer">The JSON API serializer.</param>
 /// <param name="cache">The entity cache to use for resolving existing instances in the object graph.</param>
 /// <returns>The list of JSON API entities.</returns>
 public TEntity Get <TEntity>(JsonApiSerializer serializer, IJsonApiEntityCache cache)
 {
     return((TEntity)serializer.DeserializeEntity(_jsonObject, cache));
 }
Esempio n. 7
0
 /// <summary>
 /// Gets a single entity.
 /// </summary>
 /// <typeparam name="TEntity">The element type.</typeparam>
 /// <param name="contractResolver">The contract resolver.</param>
 /// <param name="cache">The entity cache to use for resolving existing instances in the object graph.</param>
 /// <returns>The list of JSON API entities.</returns>
 public TEntity Get <TEntity>(IContractResolver contractResolver, IJsonApiEntityCache cache)
 {
     return(Get <TEntity>(new JsonApiSerializer(contractResolver), cache));
 }
Esempio n. 8
0
 /// <summary>
 /// Gets a list of entities.
 /// </summary>
 /// <typeparam name="TEntity">The element type.</typeparam>
 /// <param name="serializer">The JSON API serializer.</param>
 /// <param name="cache">The entity cache to use for resolving existing instances in the object graph.</param>
 /// <returns>The list of JSON API entities.</returns>
 public IEnumerable <TEntity> GetMany <TEntity>(JsonApiSerializer serializer, IJsonApiEntityCache cache)
 {
     return(serializer.DeserializeMany(_jsonObject, cache).OfType <TEntity>().ToList());
 }
Esempio n. 9
0
 /// <summary>
 /// Gets a single entity.
 /// </summary>
 /// <typeparam name="TEntity">The element type.</typeparam>
 /// <param name="serializerOptions">The serializer options.</param>
 /// <param name="cache">The entity cache to use for resolving existing instances in the object graph.</param>
 /// <returns>The list of JSON API entities.</returns>
 public TEntity Get <TEntity>(JsonApiSerializerOptions serializerOptions, IJsonApiEntityCache cache)
 {
     return(Get <TEntity>(new JsonApiSerializer(serializerOptions), cache));
 }
Esempio n. 10
0
 /// <summary>
 /// Gets a list of entities.
 /// </summary>
 /// <typeparam name="TEntity">The element type.</typeparam>
 /// <param name="serializerOptions">The serializer options.</param>
 /// <param name="cache">The entity cache to use for resolving existing instances in the object graph.</param>
 /// <returns>The list of JSON API entities.</returns>
 public IEnumerable <TEntity> GetMany <TEntity>(JsonApiSerializerOptions serializerOptions, IJsonApiEntityCache cache)
 {
     return(GetMany <TEntity>(new JsonApiSerializer(serializerOptions), cache));
 }
Esempio n. 11
0
        /// <summary>
        /// Read the content as a JSONAPI response object.
        /// </summary>
        /// <param name="httpContent">The HTTP content to read the JSONAPI response from.</param>
        /// <param name="serializer">The JSON API serializer instance to use.</param>
        /// <param name="cache">The entity cache to use for resolving existing instances in the object graph.</param>
        /// <returns>The JSONAPI response element that was read from the stream in the HTTP content.</returns>
        public static async Task <TEntity> ReadAsJsonApiAsync <TEntity>(this HttpContent httpContent, JsonApiSerializer serializer, IJsonApiEntityCache cache)
        {
            if (httpContent == null)
            {
                throw new ArgumentNullException(nameof(httpContent));
            }

            var response = await httpContent.ReadAsJsonApiAsync();

            return(response.Get <TEntity>(serializer, cache));
        }
Esempio n. 12
0
 /// <summary>
 /// Gets a single entity.
 /// </summary>
 /// <typeparam name="TEntity">The element type.</typeparam>
 /// <param name="contractResolver">The contract resolver.</param>
 /// <param name="cache">The entity cache to use for resolving existing instances in the object graph.</param>
 /// <returns>The list of JSON API entities.</returns>
 public TEntity Get <TEntity>(IContractResolver contractResolver, IJsonApiEntityCache cache)
 {
     return(Get <TEntity>(new JsonApiSerializerOptions {
         ContractResolver = contractResolver, FieldNamingStrategy = DasherizedFieldNamingStrategy.Instance
     }, cache));
 }
Esempio n. 13
0
 /// <summary>
 /// Gets a list of entities.
 /// </summary>
 /// <typeparam name="TEntity">The element type.</typeparam>
 /// <param name="contractResolver">The contract resolver.</param>
 /// <param name="cache">The entity cache to use for resolving existing instances in the object graph.</param>
 /// <returns>The list of JSON API entities.</returns>
 public IEnumerable <TEntity> GetMany <TEntity>(IContractResolver contractResolver, IJsonApiEntityCache cache)
 {
     return(GetMany <TEntity>(
                new JsonApiSerializer(
                    new JsonApiSerializerOptions {
         ContractResolver = contractResolver, FieldNamingStrategy = DasherizedFieldNamingStrategy.Instance
     }),
                cache));
 }
Esempio n. 14
0
        /// <summary>
        /// Read the content as a JSONAPI response object.
        /// </summary>
        /// <param name="httpContent">The HTTP content to read the JSONAPI response from.</param>
        /// <param name="contractResolver">The resource contract resolver use to resolve types during deserialization.</param>
        /// <param name="cache">The entity cache to use for resolving existing instances in the object graph.</param>
        /// <returns>The JSONAPI response element that was read from the stream in the HTTP content.</returns>
        public static Task <List <TEntity> > ReadAsJsonApiManyAsync <TEntity>(this HttpContent httpContent, IContractResolver contractResolver, IJsonApiEntityCache cache)
        {
            var serializer = new JsonApiSerializer(
                new JsonApiSerializerOptions
            {
                ContractResolver    = contractResolver,
                FieldNamingStrategy = DasherizedFieldNamingStrategy.Instance
            });

            return(httpContent.ReadAsJsonApiManyAsync <TEntity>(serializer, cache));
        }