Add generic overloads on FilterCollection
This commit is contained in:
parent
1886d53d89
commit
7166dfecd7
|
|
@ -9,6 +9,21 @@ namespace Microsoft.AspNetCore.Mvc.Filters
|
|||
{
|
||||
public class FilterCollection : Collection<IFilterMetadata>
|
||||
{
|
||||
/// <summary>
|
||||
/// Adds a type representing an <see cref="IFilterMetadata"/>.
|
||||
/// </summary>
|
||||
/// <typeparam name="TFilterType">Type representing an <see cref="IFilterMetadata"/>.</typeparam>
|
||||
/// <returns>An <see cref="IFilterMetadata"/> representing the added type.</returns>
|
||||
/// <remarks>
|
||||
/// Filter instances will be created using
|
||||
/// <see cref="Microsoft.Extensions.DependencyInjection.ActivatorUtilities"/>.
|
||||
/// Use <see cref="AddService(Type)"/> to register a service as a filter.
|
||||
/// </remarks>
|
||||
public IFilterMetadata Add<TFilterType>() where TFilterType : IFilterMetadata
|
||||
{
|
||||
return Add(typeof(TFilterType));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a type representing an <see cref="IFilterMetadata"/>.
|
||||
/// </summary>
|
||||
|
|
@ -29,6 +44,22 @@ namespace Microsoft.AspNetCore.Mvc.Filters
|
|||
return Add(filterType, order: 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a type representing an <see cref="IFilterMetadata"/>.
|
||||
/// </summary>
|
||||
/// <typeparam name="TFilterType">Type representing an <see cref="IFilterMetadata"/>.</typeparam>
|
||||
/// <param name="order">The order of the added filter.</param>
|
||||
/// <returns>An <see cref="IFilterMetadata"/> representing the added type.</returns>
|
||||
/// <remarks>
|
||||
/// Filter instances will be created using
|
||||
/// <see cref="Microsoft.Extensions.DependencyInjection.ActivatorUtilities"/>.
|
||||
/// Use <see cref="AddService(Type)"/> to register a service as a filter.
|
||||
/// </remarks>
|
||||
public IFilterMetadata Add<TFilterType>(int order) where TFilterType : IFilterMetadata
|
||||
{
|
||||
return Add(typeof(TFilterType), order);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a type representing an <see cref="IFilterMetadata"/>.
|
||||
/// </summary>
|
||||
|
|
@ -59,6 +90,21 @@ namespace Microsoft.AspNetCore.Mvc.Filters
|
|||
Add(filter);
|
||||
return filter;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a type representing an <see cref="IFilterMetadata"/>.
|
||||
/// </summary>
|
||||
/// <typeparam name="TFilterType">Type representing an <see cref="IFilterMetadata"/>.</typeparam>
|
||||
/// <returns>An <see cref="IFilterMetadata"/> representing the added service type.</returns>
|
||||
/// <remarks>
|
||||
/// Filter instances will be created through dependency injection. Use
|
||||
/// <see cref="Add(Type)"/> to register a service that will be created via
|
||||
/// type activation.
|
||||
/// </remarks>
|
||||
public IFilterMetadata AddService<TFilterType>() where TFilterType : IFilterMetadata
|
||||
{
|
||||
return AddService(typeof(TFilterType));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a type representing an <see cref="IFilterMetadata"/>.
|
||||
|
|
@ -80,6 +126,22 @@ namespace Microsoft.AspNetCore.Mvc.Filters
|
|||
return AddService(filterType, order: 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a type representing an <see cref="IFilterMetadata"/>.
|
||||
/// </summary>
|
||||
/// <typeparam name="TFilterType">Type representing an <see cref="IFilterMetadata"/>.</typeparam>
|
||||
/// <param name="order">The order of the added filter.</param>
|
||||
/// <returns>An <see cref="IFilterMetadata"/> representing the added service type.</returns>
|
||||
/// <remarks>
|
||||
/// Filter instances will be created through dependency injection. Use
|
||||
/// <see cref="Add(Type)"/> to register a service that will be created via
|
||||
/// type activation.
|
||||
/// </remarks>
|
||||
public IFilterMetadata AddService<TFilterType>(int order) where TFilterType : IFilterMetadata
|
||||
{
|
||||
return AddService(typeof(TFilterType), order);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a type representing an <see cref="IFilterMetadata"/>.
|
||||
/// </summary>
|
||||
|
|
|
|||
|
|
@ -23,6 +23,21 @@ namespace Microsoft.AspNetCore.Mvc.Filters
|
|||
Assert.Same(typeFilter, Assert.Single(collection));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GenericAdd_UsesTypeFilterAttribute()
|
||||
{
|
||||
// Arrange
|
||||
var collection = new FilterCollection();
|
||||
|
||||
// Act
|
||||
var added = collection.Add<MyFilter>();
|
||||
|
||||
// Assert
|
||||
var typeFilter = Assert.IsType<TypeFilterAttribute>(added);
|
||||
Assert.Equal(typeof(MyFilter), typeFilter.ImplementationType);
|
||||
Assert.Same(typeFilter, Assert.Single(collection));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Add_WithOrder_SetsOrder()
|
||||
{
|
||||
|
|
@ -36,6 +51,19 @@ namespace Microsoft.AspNetCore.Mvc.Filters
|
|||
Assert.Equal(17, Assert.IsAssignableFrom<IOrderedFilter>(added).Order);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GenericAdd_WithOrder_SetsOrder()
|
||||
{
|
||||
// Arrange
|
||||
var collection = new FilterCollection();
|
||||
|
||||
// Act
|
||||
var added = collection.Add<MyFilter>(17);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(17, Assert.IsAssignableFrom<IOrderedFilter>(added).Order);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Add_ThrowsOnNonIFilter()
|
||||
{
|
||||
|
|
@ -66,6 +94,21 @@ namespace Microsoft.AspNetCore.Mvc.Filters
|
|||
Assert.Same(serviceFilter, Assert.Single(collection));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GenericAddService_UsesServiceFilterAttribute()
|
||||
{
|
||||
// Arrange
|
||||
var collection = new FilterCollection();
|
||||
|
||||
// Act
|
||||
var added = collection.AddService<MyFilter>();
|
||||
|
||||
// Assert
|
||||
var serviceFilter = Assert.IsType<ServiceFilterAttribute>(added);
|
||||
Assert.Equal(typeof(MyFilter), serviceFilter.ServiceType);
|
||||
Assert.Same(serviceFilter, Assert.Single(collection));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddService_SetsOrder()
|
||||
{
|
||||
|
|
@ -79,6 +122,19 @@ namespace Microsoft.AspNetCore.Mvc.Filters
|
|||
Assert.Equal(17, Assert.IsAssignableFrom<IOrderedFilter>(added).Order);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GenericAddService_SetsOrder()
|
||||
{
|
||||
// Arrange
|
||||
var collection = new FilterCollection();
|
||||
|
||||
// Act
|
||||
var added = collection.AddService<MyFilter>(17);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(17, Assert.IsAssignableFrom<IOrderedFilter>(added).Order);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddService_ThrowsOnNonIFilter()
|
||||
{
|
||||
|
|
@ -107,4 +163,4 @@ namespace Microsoft.AspNetCore.Mvc.Filters
|
|||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue