Adding MvcOptions.OutputFormatters property.

This commit is contained in:
harshgMSFT 2014-07-14 11:06:36 -07:00
parent 8ed7dadfd8
commit a2561281b7
9 changed files with 267 additions and 1 deletions

View File

@ -0,0 +1,61 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Microsoft.AspNet.Mvc.Core;
namespace Microsoft.AspNet.Mvc
{
/// <summary>
/// Encapsulates information that describes an <see cref="OutputFormatter"/>.
/// </summary>
public class OutputFormatterDescriptor
{
/// <summary>
/// Creates a new instance of <see cref="OutputFormatterDescriptor"/>.
/// </summary>
/// <param name="outputFormatterType">A <see cref="OutputFormatter/> type that the descriptor represents.
/// </param>
public OutputFormatterDescriptor([NotNull] Type outputFormatterType)
{
var formatterType = typeof(OutputFormatter);
if (!formatterType.IsAssignableFrom(outputFormatterType))
{
var message = Resources.FormatTypeMustDeriveFromType(outputFormatterType,
formatterType.FullName);
throw new ArgumentException(message, "outputFormatterType");
}
OutputFormatterType = outputFormatterType;
}
/// <summary>
/// Creates a new instance of <see cref="OutputFormatterDescriptor"/>.
/// </summary>
/// <param name="outputFormatter">An instance of <see cref="OutputFormatter"/>
/// that the descriptor represents.</param>
public OutputFormatterDescriptor([NotNull] OutputFormatter outputFormatter)
{
OutputFormatter = outputFormatter;
OutputFormatterType = outputFormatter.GetType();
}
/// <summary>
/// Gets the type of the <see cref="OutputFormatter"/>.
/// </summary>
public Type OutputFormatterType
{
get;
private set;
}
/// <summary>
/// Gets the instance of the <see cref="OutputFormatter"/>.
/// </summary>
public OutputFormatter OutputFormatter
{
get;
private set;
}
}
}

View File

@ -35,6 +35,8 @@
<Compile Include="Formatters\OutputFormatterContext.cs" />
<Compile Include="Formatters\JsonOutputFormatter.cs" />
<Compile Include="Formatters\OutputFormatter.cs" />
<Compile Include="Formatters\OutputFormatterDescriptor.cs" />
<Compile Include="OutputFormatterDescriptorExtensions.cs" />
<Compile Include="ReflectedActionDescriptor.cs" />
<Compile Include="ReflectedActionDescriptorProvider.cs" />
<Compile Include="ReflectedModelBuilder\IReflectedApplicationModelConvention.cs" />

View File

@ -24,6 +24,7 @@ namespace Microsoft.AspNet.Mvc
ModelBinders = new List<ModelBinderDescriptor>();
ViewEngines = new List<ViewEngineDescriptor>();
ValueProviderFactories = new List<IValueProviderFactory>();
OutputFormatters = new List<OutputFormatterDescriptor>();
}
/// <summary>
@ -49,6 +50,8 @@ namespace Microsoft.AspNet.Mvc
}
}
public List<OutputFormatterDescriptor> OutputFormatters { get; private set; }
/// <summary>
/// Provides programmatic configuration for the default <see cref="IViewEngine" />.
/// </summary>

View File

@ -0,0 +1,82 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
namespace Microsoft.AspNet.Mvc
{
/// <summary>
/// Extension methods for adding output formatters to a collection.
/// </summary>
public static class OutputFormatterDescriptorExtensions
{
/// <summary>
/// Adds a type representing a <see cref="OutputFormatter"/> to a descriptor collection.
/// </summary>
/// <param name="descriptors">A list of OutputFormatterDescriptors</param>
/// <param name="outputFormatterType">Type representing an <see cref="OutputFormatter"/>.</param>
/// <returns>OutputFormatterDescriptor representing the added instance.</returns>
public static OutputFormatterDescriptor Add([NotNull] this IList<OutputFormatterDescriptor> descriptors,
[NotNull] Type outputFormatterType)
{
var descriptor = new OutputFormatterDescriptor(outputFormatterType);
descriptors.Add(descriptor);
return descriptor;
}
/// <summary>
/// Inserts a type representing a <see cref="OutputFormatter"/> to a descriptor collection.
/// </summary>
/// <param name="descriptors">A list of OutputFormatterDescriptors</param>
/// <param name="outputFormatterType">Type representing an <see cref="OutputFormatter"/>.</param>
/// <returns>OutputFormatterDescriptor representing the inserted instance.</returns>
public static OutputFormatterDescriptor Insert([NotNull] this IList<OutputFormatterDescriptor> descriptors,
int index,
[NotNull] Type outputFormatterType)
{
if (index < 0 || index > descriptors.Count)
{
throw new ArgumentOutOfRangeException("index");
}
var descriptor = new OutputFormatterDescriptor(outputFormatterType);
descriptors.Insert(index, descriptor);
return descriptor;
}
/// <summary>
/// Adds an <see cref="OutputFormatter"/> to a descriptor collection.
/// </summary>
/// <param name="descriptors">A list of OutputFormatterDescriptors</param>
/// <param name="outputFormatter">An <see cref="OutputFormatter"/> instance.</param>
/// <returns>OutputFormatterDescriptor representing the added instance.</returns>
public static OutputFormatterDescriptor Add([NotNull] this IList<OutputFormatterDescriptor> descriptors,
[NotNull] OutputFormatter outputFormatter)
{
var descriptor = new OutputFormatterDescriptor(outputFormatter);
descriptors.Add(descriptor);
return descriptor;
}
/// <summary>
/// Insert an <see cref="OutputFormatter"/> to a descriptor collection.
/// </summary>
/// <param name="descriptors">A list of OutputFormatterDescriptors</param>
/// <param name="outputFormatter">An <see cref="OutputFormatter"/> instance.</param>
/// <returns>OutputFormatterDescriptor representing the added instance.</returns>
public static OutputFormatterDescriptor Insert([NotNull] this IList<OutputFormatterDescriptor> descriptors,
int index,
[NotNull] OutputFormatter outputFormatter)
{
if (index < 0 || index > descriptors.Count)
{
throw new ArgumentOutOfRangeException("index");
}
var descriptor = new OutputFormatterDescriptor(outputFormatter);
descriptors.Insert(index, descriptor);
return descriptor;
}
}
}

