From 9badd9386e6349f80b817277e86eee1330dfa541 Mon Sep 17 00:00:00 2001 From: Ryan Nowak Date: Sun, 20 Sep 2015 22:10:08 -0700 Subject: [PATCH] API-Review Strongly-typed collections for a few options types. We don't want these extensions methods defined in a wierd namespace, it's straightforward and future-proof to just make these strongly-typed collections. --- .../ExcludeTypeValidationFilterExtensions.cs | 43 ------------ .../FilterCollection.cs} | 68 ++++++++----------- .../ExcludeTypeValidationFilterCollection.cs | 34 ++++++++++ src/Microsoft.AspNet.Mvc.Core/MvcOptions.cs | 12 ++-- .../FilterCollectionTest.cs} | 22 +++--- ...ludeTypeValidationFilterCollectionTest.cs} | 7 +- 6 files changed, 80 insertions(+), 106 deletions(-) delete mode 100644 src/Microsoft.AspNet.Mvc.Core/ExcludeTypeValidationFilterExtensions.cs rename src/Microsoft.AspNet.Mvc.Core/{FilterCollectionExtensions.cs => Filters/FilterCollection.cs} (57%) create mode 100644 src/Microsoft.AspNet.Mvc.Core/ModelBinding/Validation/ExcludeTypeValidationFilterCollection.cs rename test/Microsoft.AspNet.Mvc.Core.Test/{FilterCollectionExtensionsTest.cs => Filters/FilterCollectionTest.cs} (79%) rename test/Microsoft.AspNet.Mvc.Core.Test/ModelBinding/{ExcludeTypeValidationFilterExtensionsTests.cs => Validation/ExcludeTypeValidationFilterCollectionTest.cs} (82%) diff --git a/src/Microsoft.AspNet.Mvc.Core/ExcludeTypeValidationFilterExtensions.cs b/src/Microsoft.AspNet.Mvc.Core/ExcludeTypeValidationFilterExtensions.cs deleted file mode 100644 index ec736bc618..0000000000 --- a/src/Microsoft.AspNet.Mvc.Core/ExcludeTypeValidationFilterExtensions.cs +++ /dev/null @@ -1,43 +0,0 @@ -// 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.Collections.Generic; -using Microsoft.AspNet.Mvc.ModelBinding.Validation; - -namespace Microsoft.AspNet.Mvc -{ - /// - /// Extensions for . - /// - public static class ExcludeTypeValidationFilterExtensions - { - /// - /// Adds a descriptor to the specified that excludes the properties of - /// the specified and its derived types from validaton. - /// - /// A list of which are used to - /// get a collection of exclude filters to be applied for filtering model properties during validation. - /// - /// which should be excluded from validation. - public static void Add(this IList excludeTypeValidationFilters, Type type) - { - var typeBasedExcludeFilter = new DefaultTypeBasedExcludeFilter(type); - excludeTypeValidationFilters.Add(typeBasedExcludeFilter); - } - - /// - /// Adds a descriptor to the specified that excludes the properties of - /// the type specified and its derived types from validaton. - /// - /// A list of which are used to - /// get a collection of exclude filters to be applied for filtering model properties during validation. - /// - /// Full name of the type which should be excluded from validation. - public static void Add(this IList excludeTypeValidationFilters, string typeFullName) - { - var filter = new DefaultTypeNameBasedExcludeFilter(typeFullName); - excludeTypeValidationFilters.Add(filter); - } - } -} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Core/FilterCollectionExtensions.cs b/src/Microsoft.AspNet.Mvc.Core/Filters/FilterCollection.cs similarity index 57% rename from src/Microsoft.AspNet.Mvc.Core/FilterCollectionExtensions.cs rename to src/Microsoft.AspNet.Mvc.Core/Filters/FilterCollection.cs index c0d88cd0c3..a4ff3bb3d3 100644 --- a/src/Microsoft.AspNet.Mvc.Core/FilterCollectionExtensions.cs +++ b/src/Microsoft.AspNet.Mvc.Core/Filters/FilterCollection.cs @@ -1,110 +1,96 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// 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.Collections.Generic; +using System.Collections.ObjectModel; using System.Reflection; using Microsoft.AspNet.Mvc.Core; -using Microsoft.AspNet.Mvc.Filters; using Microsoft.Framework.Internal; -namespace Microsoft.AspNet.Mvc +namespace Microsoft.AspNet.Mvc.Filters { - /// - /// Extension methods for adding filters to the global filters collection. - /// - public static class FilterCollectionExtensions + public class FilterCollection : Collection { /// - /// Adds a type representing an to a filter collection. + /// Adds a type representing an . /// - /// A collection of . /// Type representing an . /// An representing the added type. /// /// Filter instances will be created using /// . - /// Use to register a service as a filter. + /// Use to register a service as a filter. /// - public static IFilterMetadata Add( - [NotNull] this ICollection filters, - [NotNull] Type filterType) + public IFilterMetadata Add([NotNull] Type filterType) { - return Add(filters, filterType, order: 0); + return Add(filterType, order: 0); } /// - /// Adds a type representing an to a filter collection. + /// Adds a type representing an . /// - /// A collection of . /// 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. + /// Use to register a service as a filter. /// - public static IFilterMetadata Add( - [NotNull] this ICollection filters, - [NotNull] Type filterType, - int order) + public IFilterMetadata Add([NotNull] Type filterType, int order) { if (!typeof(IFilterMetadata).IsAssignableFrom(filterType)) { - var message = Resources.FormatTypeMustDeriveFromType(filterType.FullName, typeof(IFilterMetadata).FullName); + var message = Resources.FormatTypeMustDeriveFromType( + filterType.FullName, + typeof(IFilterMetadata).FullName); throw new ArgumentException(message, nameof(filterType)); } var filter = new TypeFilterAttribute(filterType) { Order = order }; - filters.Add(filter); + Add(filter); return filter; } /// - /// Adds a type representing an to a filter collection. + /// Adds a type representing an . /// - /// A collection of . /// Type representing an . /// An representing the added service type. /// /// Filter instances will created through dependency injection. Use - /// to register a service that will be created via + /// to register a service that will be created via /// type activation. /// - public static IFilterMetadata AddService( - [NotNull] this ICollection filters, - [NotNull] Type filterType) + public IFilterMetadata AddService([NotNull] Type filterType) { - return AddService(filters, filterType, order: 0); + return AddService(filterType, order: 0); } /// - /// Adds a type representing an to a filter collection. + /// Adds a type representing an . /// - /// A collection of . /// Type representing an . /// The order of the added filter. /// An representing the added service type. /// /// Filter instances will created through dependency injection. Use - /// to register a service that will be created via + /// to register a service that will be created via /// type activation. /// - public static IFilterMetadata AddService( - [NotNull] this ICollection filters, - [NotNull] Type filterType, - int order) + public IFilterMetadata AddService([NotNull] Type filterType, int order) { if (!typeof(IFilterMetadata).GetTypeInfo().IsAssignableFrom(filterType.GetTypeInfo())) { - var message = Resources.FormatTypeMustDeriveFromType(filterType.FullName, typeof(IFilterMetadata).FullName); + var message = Resources.FormatTypeMustDeriveFromType( + filterType.FullName, + typeof(IFilterMetadata).FullName); throw new ArgumentException(message, nameof(filterType)); } var filter = new ServiceFilterAttribute(filterType) { Order = order }; - filters.Add(filter); + Add(filter); return filter; } } -} \ No newline at end of file +} diff --git a/src/Microsoft.AspNet.Mvc.Core/ModelBinding/Validation/ExcludeTypeValidationFilterCollection.cs b/src/Microsoft.AspNet.Mvc.Core/ModelBinding/Validation/ExcludeTypeValidationFilterCollection.cs new file mode 100644 index 0000000000..7444b28da5 --- /dev/null +++ b/src/Microsoft.AspNet.Mvc.Core/ModelBinding/Validation/ExcludeTypeValidationFilterCollection.cs @@ -0,0 +1,34 @@ +// 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.Collections.ObjectModel; +using Microsoft.Framework.Internal; + +namespace Microsoft.AspNet.Mvc.ModelBinding.Validation +{ + public class ExcludeTypeValidationFilterCollection : Collection + { + /// + /// Adds an that excludes the properties of + /// the specified and its derived types from validaton. + /// + /// which should be excluded from validation. + public void Add([NotNull] Type type) + { + var typeBasedExcludeFilter = new DefaultTypeBasedExcludeFilter(type); + Add(typeBasedExcludeFilter); + } + + /// + /// Adds an that excludes the properties of + /// the specified and its derived types from validaton. + /// + /// Full name of the type which should be excluded from validation. + public void Add([NotNull] string typeFullName) + { + var filter = new DefaultTypeNameBasedExcludeFilter(typeFullName); + Add(filter); + } + } +} diff --git a/src/Microsoft.AspNet.Mvc.Core/MvcOptions.cs b/src/Microsoft.AspNet.Mvc.Core/MvcOptions.cs index 33300eefda..d3907fe078 100644 --- a/src/Microsoft.AspNet.Mvc.Core/MvcOptions.cs +++ b/src/Microsoft.AspNet.Mvc.Core/MvcOptions.cs @@ -23,14 +23,14 @@ namespace Microsoft.AspNet.Mvc { CacheProfiles = new Dictionary(StringComparer.OrdinalIgnoreCase); Conventions = new List(); - Filters = new List(); + Filters = new FilterCollection(); FormatterMappings = new FormatterMappings(); InputFormatters = new List(); OutputFormatters = new List(); ModelBinders = new List(); ModelMetadataDetailsProviders = new List(); ModelValidatorProviders = new List(); - ValidationExcludeFilters = new List(); + ValidationExcludeFilters = new ExcludeTypeValidationFilterCollection(); ValueProviderFactories = new List(); } @@ -47,10 +47,10 @@ namespace Microsoft.AspNet.Mvc public IList Conventions { get; } /// - /// Gets a list of which are used to construct filters that + /// Gets a collection of which are used to construct filters that /// apply to all actions. /// - public ICollection Filters { get; } + public FilterCollection Filters { get; } /// /// Used to specify mapping between the URL Format and corresponding @@ -118,9 +118,9 @@ namespace Microsoft.AspNet.Mvc public bool RespectBrowserAcceptHeader { get; set; } /// - /// Gets a list of s that are used by this application. + /// Gets a collection of s that are used by this application. /// - public IList ValidationExcludeFilters { get; } + public ExcludeTypeValidationFilterCollection ValidationExcludeFilters { get; } /// /// Gets a list of used by this application. diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/FilterCollectionExtensionsTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/Filters/FilterCollectionTest.cs similarity index 79% rename from test/Microsoft.AspNet.Mvc.Core.Test/FilterCollectionExtensionsTest.cs rename to test/Microsoft.AspNet.Mvc.Core.Test/Filters/FilterCollectionTest.cs index 0947ac0f25..671c39e6c3 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/FilterCollectionExtensionsTest.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/Filters/FilterCollectionTest.cs @@ -2,19 +2,17 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; -using System.Collections.ObjectModel; -using Microsoft.AspNet.Mvc.Filters; using Xunit; -namespace Microsoft.AspNet.Mvc +namespace Microsoft.AspNet.Mvc.Filters { - public class FilterCollectionExtensionsTest + public class FilterCollectionTest { [Fact] public void Add_UsesTypeFilterAttribute() { // Arrange - var collection = new Collection(); + var collection = new FilterCollection(); // Act var added = collection.Add(typeof(MyFilter)); @@ -29,7 +27,7 @@ namespace Microsoft.AspNet.Mvc public void Add_WithOrder_SetsOrder() { // Arrange - var collection = new Collection(); + var collection = new FilterCollection(); // Act var added = collection.Add(typeof(MyFilter), 17); @@ -42,10 +40,10 @@ namespace Microsoft.AspNet.Mvc public void Add_ThrowsOnNonIFilter() { // Arrange - var collection = new Collection(); + var collection = new FilterCollection(); var expectedMessage = - "The type 'Microsoft.AspNet.Mvc.FilterCollectionExtensionsTest+NonFilter' must derive from " + + $"The type '{typeof(NonFilter).FullName}' must derive from " + $"'{typeof(IFilterMetadata).FullName}'." + Environment.NewLine + "Parameter name: filterType"; @@ -60,7 +58,7 @@ namespace Microsoft.AspNet.Mvc public void AddService_UsesServiceFilterAttribute() { // Arrange - var collection = new Collection(); + var collection = new FilterCollection(); // Act var added = collection.AddService(typeof(MyFilter)); @@ -75,7 +73,7 @@ namespace Microsoft.AspNet.Mvc public void AddService_SetsOrder() { // Arrange - var collection = new Collection(); + var collection = new FilterCollection(); // Act var added = collection.AddService(typeof(MyFilter), 17); @@ -88,10 +86,10 @@ namespace Microsoft.AspNet.Mvc public void AddService_ThrowsOnNonIFilter() { // Arrange - var collection = new Collection(); + var collection = new FilterCollection(); var expectedMessage = - "The type 'Microsoft.AspNet.Mvc.FilterCollectionExtensionsTest+NonFilter' must derive from " + + $"The type '{typeof(NonFilter).FullName}' must derive from " + $"'{typeof(IFilterMetadata).FullName}'." + Environment.NewLine + "Parameter name: filterType"; diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/ModelBinding/ExcludeTypeValidationFilterExtensionsTests.cs b/test/Microsoft.AspNet.Mvc.Core.Test/ModelBinding/Validation/ExcludeTypeValidationFilterCollectionTest.cs similarity index 82% rename from test/Microsoft.AspNet.Mvc.Core.Test/ModelBinding/ExcludeTypeValidationFilterExtensionsTests.cs rename to test/Microsoft.AspNet.Mvc.Core.Test/ModelBinding/Validation/ExcludeTypeValidationFilterCollectionTest.cs index 2b6cc69528..8141b53dba 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/ModelBinding/ExcludeTypeValidationFilterExtensionsTests.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/ModelBinding/Validation/ExcludeTypeValidationFilterCollectionTest.cs @@ -1,20 +1,19 @@ // 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.Generic; using Microsoft.AspNet.Mvc.ModelBinding.Validation; using Xunit; namespace Microsoft.AspNet.Mvc.ModelBinding { - public class ExcludeTypeValidationFilterExtensionsTests + public class ExcludeTypeValidationFilterCollectionTest { [Fact] public void AddFilter_ByType() { // Arrange var type = typeof(BaseType); - var collection = new List(); + var collection = new ExcludeTypeValidationFilterCollection(); // Act collection.Add(type); @@ -29,7 +28,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding { // Arrange var type = typeof(BaseType); - var collection = new List(); + var collection = new ExcludeTypeValidationFilterCollection(); // Act collection.Add(type.FullName);