コード例 #1
0
        public ITypeBuilderStrategy Build(IActionConfiguration actionConfiguration, Type returnType)
        {
            string key = actionConfiguration.ActionMethodInfo.ToString() + returnType.ToString();

            return(StrategyCache.GetCachedOrAdd(key,
                                                () =>
            {
                var sirenMetadataTypes = actionConfiguration.MetadataProvider.GetRegisteredMetadataTypes().ToList();

                Func <CustomAttributeBuilder> jsonPropAttrNullValueHandling = () =>
                {
                    var type = typeof(JsonPropertyAttribute);

                    var ctor = type.GetConstructor(Type.EmptyTypes);

                    return new CustomAttributeBuilder(ctor,
                                                      new object[] {},
                                                      new[] { type.GetProperty("NullValueHandling") }, new object[] { NullValueHandling.Ignore });
                };

                // Do not use IActionConfiguration from parameter list as configuration might be altered on call
                Action <object, object, IActionConfiguration> classNameAssigner =
                    (proxyObject, originalObject, configuration) =>
                {
                    var classProp = proxyObject.GetType().GetProperty("class");
                    classProp.SetValue(proxyObject, configuration.Class);
                };

                var strategyBuilder = new StrategyBuilder()
                                      .For(returnType)
                                      .WithPayloadPropertyStrategy(returnType, "properties")
                                      .WithSimpleAttributedPropertyStrategy(typeof(string[]), "class", new [] { jsonPropAttrNullValueHandling })
                                      .WithSimpleAttributedPropertyStrategy(typeof(string), "href", new[] { jsonPropAttrNullValueHandling })
                                      .WithSimpleAttributedPropertyStrategy(typeof(string[]), "rel", new[] { jsonPropAttrNullValueHandling })
                                      .WithCustomActivationStrategy(classNameAssigner);

                sirenMetadataTypes.ForEach(metadataType => strategyBuilder.WithTypedMetadataProperty(metadataType, metadataType.Name.ToLower()));

                return strategyBuilder.Build();
            }));
        }
コード例 #2
0
        public ITypeBuilderStrategy Build(IActionConfiguration actionConfiguration, Type returnType)
        {
            string key = actionConfiguration.ActionMethodInfo.ToString() + returnType.ToString();

            return(StrategyCache.GetCachedOrAdd(key,
                                                () =>
            {
                var rels =
                    (Dictionary <string, IList <string> >)
                    actionConfiguration.MetadataProvider.GetMetadataByType(
                        typeof(Dictionary <string, IList <string> >));

                IList <string> topRels = rels.Values.ToList().ConvertAll(c => c.FirstOrDefault());

                var strategyBuilder = new StrategyBuilder()
                                      .For(returnType)
                                      .WithSimpleProperties()
                                      .WithPlainRouteInformation(topRels);

                return strategyBuilder.Build();
            }));
        }