From 7166dfecd75779fae4d03bd9b2ff5ed00cfede6e Mon Sep 17 00:00:00 2001 From: Henk Mollema Date: Wed, 28 Jun 2017 17:17:34 +0200 Subject: [PATCH] Add generic overloads on FilterCollection --- .../Filters/FilterCollection.cs | 62 +++++++++++++++++++ .../Filters/FilterCollectionTest.cs | 58 ++++++++++++++++- 2 files changed, 119 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.AspNetCore.Mvc.Core/Filters/FilterCollection.cs b/src/Microsoft.AspNetCore.Mvc.Core/Filters/FilterCollection.cs index 9602800ec0..b5ea9a26c1 100644 --- a/src/Microsoft.AspNetCore.Mvc.Core/Filters/FilterCollection.cs +++ b/src/Microsoft.AspNetCore.Mvc.Core/Filters/FilterCollection.cs @@ -9,6 +9,21 @@ namespace Microsoft.AspNetCore.Mvc.Filters { public class FilterCollection : Collection { + /// + /// Adds a type representing an . + /// + /// Type representing an . + /// An representing the added type. + /// + /// Filter instances will be created using + /// . + /// Use to register a service as a filter. + /// + public IFilterMetadata Add() where TFilterType : IFilterMetadata + { + return Add(typeof(TFilterType)); + } + /// /// Adds a type representing an . /// @@ -29,6 +44,22 @@ public IFilterMetadata Add(Type filterType) return Add(filterType, order: 0); } + /// + /// Adds a type representing an . + /// + /// Type representing an . + /// The order of the added filter. + /// An representing the added type. + /// + /// Filter instances will be created using + /// . + /// Use to register a service as a filter. + /// + public IFilterMetadata Add(int order) where TFilterType : IFilterMetadata + { + return Add(typeof(TFilterType), order); + } + /// /// Adds a type representing an . /// @@ -59,6 +90,21 @@ public IFilterMetadata Add(Type filterType, int order) Add(filter); return filter; } + + /// + /// Adds a type representing an . + /// + /// Type representing an . + /// An representing the added service type. + /// + /// Filter instances will be created through dependency injection. Use + /// to register a service that will be created via + /// type activation. + /// + public IFilterMetadata AddService() where TFilterType : IFilterMetadata + { + return AddService(typeof(TFilterType)); + } /// /// Adds a type representing an . @@ -80,6 +126,22 @@ public IFilterMetadata AddService(Type filterType) return AddService(filterType, order: 0); } + /// + /// Adds a type representing an . + /// + /// Type representing an . + /// The order of the added filter. + /// An representing the added service type. + /// + /// Filter instances will be created through dependency injection. Use + /// to register a service that will be created via + /// type activation. + /// + public IFilterMetadata AddService(int order) where TFilterType : IFilterMetadata + { + return AddService(typeof(TFilterType), order); + } + /// /// Adds a type representing an . /// diff --git a/test/Microsoft.AspNetCore.Mvc.Core.Test/Filters/FilterCollectionTest.cs b/test/Microsoft.AspNetCore.Mvc.Core.Test/Filters/FilterCollectionTest.cs index 095ee2dd8e..971e43eebf 100644 --- a/test/Microsoft.AspNetCore.Mvc.Core.Test/Filters/FilterCollectionTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Core.Test/Filters/FilterCollectionTest.cs @@ -23,6 +23,21 @@ public void Add_UsesTypeFilterAttribute() Assert.Same(typeFilter, Assert.Single(collection)); } + [Fact] + public void GenericAdd_UsesTypeFilterAttribute() + { + // Arrange + var collection = new FilterCollection(); + + // Act + var added = collection.Add(); + + // Assert + var typeFilter = Assert.IsType(added); + Assert.Equal(typeof(MyFilter), typeFilter.ImplementationType); + Assert.Same(typeFilter, Assert.Single(collection)); + } + [Fact] public void Add_WithOrder_SetsOrder() { @@ -36,6 +51,19 @@ public void Add_WithOrder_SetsOrder() Assert.Equal(17, Assert.IsAssignableFrom(added).Order); } + [Fact] + public void GenericAdd_WithOrder_SetsOrder() + { + // Arrange + var collection = new FilterCollection(); + + // Act + var added = collection.Add(17); + + // Assert + Assert.Equal(17, Assert.IsAssignableFrom(added).Order); + } + [Fact] public void Add_ThrowsOnNonIFilter() { @@ -66,6 +94,21 @@ public void AddService_UsesServiceFilterAttribute() Assert.Same(serviceFilter, Assert.Single(collection)); } + [Fact] + public void GenericAddService_UsesServiceFilterAttribute() + { + // Arrange + var collection = new FilterCollection(); + + // Act + var added = collection.AddService(); + + // Assert + var serviceFilter = Assert.IsType(added); + Assert.Equal(typeof(MyFilter), serviceFilter.ServiceType); + Assert.Same(serviceFilter, Assert.Single(collection)); + } + [Fact] public void AddService_SetsOrder() { @@ -79,6 +122,19 @@ public void AddService_SetsOrder() Assert.Equal(17, Assert.IsAssignableFrom(added).Order); } + [Fact] + public void GenericAddService_SetsOrder() + { + // Arrange + var collection = new FilterCollection(); + + // Act + var added = collection.AddService(17); + + // Assert + Assert.Equal(17, Assert.IsAssignableFrom(added).Order); + } + [Fact] public void AddService_ThrowsOnNonIFilter() { @@ -107,4 +163,4 @@ private class NonFilter { } } -} \ No newline at end of file +}