Moving to the latest pattern of Specifying formatters using options
This commit is contained in:
parent
307c191c17
commit
2fe2efa94a
|
|
@ -3,20 +3,15 @@
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using Microsoft.AspNet.Mvc.OptionDescriptors;
|
||||||
using Microsoft.AspNet.Mvc.ModelBinding;
|
|
||||||
using Microsoft.Framework.DependencyInjection;
|
using Microsoft.Framework.DependencyInjection;
|
||||||
using Microsoft.Framework.OptionsModel;
|
using Microsoft.Framework.OptionsModel;
|
||||||
|
|
||||||
namespace Microsoft.AspNet.Mvc
|
namespace Microsoft.AspNet.Mvc
|
||||||
{
|
{
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public class DefaultOutputFormattersProvider : IOutputFormattersProvider
|
public class DefaultOutputFormattersProvider : OptionDescriptorBasedProvider<IOutputFormatter>, IOutputFormattersProvider
|
||||||
{
|
{
|
||||||
private readonly List<OutputFormatterDescriptor> _descriptors;
|
|
||||||
private readonly ITypeActivator _typeActivator;
|
|
||||||
private readonly IServiceProvider _serviceProvider;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the DefaultOutputFormattersProvider class.
|
/// Initializes a new instance of the DefaultOutputFormattersProvider class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
@ -24,34 +19,19 @@ namespace Microsoft.AspNet.Mvc
|
||||||
/// <param name="typeActivator">An <see cref="ITypeActivator"/> instance used to instantiate types.</param>
|
/// <param name="typeActivator">An <see cref="ITypeActivator"/> instance used to instantiate types.</param>
|
||||||
/// <param name="serviceProvider">A <see cref="IServiceProvider"/> instance that retrieves services from the
|
/// <param name="serviceProvider">A <see cref="IServiceProvider"/> instance that retrieves services from the
|
||||||
/// service collection.</param>
|
/// service collection.</param>
|
||||||
public DefaultOutputFormattersProvider(IOptionsAccessor<MvcOptions> options,
|
public DefaultOutputFormattersProvider(IOptionsAccessor<MvcOptions> optionsAccessor,
|
||||||
ITypeActivator typeActivator,
|
ITypeActivator typeActivator,
|
||||||
IServiceProvider serviceProvider)
|
IServiceProvider serviceProvider)
|
||||||
|
: base(optionsAccessor.Options.OutputFormatters, typeActivator, serviceProvider)
|
||||||
{
|
{
|
||||||
_descriptors = options.Options.OutputFormatters;
|
|
||||||
_typeActivator = typeActivator;
|
|
||||||
_serviceProvider = serviceProvider;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public IReadOnlyList<IOutputFormatter> OutputFormatters
|
public IReadOnlyList<IOutputFormatter> OutputFormatters
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
var outputFormatters = new List<IOutputFormatter>();
|
return Options;
|
||||||
foreach (var descriptor in _descriptors)
|
|
||||||
{
|
|
||||||
var formatter = descriptor.OutputFormatter;
|
|
||||||
if (formatter == null)
|
|
||||||
{
|
|
||||||
formatter = (IOutputFormatter)_typeActivator.CreateInstance(_serviceProvider,
|
|
||||||
descriptor.OutputFormatterType);
|
|
||||||
}
|
|
||||||
|
|
||||||
outputFormatters.Add(formatter);
|
|
||||||
}
|
|
||||||
|
|
||||||
return outputFormatters;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,14 +6,14 @@ using System;
|
||||||
namespace Microsoft.AspNet.Mvc.OptionDescriptors
|
namespace Microsoft.AspNet.Mvc.OptionDescriptors
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Encapsulates information that describes an <see cref="OutputFormatter"/>.
|
/// Encapsulates information that describes an <see cref="IOutputFormatter"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class OutputFormatterDescriptor : OptionDescriptor<OutputFormatter>
|
public class OutputFormatterDescriptor : OptionDescriptor<IOutputFormatter>
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a new instance of <see cref="OutputFormatterDescriptor"/>.
|
/// Creates a new instance of <see cref="OutputFormatterDescriptor"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="type">A <see cref="OutputFormatter/> type that the descriptor represents.
|
/// <param name="type">A <see cref="IOutputFormatter/> type that the descriptor represents.
|
||||||
/// </param>
|
/// </param>
|
||||||
public OutputFormatterDescriptor([NotNull] Type type)
|
public OutputFormatterDescriptor([NotNull] Type type)
|
||||||
: base(type)
|
: base(type)
|
||||||
|
|
@ -23,9 +23,9 @@ namespace Microsoft.AspNet.Mvc.OptionDescriptors
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a new instance of <see cref="OutputFormatterDescriptor"/>.
|
/// Creates a new instance of <see cref="OutputFormatterDescriptor"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="outputFormatter">An instance of <see cref="OutputFormatter"/>
|
/// <param name="outputFormatter">An instance of <see cref="IOutputFormatter"/>
|
||||||
/// that the descriptor represents.</param>
|
/// that the descriptor represents.</param>
|
||||||
public OutputFormatterDescriptor([NotNull] OutputFormatter outputFormatter)
|
public OutputFormatterDescriptor([NotNull] IOutputFormatter outputFormatter)
|
||||||
: base(outputFormatter)
|
: base(outputFormatter)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -53,7 +53,7 @@ namespace Microsoft.AspNet.Mvc
|
||||||
/// <param name="outputFormatter">An <see cref="IOutputFormatter"/> instance.</param>
|
/// <param name="outputFormatter">An <see cref="IOutputFormatter"/> instance.</param>
|
||||||
/// <returns>OutputFormatterDescriptor representing the added instance.</returns>
|
/// <returns>OutputFormatterDescriptor representing the added instance.</returns>
|
||||||
public static OutputFormatterDescriptor Add([NotNull] this IList<OutputFormatterDescriptor> descriptors,
|
public static OutputFormatterDescriptor Add([NotNull] this IList<OutputFormatterDescriptor> descriptors,
|
||||||
[NotNull] IOutputFormatter outputFormatter)
|
[NotNull] IOutputFormatter outputFormatter)
|
||||||
{
|
{
|
||||||
var descriptor = new OutputFormatterDescriptor(outputFormatter);
|
var descriptor = new OutputFormatterDescriptor(outputFormatter);
|
||||||
descriptors.Add(descriptor);
|
descriptors.Add(descriptor);
|
||||||
|
|
@ -68,7 +68,7 @@ namespace Microsoft.AspNet.Mvc
|
||||||
/// <returns>OutputFormatterDescriptor representing the added instance.</returns>
|
/// <returns>OutputFormatterDescriptor representing the added instance.</returns>
|
||||||
public static OutputFormatterDescriptor Insert([NotNull] this IList<OutputFormatterDescriptor> descriptors,
|
public static OutputFormatterDescriptor Insert([NotNull] this IList<OutputFormatterDescriptor> descriptors,
|
||||||
int index,
|
int index,
|
||||||
[NotNull] IOutputFormatter outputFormatter)
|
[NotNull] IOutputFormatter outputFormatter)
|
||||||
{
|
{
|
||||||
if (index < 0 || index > descriptors.Count)
|
if (index < 0 || index > descriptors.Count)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -1066,6 +1066,23 @@ namespace Microsoft.AspNet.Mvc.Core
|
||||||
return string.Format(CultureInfo.CurrentCulture, GetString("OutputFormatterNoEncoding"), p0);
|
return string.Format(CultureInfo.CurrentCulture, GetString("OutputFormatterNoEncoding"), p0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// No supported media type registered for output formatter '{0}'. There must be at least one supported media type registered in order for the output formatter to write content.
|
||||||
|
/// </summary>
|
||||||
|
internal static string OutputFormatterNoMediaType
|
||||||
|
{
|
||||||
|
get { return GetString("OutputFormatterNoMediaType"); }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// No supported media type registered for output formatter '{0}'. There must be at least one supported media type registered in order for the output formatter to write content.
|
||||||
|
/// </summary>
|
||||||
|
internal static string FormatOutputFormatterNoMediaType(object p0)
|
||||||
|
{
|
||||||
|
return string.Format(CultureInfo.CurrentCulture, GetString("OutputFormatterNoMediaType"), p0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
/// The following errors occurred with attribute routing information:{0}{0}{1}
|
/// The following errors occurred with attribute routing information:{0}{0}{1}
|
||||||
/// </summary>
|
/// </summary>
|
||||||
internal static string AttributeRoute_AggregateErrorMessage
|
internal static string AttributeRoute_AggregateErrorMessage
|
||||||
|
|
|
||||||
|
|
@ -322,8 +322,7 @@
|
||||||
<value>The following errors occurred with attribute routing information:{0}{0}{1}</value>
|
<value>The following errors occurred with attribute routing information:{0}{0}{1}</value>
|
||||||
<comment>{0} is the newline. {1} is the formatted list of errors using AttributeRoute_IndividualErrorMessage</comment>
|
<comment>{0} is the newline. {1} is the formatted list of errors using AttributeRoute_IndividualErrorMessage</comment>
|
||||||
</data>
|
</data>
|
||||||
|
<data name="AttributeRoute_CannotContainParameter" xml:space="preserve">
|
||||||
<data>
|
|
||||||
<value>The attribute route '{0}' cannot contain a parameter named '{{{1}}}'. Use '[{1}]' in the route template to insert the value '{2}'.</value>
|
<value>The attribute route '{0}' cannot contain a parameter named '{{{1}}}'. Use '[{1}]' in the route template to insert the value '{2}'.</value>
|
||||||
</data>
|
</data>
|
||||||
<data name="AttributeRoute_IndividualErrorMessage" xml:space="preserve">
|
<data name="AttributeRoute_IndividualErrorMessage" xml:space="preserve">
|
||||||
|
|
|
||||||
|
|
@ -33,7 +33,7 @@ namespace Microsoft.AspNet.Mvc
|
||||||
options.ModelBinders.Add(new ComplexModelDtoModelBinder());
|
options.ModelBinders.Add(new ComplexModelDtoModelBinder());
|
||||||
|
|
||||||
// Set up default output formatters.
|
// Set up default output formatters.
|
||||||
options.OutputFormatters.Add(new JsonOutputFormatter(JsonOutputFormatter.CreateDefaultSettings(), true));
|
options.OutputFormatters.Add(new JsonOutputFormatter(JsonOutputFormatter.CreateDefaultSettings(), false));
|
||||||
|
|
||||||
// Set up ValueProviders
|
// Set up ValueProviders
|
||||||
options.ValueProviderFactories.Add(new RouteValueValueProviderFactory());
|
options.ValueProviderFactories.Add(new RouteValueValueProviderFactory());
|
||||||
|
|
|
||||||
|
|
@ -66,9 +66,11 @@ namespace Microsoft.AspNet.Mvc
|
||||||
yield return describe.Transient<IInputFormatter, XmlDataContractSerializerInputFormatter>();
|
yield return describe.Transient<IInputFormatter, XmlDataContractSerializerInputFormatter>();
|
||||||
yield return describe.Transient<IInputFormatterProvider, TempInputFormatterProvider>();
|
yield return describe.Transient<IInputFormatterProvider, TempInputFormatterProvider>();
|
||||||
|
|
||||||
|
yield return describe.Transient<IModelBinderProvider, DefaultModelBindersProvider>();
|
||||||
|
yield return describe.Scoped<ICompositeModelBinder, CompositeModelBinder>();
|
||||||
|
yield return describe.Transient<IValueProviderFactoryProvider, DefaultValueProviderFactoryProvider>();
|
||||||
|
yield return describe.Scoped<ICompositeValueProviderFactory, CompositeValueProviderFactory>();
|
||||||
yield return describe.Transient<IOutputFormattersProvider, DefaultOutputFormattersProvider>();
|
yield return describe.Transient<IOutputFormattersProvider, DefaultOutputFormattersProvider>();
|
||||||
yield return describe.Transient<IModelBindersProvider, DefaultModelBindersProvider>();
|
|
||||||
yield return describe.Transient<ICompositeModelBinder, CompositeModelBinder>();
|
|
||||||
|
|
||||||
yield return describe.Transient<INestedProvider<FilterProviderContext>, DefaultFilterProvider>();
|
yield return describe.Transient<INestedProvider<FilterProviderContext>, DefaultFilterProvider>();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -79,7 +79,7 @@ namespace Microsoft.AspNet.Mvc.Core.Test.ActionResults
|
||||||
result.Formatters = new List<IOutputFormatter>
|
result.Formatters = new List<IOutputFormatter>
|
||||||
{
|
{
|
||||||
new CannotWriteFormatter(),
|
new CannotWriteFormatter(),
|
||||||
new JsonOutputFormatter(JsonOutputFormatter.CreateDefaultSettings(), true),
|
new JsonOutputFormatter(JsonOutputFormatter.CreateDefaultSettings(), false),
|
||||||
};
|
};
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
|
|
@ -145,7 +145,7 @@ namespace Microsoft.AspNet.Mvc.Core.Test.ActionResults
|
||||||
result.Formatters = new List<IOutputFormatter>
|
result.Formatters = new List<IOutputFormatter>
|
||||||
{
|
{
|
||||||
new CannotWriteFormatter(),
|
new CannotWriteFormatter(),
|
||||||
new JsonOutputFormatter(JsonOutputFormatter.CreateDefaultSettings(), true),
|
new JsonOutputFormatter(JsonOutputFormatter.CreateDefaultSettings(), false),
|
||||||
};
|
};
|
||||||
// Act
|
// Act
|
||||||
await result.ExecuteResultAsync(actionContext);
|
await result.ExecuteResultAsync(actionContext);
|
||||||
|
|
@ -177,7 +177,7 @@ namespace Microsoft.AspNet.Mvc.Core.Test.ActionResults
|
||||||
result.Formatters = new List<IOutputFormatter>
|
result.Formatters = new List<IOutputFormatter>
|
||||||
{
|
{
|
||||||
mockFormatter.Object,
|
mockFormatter.Object,
|
||||||
new JsonOutputFormatter(JsonOutputFormatter.CreateDefaultSettings(), true),
|
new JsonOutputFormatter(JsonOutputFormatter.CreateDefaultSettings(), false),
|
||||||
new CannotWriteFormatter()
|
new CannotWriteFormatter()
|
||||||
};
|
};
|
||||||
// Act
|
// Act
|
||||||
|
|
@ -207,7 +207,7 @@ namespace Microsoft.AspNet.Mvc.Core.Test.ActionResults
|
||||||
result.Formatters = new List<IOutputFormatter>
|
result.Formatters = new List<IOutputFormatter>
|
||||||
{
|
{
|
||||||
new CannotWriteFormatter(),
|
new CannotWriteFormatter(),
|
||||||
new JsonOutputFormatter(JsonOutputFormatter.CreateDefaultSettings(), true),
|
new JsonOutputFormatter(JsonOutputFormatter.CreateDefaultSettings(), false),
|
||||||
};
|
};
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
|
|
@ -238,7 +238,7 @@ namespace Microsoft.AspNet.Mvc.Core.Test.ActionResults
|
||||||
result.Formatters = new List<IOutputFormatter>
|
result.Formatters = new List<IOutputFormatter>
|
||||||
{
|
{
|
||||||
new CannotWriteFormatter(),
|
new CannotWriteFormatter(),
|
||||||
new JsonOutputFormatter(JsonOutputFormatter.CreateDefaultSettings(), true),
|
new JsonOutputFormatter(JsonOutputFormatter.CreateDefaultSettings(), false),
|
||||||
};
|
};
|
||||||
// Act
|
// Act
|
||||||
await result.ExecuteResultAsync(actionContext);
|
await result.ExecuteResultAsync(actionContext);
|
||||||
|
|
@ -269,7 +269,7 @@ namespace Microsoft.AspNet.Mvc.Core.Test.ActionResults
|
||||||
result.Formatters = new List<IOutputFormatter>
|
result.Formatters = new List<IOutputFormatter>
|
||||||
{
|
{
|
||||||
new CannotWriteFormatter(),
|
new CannotWriteFormatter(),
|
||||||
new JsonOutputFormatter(JsonOutputFormatter.CreateDefaultSettings(), true),
|
new JsonOutputFormatter(JsonOutputFormatter.CreateDefaultSettings(), false),
|
||||||
};
|
};
|
||||||
// Act
|
// Act
|
||||||
await result.ExecuteResultAsync(actionContext);
|
await result.ExecuteResultAsync(actionContext);
|
||||||
|
|
@ -360,7 +360,7 @@ namespace Microsoft.AspNet.Mvc.Core.Test.ActionResults
|
||||||
Object = nonStringValue,
|
Object = nonStringValue,
|
||||||
DeclaredType = nonStringValue.GetType()
|
DeclaredType = nonStringValue.GetType()
|
||||||
};
|
};
|
||||||
var formatter = new JsonOutputFormatter(JsonOutputFormatter.CreateDefaultSettings(), true);
|
var formatter = new JsonOutputFormatter(JsonOutputFormatter.CreateDefaultSettings(), false);
|
||||||
formatter.WriteResponseContentHeaders(formatterContext);
|
formatter.WriteResponseContentHeaders(formatterContext);
|
||||||
await formatter.WriteAsync(formatterContext);
|
await formatter.WriteAsync(formatterContext);
|
||||||
|
|
||||||
|
|
@ -471,7 +471,7 @@ namespace Microsoft.AspNet.Mvc.Core.Test.ActionResults
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
return new List<IOutputFormatter>()
|
return new List<IOutputFormatter>()
|
||||||
{ new JsonOutputFormatter(JsonOutputFormatter.CreateDefaultSettings(), indent: true) };
|
{ new JsonOutputFormatter(JsonOutputFormatter.CreateDefaultSettings(), indent: false) };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@
|
||||||
#if NET45
|
#if NET45
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using Microsoft.AspNet.Mvc.OptionDescriptors;
|
||||||
using Moq;
|
using Moq;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
|
|
@ -63,10 +64,10 @@ namespace Microsoft.AspNet.Mvc.Core.Test
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.Equal(4, collection.Count);
|
Assert.Equal(4, collection.Count);
|
||||||
Assert.Equal(formatter1, collection[0].OutputFormatter);
|
Assert.Equal(formatter1, collection[0].Instance);
|
||||||
Assert.Equal(formatter2, collection[1].OutputFormatter);
|
Assert.Equal(formatter2, collection[1].Instance);
|
||||||
Assert.Equal(type2, collection[2].OutputFormatterType);
|
Assert.Equal(type2, collection[2].OptionType);
|
||||||
Assert.Equal(type1, collection[3].OutputFormatterType);
|
Assert.Equal(type1, collection[3].OptionType);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue