コード例 #1
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));
            }
        }
コード例 #2
0
        public void Embedded_Resource_Collections_To_JObject(object model, Dictionary <string, IEnumerable <object> > embeddedCollections, string expectedHal, string expectedPlain)
        {
            var serializer = new JsonSerializer();

            var response = new HALResponse(model);

            response.AddEmbeddedCollections(embeddedCollections);

            var jObject = response.ToJObject(serializer);

            string actual = jObject.ToString(Formatting.None);

            Assert.Equal(expectedHal, actual);
        }
コード例 #3
0
        public HALResponse Convert(object model)
        {
            if(!this.CanConvert(model?.GetType())) {
                throw new InvalidOperationException();
            }

            var resolver = new HALAttributeResolver();

            var halConfig = resolver.GetConfig(model);

            var response = new HALResponse(model, halConfig);
            response.AddLinks(resolver.GetLinks(model));
            response.AddEmbeddedCollections(resolver.GetEmbeddedCollections(model, halConfig));

            return response;
        }
コード例 #4
0
        public HALResponse Convert(object model)
        {
            if (!this.CanConvert(model?.GetType()))
            {
                throw new InvalidOperationException();
            }

            var resolver = new HALAttributeResolver();

            var halConfig = resolver.GetConfig(model);

            var response = new HALResponse(model, halConfig);

            response.AddLinks(resolver.GetLinks(model));
            response.AddEmbeddedCollections(resolver.GetEmbeddedCollections(model, halConfig));

            return(response);
        }
        private static HALResponse ConvertInstance(object model, HttpContext context, HalcyonConventionOptions options)
        {
            //If this is called for a collection it will scan all the links for each item, but
            //each one needs to be customized to work anyway.

            //If the options provide a model, use that, otherwise get it from the resolver.
            IHALModelConfig halConfig;

            if (options.BaseUrl != null)
            {
                var pathBaseValue = "";
                var pathBase      = context.Request.PathBase;
                if (pathBase.HasValue)
                {
                    //If we have a value, use that as the pathBaseValue, otherwise stick with the empty string.
                    pathBaseValue = pathBase.Value;
                }

                var currentUri = new Uri(context.Request.GetDisplayUrl());
                var host       = $"{currentUri.Scheme}://{currentUri.Authority}{pathBaseValue}";

                halConfig = new HALModelConfig()
                {
                    LinkBase = options.BaseUrl.Replace(HalcyonConventionOptions.HostVariable, host),
                    ForceHAL = false
                };
            }
            else
            {
                halConfig = CustomHALAttributeResolver.GetConfig(model);
            }

            var response = new HALResponse(model, halConfig);

            response.AddLinks(CustomHALAttributeResolver.GetUserLinks(model, context, options.HalDocEndpointInfo));
            var embeddedCollections = CustomHALAttributeResolver.GetEmbeddedCollectionValues(model)
                                      .Select(i => new KeyValuePair <String, IEnumerable <HALResponse> >(i.Key, GetEmbeddedResponses(i.Value, context, options)));

            response.AddEmbeddedCollections(embeddedCollections);

            return(response);
        }
コード例 #6
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);
            }
        }