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

Commit 7c5a16c

Browse files
committed
Add RemoveType methods of PageConventionCollection
1 parent bc4328d commit 7c5a16c

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

src/Microsoft.AspNetCore.Mvc.RazorPages/ApplicationModels/PageConventionCollection.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,31 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5+
using System.Collections.Generic;
56
using System.Collections.ObjectModel;
67
using Microsoft.AspNetCore.Mvc.RazorPages;
78

89
namespace Microsoft.AspNetCore.Mvc.ApplicationModels
910
{
1011
public class PageConventionCollection : Collection<IPageConvention>
1112
{
13+
/// <summary>
14+
/// Initializes a new instance of the <see cref="PageConventionCollection"/> class that is empty.
15+
/// </summary>
16+
public PageConventionCollection()
17+
{
18+
}
19+
20+
/// <summary>
21+
/// Initializes a new instance of the <see cref="PageConventionCollection"/> class
22+
/// as a wrapper for the specified list.
23+
/// </summary>
24+
/// <param name="conventions">The list that is wrapped by the new collection.</param>
25+
public PageConventionCollection(IList<IPageConvention> conventions)
26+
: base(conventions)
27+
{
28+
}
29+
1230
/// <summary>
1331
/// Creates and adds an <see cref="IPageApplicationModelConvention"/> that invokes an action on the
1432
/// <see cref="PageApplicationModel"/> for the page with the speciifed name.
@@ -87,6 +105,31 @@ public IPageRouteModelConvention AddFolderRouteModelConvention(string folderPath
87105
return Add(new FolderRouteModelConvention(folderPath, action));
88106
}
89107

108+
/// <summary>
109+
/// Removes all <see cref="IPageConvention"/> instances of the specified type.
110+
/// </summary>
111+
/// <typeparam name="TPageConvention">The type to remove.</typeparam>
112+
public void RemoveType<TPageConvention>() where TPageConvention : IPageConvention
113+
{
114+
RemoveType(typeof(TPageConvention));
115+
}
116+
117+
/// <summary>
118+
/// Removes all <see cref="IPageConvention"/> instances of the specified type.
119+
/// </summary>
120+
/// <param name="pageConventionType">The type to remove.</param>
121+
public void RemoveType(Type pageConventionType)
122+
{
123+
for (var i = Count - 1; i >= 0; i--)
124+
{
125+
var pageConvention = this[i];
126+
if (pageConvention.GetType() == pageConventionType)
127+
{
128+
RemoveAt(i);
129+
}
130+
}
131+
}
132+
90133
// Internal for unit testing
91134
internal static void EnsureValidPageName(string pageName)
92135
{

test/Microsoft.AspNetCore.Mvc.RazorPages.Test/ApplicationModels/PageConventionCollectionTest.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,49 @@ public void EnsureValidFolderPath_ThrowsIfPageNameDoesNotStartWithLeadingSlash(s
7272
"folderPath",
7373
"Path must be a root relative path that starts with a forward slash '/'.");
7474
}
75+
76+
[Fact]
77+
public void RemoveType_RemovesAllOfType()
78+
{
79+
// Arrange
80+
var collection = new PageConventionCollection
81+
{
82+
new FooPageConvention(),
83+
new BarPageConvention(),
84+
new FooPageConvention()
85+
};
86+
87+
// Act
88+
collection.RemoveType(typeof(FooPageConvention));
89+
90+
// Assert
91+
Assert.Collection(
92+
collection,
93+
convention => Assert.IsType<BarPageConvention>(convention));
94+
}
95+
96+
[Fact]
97+
public void GenericRemoveType_RemovesAllOfType()
98+
{
99+
// Arrange
100+
var collection = new PageConventionCollection
101+
{
102+
new FooPageConvention(),
103+
new BarPageConvention(),
104+
new FooPageConvention()
105+
};
106+
107+
// Act
108+
collection.RemoveType<FooPageConvention>();
109+
110+
// Assert
111+
Assert.Collection(
112+
collection,
113+
convention => Assert.IsType<BarPageConvention>(convention));
114+
}
115+
116+
private class FooPageConvention : IPageConvention { }
117+
118+
private class BarPageConvention : IPageConvention { }
75119
}
76120
}

0 commit comments

Comments
 (0)