From ec18b3512307f7cac66c26c0668b9bca05b52101 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B8ren=20Kruse?= Date: Fri, 25 Sep 2015 21:23:33 +0200 Subject: [PATCH] Add FormatterCollection #2945 --- .../Formatters/FormatterCollection.cs | 31 ++++++++++++ src/Microsoft.AspNet.Mvc.Core/MvcOptions.cs | 8 ++-- .../Formatters/FormatterCollectionTest.cs | 47 +++++++++++++++++++ 3 files changed, 82 insertions(+), 4 deletions(-) create mode 100644 src/Microsoft.AspNet.Mvc.Core/Formatters/FormatterCollection.cs create mode 100644 test/Microsoft.AspNet.Mvc.Core.Test/Formatters/FormatterCollectionTest.cs diff --git a/src/Microsoft.AspNet.Mvc.Core/Formatters/FormatterCollection.cs b/src/Microsoft.AspNet.Mvc.Core/Formatters/FormatterCollection.cs new file mode 100644 index 0000000000..005c0ab677 --- /dev/null +++ b/src/Microsoft.AspNet.Mvc.Core/Formatters/FormatterCollection.cs @@ -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 +{ + /// + /// Represents a collection of formatters. + /// + /// The type of formatters in the collection. + public class FormatterCollection : Collection + { + + /// + /// Removes all formatters of the specified type. + /// + /// The type to remove. + public void RemoveType() where T : TFormatter + { + for (var i = Count - 1; i >= 0; i--) + { + var formatter = this[i]; + if (formatter is T) + { + RemoveAt(i); + } + } + } + } +} diff --git a/src/Microsoft.AspNet.Mvc.Core/MvcOptions.cs b/src/Microsoft.AspNet.Mvc.Core/MvcOptions.cs index d3907fe078..92969b7783 100644 --- a/src/Microsoft.AspNet.Mvc.Core/MvcOptions.cs +++ b/src/Microsoft.AspNet.Mvc.Core/MvcOptions.cs @@ -25,8 +25,8 @@ public MvcOptions() Conventions = new List(); Filters = new FilterCollection(); FormatterMappings = new FormatterMappings(); - InputFormatters = new List(); - OutputFormatters = new List(); + InputFormatters = new FormatterCollection(); + OutputFormatters = new FormatterCollection(); ModelBinders = new List(); ModelMetadataDetailsProviders = new List(); ModelValidatorProviders = new List(); @@ -61,7 +61,7 @@ public MvcOptions() /// /// Gets a list of s that are used by this application. /// - public IList InputFormatters { get; } + public FormatterCollection InputFormatters { get; } /// /// Gets or sets the maximum number of validation errors that are allowed by this application before further @@ -109,7 +109,7 @@ public int MaxModelValidationErrors /// /// Gets a list of s that are used by this application. /// - public IList OutputFormatters { get; } + public FormatterCollection OutputFormatters { get; } /// /// Gets or sets the flag which causes content negotiation to ignore Accept header diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/Formatters/FormatterCollectionTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/Formatters/FormatterCollectionTest.cs new file mode 100644 index 0000000000..cf0ad2e578 --- /dev/null +++ b/test/Microsoft.AspNet.Mvc.Core.Test/Formatters/FormatterCollectionTest.cs @@ -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 + { + new TestOutputFormatter(), + new AnotherTestOutputFormatter(), + new TestOutputFormatter() + }; + + // Act + collection.RemoveType(); + + // 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(); + } + } + } +}