public HALResponse(object model, IHALModelConfig config = null)
     : this(config) {
     if (!(model is JObject) && (model is IEnumerable))
     {
         throw new ArgumentException("The HAL model should not be Enumerable. You should use an embedded collection instead", nameof(model));
     }
     this.model = model;
 }
Beispiel #2
0
 public HALResponse(object model, IHALModelConfig config = null)
     : this(config)
 {
     if(!(model is JObject) && (model is IEnumerable)) {
         throw new ArgumentException("The HAL model should not be Enumerable. You should use an embedded collection instead", nameof(model));
     }
     this.model = model;
 }
        public void AddEmbeddedResources(HALResponse response, object modelValue, IHALModelConfig config)
        {
            response.AddLinks(this.GetLinks(modelValue));
            var embeddedCollections = this.GetEmbeddedCollections(modelValue, config);

            foreach (var embedded in embeddedCollections)
            {
                if (embedded.IsCollection)
                {
                    response.AddEmbeddedCollection(embedded.ResourceName, embedded.HALResponses);
                }
                else
                {
                    response.AddEmbeddedResource(embedded.ResourceName, embedded.HALResponses.Single());
                }
            }
        }
        private IEnumerable <HALEmbeddedItem> GetEmbeddedCollections(object model, IHALModelConfig config)
        {
            var type = model.GetType();
            var embeddedModelProperties = type.GetTypeInfo().GetProperties().Where(x => x.IsDefined(typeof(HalEmbeddedAttribute)));

            foreach (var propertyInfo in embeddedModelProperties)
            {
                var embeddAttribute = propertyInfo.GetCustomAttribute(typeof(HalEmbeddedAttribute)) as HalEmbeddedAttribute;
                if (embeddAttribute == null)
                {
                    continue;
                }

                var modelValue    = propertyInfo.GetValue(model);
                var embeddedItems = modelValue as IEnumerable <object>;

                IEnumerable <HALResponse> halResponses = null;
                if (embeddedItems != null)
                {
                    halResponses = embeddedItems.Select(embeddedModel =>
                    {
                        var response = new HALResponse(embeddedModel, config);
                        AddEmbeddedResources(response, embeddedModel, config);
                        return(response);
                    });
                }
                else if (modelValue != null)
                {
                    var response = new HALResponse(modelValue, config);
                    AddEmbeddedResources(response, modelValue, config);
                    halResponses = new[] { response };
                }
                else
                {
                    continue;
                }

                yield return(new HALEmbeddedItem(embeddAttribute.CollectionName, halResponses, embeddedItems != null));
            }
        }
        public IEnumerable<KeyValuePair<string, IEnumerable<HALResponse>>> GetEmbeddedCollections(object model, IHALModelConfig config)
        {
            var type = model.GetType();
            var embeddedModelProperties = type.GetTypeInfo().GetProperties().Where(x => x.IsDefined(typeof(HalEmbeddedAttribute)));

            foreach(var propertyInfo in embeddedModelProperties) {
                var embeddAttribute = propertyInfo.GetCustomAttribute(typeof(HalEmbeddedAttribute)) as HalEmbeddedAttribute;
                if(embeddAttribute == null) continue;

                var modelValue = propertyInfo.GetValue(model);

                var embeddedItems = modelValue as IEnumerable<object> ?? new List<object> { modelValue };

                var halResponses = embeddedItems.Select(embeddedModel => {
                    var response = new HALResponse(embeddedModel, config);
                    response.AddLinks(this.GetLinks(embeddedModel));
                    response.AddEmbeddedCollections(this.GetEmbeddedCollections(embeddedModel, config));

                    return response;
                });

                yield return new KeyValuePair<string, IEnumerable<HALResponse>>(embeddAttribute.CollectionName, halResponses);
            }
        }
 public HALResponse(IHALModelConfig config)
 {
     this.config = config ?? new HALModelConfig();
 }
Beispiel #7
0
        public IEnumerable <KeyValuePair <string, IEnumerable <HALResponse> > > GetEmbeddedCollections(object model, IHALModelConfig config)
        {
            var type = model.GetType();
            var embeddedModelProperties = type.GetTypeInfo().GetProperties().Where(x => x.IsDefined(typeof(HalEmbeddedAttribute)));

            foreach (var propertyInfo in embeddedModelProperties)
            {
                var embeddAttribute = propertyInfo.GetCustomAttribute(typeof(HalEmbeddedAttribute)) as HalEmbeddedAttribute;
                if (embeddAttribute == null)
                {
                    continue;
                }

                var modelValue = propertyInfo.GetValue(model);

                var embeddedItems = modelValue as IEnumerable <object> ?? new List <object> {
                    modelValue
                };

                var halResponses = embeddedItems.Select(embeddedModel => {
                    var response = new HALResponse(embeddedModel, config);
                    response.AddLinks(this.GetLinks(embeddedModel));
                    response.AddEmbeddedCollections(this.GetEmbeddedCollections(embeddedModel, config));

                    return(response);
                });

                yield return(new KeyValuePair <string, IEnumerable <HALResponse> >(embeddAttribute.CollectionName, halResponses));
            }
        }
Beispiel #8
0
 public HALResponse(IHALModelConfig config)
 {
     this.config = config ?? new HALModelConfig();
 }