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.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.AspNet.Mvc.ModelBinding;
|
||||
using Microsoft.AspNet.Mvc.OptionDescriptors;
|
||||
using Microsoft.Framework.DependencyInjection;
|
||||
using Microsoft.Framework.OptionsModel;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc
|
||||
{
|
||||
/// <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>
|
||||
/// Initializes a new instance of the DefaultOutputFormattersProvider class.
|
||||
/// </summary>
|
||||
|
|
@ -24,34 +19,19 @@ namespace Microsoft.AspNet.Mvc
|
|||
/// <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
|
||||
/// service collection.</param>
|
||||
public DefaultOutputFormattersProvider(IOptionsAccessor<MvcOptions> options,
|
||||
public DefaultOutputFormattersProvider(IOptionsAccessor<MvcOptions> optionsAccessor,
|
||||
ITypeActivator typeActivator,
|
||||
IServiceProvider serviceProvider)
|
||||
: base(optionsAccessor.Options.OutputFormatters, typeActivator, serviceProvider)
|
||||
{
|
||||
_descriptors = options.Options.OutputFormatters;
|
||||
_typeActivator = typeActivator;
|
||||
_serviceProvider = serviceProvider;
|
||||
}
|
||||
|
||||
|
||||
/// <inheritdoc />
|
||||
public IReadOnlyList<IOutputFormatter> OutputFormatters
|
||||
{
|
||||
get
|
||||
{
|
||||
var outputFormatters = new List<IOutputFormatter>();
|
||||
foreach (var descriptor in _descriptors)
|
||||
{
|
||||
var formatter = descriptor.OutputFormatter;
|
||||
if (formatter == null)
|
||||
{
|
||||
formatter = (IOutputFormatter)_typeActivator.CreateInstance(_serviceProvider,
|
||||
descriptor.OutputFormatterType);
|
||||
}
|
||||
|
||||
outputFormatters.Add(formatter);
|
||||
}
|
||||
|
||||
return outputFormatters;
|
||||
return Options;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,14 +6,14 @@ using System;
|
|||
namespace Microsoft.AspNet.Mvc.OptionDescriptors
|
||||
{
|
||||
/// <summary>
|
||||
/// Encapsulates information that describes an <see cref="OutputFormatter"/>.
|
||||
/// Encapsulates information that describes an <see cref="IOutputFormatter"/>.
|
||||
/// </summary>
|
||||
public class OutputFormatterDescriptor : OptionDescriptor<OutputFormatter>
|
||||
public class OutputFormatterDescriptor : OptionDescriptor<IOutputFormatter>
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new instance of <see cref="OutputFormatterDescriptor"/>.
|
||||
/// </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>
|
||||
public OutputFormatterDescriptor([NotNull] Type type)
|
||||
: base(type)
|
||||
|
|
@ -23,9 +23,9 @@ namespace Microsoft.AspNet.Mvc.OptionDescriptors
|
|||
/// <summary>
|
||||
/// Creates a new instance of <see cref="OutputFormatterDescriptor"/>.
|
||||
/// </summary>
|
||||
/// <param name="outputFormatter">An instance of <see cref="OutputFormatter"/>
|
||||
/// <param name="outputFormatter">An instance of <see cref="IOutputFormatter"/>
|
||||
/// that the descriptor represents.</param>
|
||||
public OutputFormatterDescriptor([NotNull] OutputFormatter outputFormatter)
|
||||
public OutputFormatterDescriptor([NotNull] IOutputFormatter outputFormatter)
|
||||
: base(outputFormatter)
|
||||
{
|
||||
}
|
||||
|
|
|
|||
|
|
@ -53,7 +53,7 @@ namespace Microsoft.AspNet.Mvc
|
|||
/// <param name="outputFormatter">An <see cref="IOutputFormatter"/> instance.</param>
|
||||
/// <returns>OutputFormatterDescriptor representing the added instance.</returns>
|
||||
public static OutputFormatterDescriptor Add([NotNull] this IList<OutputFormatterDescriptor> descriptors,
|
||||
[NotNull] IOutputFormatter outputFormatter)
|
||||
[NotNull] IOutputFormatter outputFormatter)
|
||||
{
|
||||
var descriptor = new OutputFormatterDescriptor(outputFormatter);
|
||||
descriptors.Add(descriptor);
|
||||
|
|
@ -68,7 +68,7 @@ namespace Microsoft.AspNet.Mvc
|
|||
/// <returns>OutputFormatterDescriptor representing the added instance.</returns>
|
||||
public static OutputFormatterDescriptor Insert([NotNull] this IList<OutputFormatterDescriptor> descriptors,
|
||||
int index,
|
||||
[NotNull] IOutputFormatter outputFormatter)
|
||||
[NotNull] IOutputFormatter outputFormatter)
|
||||
{
|
||||
if (index < 0 || index > descriptors.Count)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1066,6 +1066,23 @@ namespace Microsoft.AspNet.Mvc.Core
|
|||
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}
|
||||
/// </summary>
|
||||
internal static string AttributeRoute_AggregateErrorMessage
|
||||
|
|
|
|||
|
|
@ -322,8 +322,7 @@
|
|||
<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>
|
||||
</data>
|
||||
|
||||
<data>
|
||||
<data name="AttributeRoute_CannotContainParameter" xml:space="preserve">
|
||||
<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 name="AttributeRoute_IndividualErrorMessage" xml:space="preserve">
|
||||
|
|
|
|||
|
|
@ -33,7 +33,7 @@ namespace Microsoft.AspNet.Mvc
|
|||
options.ModelBinders.Add(new ComplexModelDtoModelBinder());
|
||||
|
||||
// Set up default output formatters.
|
||||
options.OutputFormatters.Add(new JsonOutputFormatter(JsonOutputFormatter.CreateDefaultSettings(), true));
|
||||
options.OutputFormatters.Add(new JsonOutputFormatter(JsonOutputFormatter.CreateDefaultSettings(), false));
|
||||
|
||||
// Set up ValueProviders
|
||||
options.ValueProviderFactories.Add(new RouteValueValueProviderFactory());
|
||||
|
|
|
|||
|
|
@ -66,9 +66,11 @@ namespace Microsoft.AspNet.Mvc
|
|||
yield return describe.Transient<IInputFormatter, XmlDataContractSerializerInputFormatter>();
|
||||
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<IModelBindersProvider, DefaultModelBindersProvider>();
|
||||
yield return describe.Transient<ICompositeModelBinder, CompositeModelBinder>();
|
||||
|
||||
yield return describe.Transient<INestedProvider<FilterProviderContext>, DefaultFilterProvider>();
|
||||
|
||||
|
|
|
|||
|
|
@ -79,7 +79,7 @@ namespace Microsoft.AspNet.Mvc.Core.Test.ActionResults
|
|||
result.Formatters = new List<IOutputFormatter>
|
||||
{
|
||||
new CannotWriteFormatter(),
|
||||
new JsonOutputFormatter(JsonOutputFormatter.CreateDefaultSettings(), true),
|
||||
new JsonOutputFormatter(JsonOutputFormatter.CreateDefaultSettings(), false),
|
||||
};
|
||||
|
||||
// Act
|
||||
|
|
@ -145,7 +145,7 @@ namespace Microsoft.AspNet.Mvc.Core.Test.ActionResults
|
|||
result.Formatters = new List<IOutputFormatter>
|
||||
{
|
||||
new CannotWriteFormatter(),
|
||||
new JsonOutputFormatter(JsonOutputFormatter.CreateDefaultSettings(), true),
|
||||
new JsonOutputFormatter(JsonOutputFormatter.CreateDefaultSettings(), false),
|
||||
};
|
||||
// Act
|
||||
await result.ExecuteResultAsync(actionContext);
|
||||
|
|
@ -177,7 +177,7 @@ namespace Microsoft.AspNet.Mvc.Core.Test.ActionResults
|
|||
result.Formatters = new List<IOutputFormatter>
|
||||
{
|
||||
mockFormatter.Object,
|
||||
new JsonOutputFormatter(JsonOutputFormatter.CreateDefaultSettings(), true),
|
||||
new JsonOutputFormatter(JsonOutputFormatter.CreateDefaultSettings(), false),
|
||||
new CannotWriteFormatter()
|
||||
};
|
||||
// Act
|
||||
|
|
@ -207,7 +207,7 @@ namespace Microsoft.AspNet.Mvc.Core.Test.ActionResults
|
|||
result.Formatters = new List<IOutputFormatter>
|
||||
{
|
||||
new CannotWriteFormatter(),
|
||||
new JsonOutputFormatter(JsonOutputFormatter.CreateDefaultSettings(), true),
|
||||
new JsonOutputFormatter(JsonOutputFormatter.CreateDefaultSettings(), false),
|
||||
};
|
||||
|
||||
// Act
|
||||
|
|
@ -238,7 +238,7 @@ namespace Microsoft.AspNet.Mvc.Core.Test.ActionResults
|
|||
result.Formatters = new List<IOutputFormatter>
|
||||
{
|
||||
new CannotWriteFormatter(),
|
||||
new JsonOutputFormatter(JsonOutputFormatter.CreateDefaultSettings(), true),
|
||||
new JsonOutputFormatter(JsonOutputFormatter.CreateDefaultSettings(), false),
|
||||
};
|
||||
// Act
|
||||
await result.ExecuteResultAsync(actionContext);
|
||||
|
|
@ -269,7 +269,7 @@ namespace Microsoft.AspNet.Mvc.Core.Test.ActionResults
|
|||
result.Formatters = new List<IOutputFormatter>
|
||||
{
|
||||
new CannotWriteFormatter(),
|
||||
new JsonOutputFormatter(JsonOutputFormatter.CreateDefaultSettings(), true),
|
||||
new JsonOutputFormatter(JsonOutputFormatter.CreateDefaultSettings(), false),
|
||||
};
|
||||
// Act
|
||||
await result.ExecuteResultAsync(actionContext);
|
||||
|
|
@ -360,7 +360,7 @@ namespace Microsoft.AspNet.Mvc.Core.Test.ActionResults
|
|||
Object = nonStringValue,
|
||||
DeclaredType = nonStringValue.GetType()
|
||||
};
|
||||
var formatter = new JsonOutputFormatter(JsonOutputFormatter.CreateDefaultSettings(), true);
|
||||
var formatter = new JsonOutputFormatter(JsonOutputFormatter.CreateDefaultSettings(), false);
|
||||
formatter.WriteResponseContentHeaders(formatterContext);
|
||||
await formatter.WriteAsync(formatterContext);
|
||||
|
||||
|
|
@ -471,7 +471,7 @@ namespace Microsoft.AspNet.Mvc.Core.Test.ActionResults
|
|||
get
|
||||
{
|
||||
return new List<IOutputFormatter>()
|
||||
{ new JsonOutputFormatter(JsonOutputFormatter.CreateDefaultSettings(), indent: true) };
|
||||
{ new JsonOutputFormatter(JsonOutputFormatter.CreateDefaultSettings(), indent: false) };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@
|
|||
#if NET45
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.AspNet.Mvc.OptionDescriptors;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
|
|
@ -63,10 +64,10 @@ namespace Microsoft.AspNet.Mvc.Core.Test
|
|||
|
||||
// Assert
|
||||
Assert.Equal(4, collection.Count);
|
||||
Assert.Equal(formatter1, collection[0].OutputFormatter);
|
||||
Assert.Equal(formatter2, collection[1].OutputFormatter);
|
||||
Assert.Equal(type2, collection[2].OutputFormatterType);
|
||||
Assert.Equal(type1, collection[3].OutputFormatterType);
|
||||
Assert.Equal(formatter1, collection[0].Instance);
|
||||
Assert.Equal(formatter2, collection[1].Instance);
|
||||
Assert.Equal(type2, collection[2].OptionType);
|
||||
Assert.Equal(type1, collection[3].OptionType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue