Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Commit

Permalink
Add FormatterCollection<T>
Browse files Browse the repository at this point in the history
  • Loading branch information
Krusen authored and rynowak committed Sep 28, 2015
1 parent 67b43b4 commit ec18b35
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 4 deletions.
31 changes: 31 additions & 0 deletions src/Microsoft.AspNet.Mvc.Core/Formatters/FormatterCollection.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
}
}
8 changes: 4 additions & 4 deletions src/Microsoft.AspNet.Mvc.Core/MvcOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public MvcOptions()
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>();
Expand Down Expand Up @@ -61,7 +61,7 @@ public MvcOptions()
/// <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
Expand Down Expand Up @@ -109,7 +109,7 @@ public int MaxModelValidationErrors
/// <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
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
}

0 comments on commit ec18b35

Please sign in to comment.