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

Commit 948982e

Browse files
henkmollemapranavkm
authored andcommitted
Create custom collection for model binder providers
Fixes #6161
1 parent 873a2fe commit 948982e

13 files changed

Lines changed: 645 additions & 12 deletions

File tree

src/Microsoft.AspNetCore.Mvc.Abstractions/Formatters/FormatterCollection.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System;
45
using System.Collections.Generic;
56
using System.Collections.ObjectModel;
67

@@ -34,11 +35,20 @@ public FormatterCollection(IList<TFormatter> list)
3435
/// </summary>
3536
/// <typeparam name="T">The type to remove.</typeparam>
3637
public void RemoveType<T>() where T : TFormatter
38+
{
39+
RemoveType(typeof(T));
40+
}
41+
42+
/// <summary>
43+
/// Removes all formatters of the specified type.
44+
/// </summary>
45+
/// <param name="formatterType">The type to remove.</param>
46+
public void RemoveType(Type formatterType)
3747
{
3848
for (var i = Count - 1; i >= 0; i--)
3949
{
4050
var formatter = this[i];
41-
if (formatter is T)
51+
if (formatter.GetType() == formatterType)
4252
{
4353
RemoveAt(i);
4454
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Collections.ObjectModel;
7+
8+
namespace Microsoft.AspNetCore.Mvc.ApplicationModels
9+
{
10+
/// <summary>
11+
/// Represents a collection of application model conventions.
12+
/// </summary>
13+
public class ApplicationModelConventionCollection : Collection<IApplicationModelConvention>
14+
{
15+
/// <summary>
16+
/// Initializes a new instance of the <see cref="ApplicationModelConventionCollection"/> class that is empty.
17+
/// </summary>
18+
public ApplicationModelConventionCollection()
19+
{
20+
}
21+
22+
/// <summary>
23+
/// Initializes a new instance of the <see cref="ApplicationModelConventionCollection"/> class
24+
/// as a wrapper for the specified list.
25+
/// </summary>
26+
/// <param name="applicationModelConventions">The list that is wrapped by the new collection.</param>
27+
public ApplicationModelConventionCollection(IList<IApplicationModelConvention> applicationModelConventions)
28+
: base(applicationModelConventions)
29+
{
30+
}
31+
32+
/// <summary>
33+
/// Removes all application model conventions of the specified type.
34+
/// </summary>
35+
/// <typeparam name="TApplicationModelConvention">The type to remove.</typeparam>
36+
public void RemoveType<TApplicationModelConvention>() where TApplicationModelConvention : IApplicationModelConvention
37+
{
38+
RemoveType(typeof(TApplicationModelConvention));
39+
}
40+
41+
/// <summary>
42+
/// Removes all application model conventions of the specified type.
43+
/// </summary>
44+
/// <param name="applicationModelConventionType">The type to remove.</param>
45+
public void RemoveType(Type applicationModelConventionType)
46+
{
47+
for (var i = Count - 1; i >= 0; i--)
48+
{
49+
var applicationModelConvention = this[i];
50+
if (applicationModelConvention.GetType() == applicationModelConventionType)
51+
{
52+
RemoveAt(i);
53+
}
54+
}
55+
}
56+
}
57+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Collections.ObjectModel;
7+
8+
namespace Microsoft.AspNetCore.Mvc.ModelBinding.Metadata
9+
{
10+
/// <summary>
11+
/// Represents a collection of metadata details providers.
12+
/// </summary>
13+
public class MetadataDetailsProviderCollection : Collection<IMetadataDetailsProvider>
14+
{
15+
/// <summary>
16+
/// Initializes a new instance of the <see cref="MetadataDetailsProviderCollection"/> class that is empty.
17+
/// </summary>
18+
public MetadataDetailsProviderCollection()
19+
{
20+
}
21+
22+
/// <summary>
23+
/// Initializes a new instance of the <see cref="MetadataDetailsProviderCollection"/> class
24+
/// as a wrapper for the specified list.
25+
/// </summary>
26+
/// <param name="metadataDetailsProviders">The list that is wrapped by the new collection.</param>
27+
public MetadataDetailsProviderCollection(IList<IMetadataDetailsProvider> metadataDetailsProviders)
28+
: base(metadataDetailsProviders)
29+
{
30+
}
31+
32+
/// <summary>
33+
/// Removes all metadata details providers of the specified type.
34+
/// </summary>
35+
/// <typeparam name="TMetadataDetailsProvider">The type to remove.</typeparam>
36+
public void RemoveType<TMetadataDetailsProvider>() where TMetadataDetailsProvider : IMetadataDetailsProvider
37+
{
38+
RemoveType(typeof(TMetadataDetailsProvider));
39+
}
40+
41+
/// <summary>
42+
/// Removes all metadata details providers of the specified type.
43+
/// </summary>
44+
/// <param name="metadataDetailsProviderType">The type to remove.</param>
45+
public void RemoveType(Type metadataDetailsProviderType)
46+
{
47+
for (var i = Count - 1; i >= 0; i--)
48+
{
49+
var metadataDetailsProvider = this[i];
50+
if (metadataDetailsProvider.GetType() == metadataDetailsProviderType)
51+
{
52+
RemoveAt(i);
53+
}
54+
}
55+
}
56+
}
57+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Collections.ObjectModel;
7+
8+
namespace Microsoft.AspNetCore.Mvc.ModelBinding
9+
{
10+
/// <summary>
11+
/// Represents a collection of model binder providers.
12+
/// </summary>
13+
public class ModelBinderProviderCollection : Collection<IModelBinderProvider>
14+
{
15+
/// <summary>
16+
/// Initializes a new instance of the <see cref="ModelBinderProviderCollection"/> class that is empty.
17+
/// </summary>
18+
public ModelBinderProviderCollection()
19+
{
20+
}
21+
22+
/// <summary>
23+
/// Initializes a new instance of the <see cref="ModelBinderProviderCollection"/> class
24+
/// as a wrapper for the specified list.
25+
/// </summary>
26+
/// <param name="modelBinderProviders">The list that is wrapped by the new collection.</param>
27+
public ModelBinderProviderCollection(IList<IModelBinderProvider> modelBinderProviders)
28+
: base(modelBinderProviders)
29+
{
30+
}
31+
32+
/// <summary>
33+
/// Removes all model binder providers of the specified type.
34+
/// </summary>
35+
/// <typeparam name="TModelBinderProvider">The type to remove.</typeparam>
36+
public void RemoveType<TModelBinderProvider>() where TModelBinderProvider : IModelBinderProvider
37+
{
38+
RemoveType(typeof(TModelBinderProvider));
39+
}
40+
41+
/// <summary>
42+
/// Removes all model binder providers of the specified type.
43+
/// </summary>
44+
/// <param name="modelBinderProviderType">The type to remove.</param>
45+
public void RemoveType(Type modelBinderProviderType)
46+
{
47+
for (var i = Count - 1; i >= 0; i--)
48+
{
49+
var modelBinderProvider = this[i];
50+
if (modelBinderProvider.GetType() == modelBinderProviderType)
51+
{
52+
RemoveAt(i);
53+
}
54+
}
55+
}
56+
}
57+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Collections.ObjectModel;
7+
8+
namespace Microsoft.AspNetCore.Mvc.ModelBinding.Validation
9+
{
10+
/// <summary>
11+
/// Represents a collection of model validator providers.
12+
/// </summary>
13+
public class ModelValidatorProviderCollection : Collection<IModelValidatorProvider>
14+
{
15+
/// <summary>
16+
/// Initializes a new instance of the <see cref="ModelValidatorProviderCollection"/> class that is empty.
17+
/// </summary>
18+
public ModelValidatorProviderCollection()
19+
{
20+
}
21+
22+
/// <summary>
23+
/// Initializes a new instance of the <see cref="ModelValidatorProviderCollection"/> class
24+
/// as a wrapper for the specified list.
25+
/// </summary>
26+
/// <param name="modelValidatorProviders">The list that is wrapped by the new collection.</param>
27+
public ModelValidatorProviderCollection(IList<IModelValidatorProvider> modelValidatorProviders)
28+
: base(modelValidatorProviders)
29+
{
30+
}
31+
32+
/// <summary>
33+
/// Removes all model validator providers of the specified type.
34+
/// </summary>
35+
/// <typeparam name="TModelValidatorProvider">The type to remove.</typeparam>
36+
public void RemoveType<TModelValidatorProvider>() where TModelValidatorProvider : IModelValidatorProvider
37+
{
38+
RemoveType(typeof(TModelValidatorProvider));
39+
}
40+
41+
/// <summary>
42+
/// Removes all model validator providers of the specified type.
43+
/// </summary>
44+
/// <param name="modelValidatorProviderType">The type to remove.</param>
45+
public void RemoveType(Type modelValidatorProviderType)
46+
{
47+
for (var i = Count - 1; i >= 0; i--)
48+
{
49+
var modelValidatorProvider = this[i];
50+
if (modelValidatorProvider.GetType() == modelValidatorProviderType)
51+
{
52+
RemoveAt(i);
53+
}
54+
}
55+
}
56+
}
57+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Collections.ObjectModel;
7+
8+
namespace Microsoft.AspNetCore.Mvc.ModelBinding
9+
{
10+
/// <summary>
11+
/// Represents a collection of value provider factories.
12+
/// </summary>
13+
public class ValueProviderFactoryCollection : Collection<IValueProviderFactory>
14+
{
15+
/// <summary>
16+
/// Initializes a new instance of the <see cref="ValueProviderFactoryCollection"/> class that is empty.
17+
/// </summary>
18+
public ValueProviderFactoryCollection()
19+
{
20+
}
21+
22+
/// <summary>
23+
/// Initializes a new instance of the <see cref="ValueProviderFactoryCollection"/> class
24+
/// as a wrapper for the specified list.
25+
/// </summary>
26+
/// <param name="valueProviderFactories">The list that is wrapped by the new collection.</param>
27+
public ValueProviderFactoryCollection(IList<IValueProviderFactory> valueProviderFactories)
28+
: base(valueProviderFactories)
29+
{
30+
}
31+
32+
/// <summary>
33+
/// Removes all value provider factories of the specified type.
34+
/// </summary>
35+
/// <typeparam name="TValueProviderFactory">The type to remove.</typeparam>
36+
public void RemoveType<TValueProviderFactory>() where TValueProviderFactory : IValueProviderFactory
37+
{
38+
RemoveType(typeof(TValueProviderFactory));
39+
}
40+
41+
/// <summary>
42+
/// Removes all value provider factories of the specified type.
43+
/// </summary>
44+
/// <param name="valueProviderFactoryType">The type to remove.</param>
45+
public void RemoveType(Type valueProviderFactoryType)
46+
{
47+
for (var i = Count - 1; i >= 0; i--)
48+
{
49+
var valueProviderFactory = this[i];
50+
if (valueProviderFactory.GetType() == valueProviderFactoryType)
51+
{
52+
RemoveAt(i);
53+
}
54+
}
55+
}
56+
}
57+
}

src/Microsoft.AspNetCore.Mvc.Core/MvcOptions.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@ public class MvcOptions
2222
public MvcOptions()
2323
{
2424
CacheProfiles = new Dictionary<string, CacheProfile>(StringComparer.OrdinalIgnoreCase);
25-
Conventions = new List<IApplicationModelConvention>();
25+
Conventions = new ApplicationModelConventionCollection();
2626
Filters = new FilterCollection();
2727
FormatterMappings = new FormatterMappings();
2828
InputFormatters = new FormatterCollection<IInputFormatter>();
2929
OutputFormatters = new FormatterCollection<IOutputFormatter>();
30-
ModelBinderProviders = new List<IModelBinderProvider>();
30+
ModelBinderProviders = new ModelBinderProviderCollection();
3131
ModelBindingMessageProvider = new DefaultModelBindingMessageProvider();
32-
ModelMetadataDetailsProviders = new List<IMetadataDetailsProvider>();
33-
ModelValidatorProviders = new List<IModelValidatorProvider>();
34-
ValueProviderFactories = new List<IValueProviderFactory>();
32+
ModelMetadataDetailsProviders = new MetadataDetailsProviderCollection();
33+
ModelValidatorProviders = new ModelValidatorProviderCollection();
34+
ValueProviderFactories = new ValueProviderFactoryCollection();
3535
}
3636

3737
/// <summary>
@@ -56,7 +56,7 @@ public MvcOptions()
5656
/// Gets a list of <see cref="IApplicationModelConvention"/> instances that will be applied to
5757
/// the <see cref="ApplicationModel"/> when discovering actions.
5858
/// </summary>
59-
public IList<IApplicationModelConvention> Conventions { get; }
59+
public ApplicationModelConventionCollection Conventions { get; }
6060

6161
/// <summary>
6262
/// Gets a collection of <see cref="IFilterMetadata"/> which are used to construct filters that
@@ -100,7 +100,7 @@ public int MaxModelValidationErrors
100100
/// <summary>
101101
/// Gets a list of <see cref="IModelBinderProvider"/>s used by this application.
102102
/// </summary>
103-
public IList<IModelBinderProvider> ModelBinderProviders { get; }
103+
public ModelBinderProviderCollection ModelBinderProviders { get; }
104104

105105
/// <summary>
106106
/// Gets the default <see cref="ModelBinding.Metadata.ModelBindingMessageProvider"/>. Changes here are copied to the
@@ -122,12 +122,12 @@ public int MaxModelValidationErrors
122122
/// <li><see cref="IValidationMetadataProvider"/></li>
123123
/// </ul>
124124
/// </remarks>
125-
public IList<IMetadataDetailsProvider> ModelMetadataDetailsProviders { get; }
125+
public MetadataDetailsProviderCollection ModelMetadataDetailsProviders { get; }
126126

127127
/// <summary>
128128
/// Gets a list of <see cref="IModelValidatorProvider"/>s used by this application.
129129
/// </summary>
130-
public IList<IModelValidatorProvider> ModelValidatorProviders { get; }
130+
public ModelValidatorProviderCollection ModelValidatorProviders { get; }
131131

132132
/// <summary>
133133
/// Gets a list of <see cref="IOutputFormatter"/>s that are used by this application.
@@ -150,7 +150,7 @@ public int MaxModelValidationErrors
150150
/// <summary>
151151
/// Gets a list of <see cref="IValueProviderFactory"/> used by this application.
152152
/// </summary>
153-
public IList<IValueProviderFactory> ValueProviderFactories { get; }
153+
public ValueProviderFactoryCollection ValueProviderFactories { get; }
154154

155155
/// <summary>
156156
/// Gets or sets the SSL port that is used by this application when <see cref="RequireHttpsAttribute"/>
@@ -163,4 +163,4 @@ public int MaxModelValidationErrors
163163
/// </summary>
164164
public bool RequireHttpsPermanent { get; set; }
165165
}
166-
}
166+
}

0 commit comments

Comments
 (0)