View File

@ -33,9 +33,13 @@ namespace Microsoft.AspNet.Mvc
options.ModelBinders.Add(new ComplexModelDtoModelBinder());
// Set up ValueProviders
options.ValueProviderFactories.Add(new RouteValueValueProviderFactory());
options.ValueProviderFactories.Add(new RouteValueValueProviderFactory());
options.ValueProviderFactories.Add(new QueryStringValueProviderFactory());
options.ValueProviderFactories.Add(new FormValueProviderFactory());
// Set up OutputFormatters
options.OutputFormatters.Add(new OutputFormatterDescriptor(
new JsonOutputFormatter(JsonOutputFormatter.CreateDefaultSettings(), indent: false)));
}
}
}

View File

@ -0,0 +1,73 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#if NET45
using System;
using System.Collections.Generic;
using Moq;
using Xunit;
namespace Microsoft.AspNet.Mvc.Core.Test
{
public class OutputFormatterDescriptorExtensionTest
{
[Theory]
[InlineData(-1)]
[InlineData(5)]
public void Insert_WithType_ThrowsIfIndexIsOutOfBounds(int index)
{
// Arrange
var collection = new List<OutputFormatterDescriptor>
{
new OutputFormatterDescriptor(Mock.Of<OutputFormatter>()),
new OutputFormatterDescriptor(Mock.Of<OutputFormatter>())
};
// Act & Assert
Assert.Throws<ArgumentOutOfRangeException>("index",
() => collection.Insert(index, typeof(OutputFormatter)));
}
[Theory]
[InlineData(-2)]
[InlineData(3)]
public void Insert_WithInstance_ThrowsIfIndexIsOutOfBounds(int index)
{
// Arrange
var collection = new List<OutputFormatterDescriptor>
{
new OutputFormatterDescriptor(Mock.Of<OutputFormatter>()),
new OutputFormatterDescriptor(Mock.Of<OutputFormatter>())
};
var formatter = Mock.Of<OutputFormatter>();
// Act & Assert
Assert.Throws<ArgumentOutOfRangeException>("index", () => collection.Insert(index, formatter));
}
[InlineData]
public void OutputFormatterDescriptors_AddsTypesAndInstances()
{
// Arrange
var formatter1 = Mock.Of<OutputFormatter>();
var formatter2 = Mock.Of<OutputFormatter>();
var type1 = typeof(JsonOutputFormatter);
var type2 = typeof(OutputFormatter);
var collection = new List<OutputFormatterDescriptor>();
// Act
collection.Add(formatter1);
collection.Insert(1, formatter2);
collection.Add(type1);
collection.Insert(2, type2);
// 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);
}
}
}
#endif

View File

@ -0,0 +1,24 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.AspNet.Testing;
using Xunit;
namespace Microsoft.AspNet.Mvc.Core.Test
{
public class OutputFormatterDescriptorTest
{
[Fact]
public void ConstructorThrows_IfTypeIsNotOutputFormatter()
{
// Arrange
var expected = "The type 'System.String' must derive from " +
"'Microsoft.AspNet.Mvc.OutputFormatter'.";
var type = typeof(string);
// Act & Assert
ExceptionAssert.ThrowsArgument(() => new OutputFormatterDescriptor(type), "outputFormatterType", expected);
}
}
}

View File

@ -33,6 +33,8 @@
<Compile Include="ExpiringFileInfoCacheTest.cs" />
<Compile Include="DefaultActionDiscoveryConventionsTests.cs" />
<Compile Include="Extensions\ViewEngineDscriptorExtensionsTest.cs" />
<Compile Include="Formatters\OutputFormatterDescriptorExtensionTest.cs" />
<Compile Include="Formatters\OutputFormatterDescriptorTest.cs" />
<Compile Include="Formatters\OutputFormatterTests.cs" />
<Compile Include="ReflectedModelBuilder\ReflectedParameterModelTests.cs" />
<Compile Include="ReflectedModelBuilder\ReflectedActionModelTests.cs" />

View File

@ -60,5 +60,20 @@ namespace Microsoft.AspNet.Mvc
Assert.IsType<QueryStringValueProviderFactory>(valueProviders[1]);
Assert.IsType<FormValueProviderFactory>(valueProviders[2]);
}
[Fact]
public void Setup_SetsUpOutputFormatters()
{
// Arrange
var mvcOptions = new MvcOptions();
var setup = new MvcOptionsSetup();
// Act
setup.Setup(mvcOptions);
// Assert
Assert.Equal(1, mvcOptions.OutputFormatters.Count);
Assert.IsType<JsonOutputFormatter>(mvcOptions.OutputFormatters[0].OutputFormatter);
}
}
}