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

Commit

Permalink
Add generic overloads on FilterCollection
Browse files Browse the repository at this point in the history
  • Loading branch information
henkmollema authored and rynowak committed Jun 28, 2017
1 parent 1886d53 commit 7166dfe
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 1 deletion.
62 changes: 62 additions & 0 deletions src/Microsoft.AspNetCore.Mvc.Core/Filters/FilterCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -29,6 +44,22 @@ public IFilterMetadata Add(Type filterType)
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>
Expand Down Expand Up @@ -59,6 +90,21 @@ public IFilterMetadata Add(Type filterType, int order)
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"/>.
Expand All @@ -80,6 +126,22 @@ public IFilterMetadata AddService(Type filterType)
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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<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()
{
Expand All @@ -36,6 +51,19 @@ public void Add_WithOrder_SetsOrder()
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()
{
Expand Down Expand Up @@ -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<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()
{
Expand All @@ -79,6 +122,19 @@ public void AddService_SetsOrder()
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()
{
Expand Down Expand Up @@ -107,4 +163,4 @@ private class NonFilter
{
}
}
}
}

0 comments on commit 7166dfe

Please sign in to comment.