Add FormatterCollection<T>

#2945
This commit is contained in:
Søren Kruse 2015-09-25 21:23:33 +02:00 committed by Ryan Nowak
parent 67b43b4cfe
commit ec18b35123
3 changed files with 82 additions and 4 deletions

View File

@ -0,0 +1,31 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.ObjectModel;
namespace Microsoft.AspNet.Mvc.Formatters
{
/// <summary>
/// Represents a collection of formatters.
/// </summary>
/// <typeparam name="TFormatter">The type of formatters in the collection.</typeparam>
public class FormatterCollection<TFormatter> : Collection<TFormatter>
{
/// <summary>
/// Removes all formatters of the specified type.
/// </summary>
/// <typeparam name="T">The type to remove.</typeparam>
public void RemoveType<T>() where T : TFormatter
{
for (var i = Count - 1; i >= 0; i--)
{
var formatter = this[i];
if (formatter is T)
{
RemoveAt(i);
}
}
}
}
}

View File

@ -25,8 +25,8 @@ namespace Microsoft.AspNet.Mvc
Conventions = new List<IApplicationModelConvention>();
Filters = new FilterCollection();
FormatterMappings = new FormatterMappings();
InputFormatters = new List<IInputFormatter>();
OutputFormatters = new List<IOutputFormatter>();
InputFormatters = new FormatterCollection<IInputFormatter>();
OutputFormatters = new FormatterCollection<IOutputFormatter>();
ModelBinders = new List<IModelBinder>();
ModelMetadataDetailsProviders = new List<IMetadataDetailsProvider>();
ModelValidatorProviders = new List<IModelValidatorProvider>();
@ -61,7 +61,7 @@ namespace Microsoft.AspNet.Mvc
/// <summary>
/// Gets a list of <see cref="IInputFormatter"/>s that are used by this application.
/// </summary>
public IList<IInputFormatter> InputFormatters { get; }
public FormatterCollection<IInputFormatter> InputFormatters { get; }
/// <summary>
/// Gets or sets the maximum number of validation errors that are allowed by this application before further
@ -109,7 +109,7 @@ namespace Microsoft.AspNet.Mvc
/// <summary>
/// Gets a list of <see cref="IOutputFormatter"/>s that are used by this application.
/// </summary>
public IList<IOutputFormatter> OutputFormatters { get; }
public FormatterCollection<IOutputFormatter> OutputFormatters { get; }
/// <summary>
/// Gets or sets the flag which causes content negotiation to ignore Accept header

View File

@ -0,0 +1,47 @@
// Copyright (c) .NET Foundation. 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.Threading.Tasks;
using Xunit;
namespace Microsoft.AspNet.Mvc.Formatters
{
public class FormatterCollectionTest
{
[Fact]
public void RemoveType_RemovesAllOfType()
{
// Arrange
var collection = new FormatterCollection<IOutputFormatter>
{
new TestOutputFormatter(),
new AnotherTestOutputFormatter(),
new TestOutputFormatter()
};
// Act
collection.RemoveType<TestOutputFormatter>();
// Assert
var formatter = Assert.Single(collection);
Assert.IsType(typeof(AnotherTestOutputFormatter), formatter);
}
private class TestOutputFormatter : OutputFormatter
{
public override Task WriteResponseBodyAsync(OutputFormatterContext context)
{
throw new NotImplementedException();
}
}
private class AnotherTestOutputFormatter : OutputFormatter
{
public override Task WriteResponseBodyAsync(OutputFormatterContext context)
{
throw new NotImplementedException();
}
}
}
}