Adding IFilterFactory

This is a generalized factory/provider for filters - TypeFilterAttribute
and ServiceFilterAttribute are now implemented in terms of this interface.
This commit is contained in:
Ryan Nowak 2014-04-29 15:56:26 -07:00
parent 185ad31491
commit 20c8dece7b
16 changed files with 197 additions and 166 deletions

View File

@ -1,28 +1,37 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.Mvc.Filters;
using MvcSample.Web.Models;
namespace MvcSample.Web.Filters
{
public class InspectResultPageAttribute : ActionFilterAttribute
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
public class InspectResultPageAttribute : Attribute, IFilterFactory
{
public override async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next)
public IFilter CreateInstance(IServiceProvider serviceProvider)
{
var viewResult = context.Result as ViewResult;
return new InspectResultPageFilter();
}
if (viewResult != null)
private class InspectResultPageFilter : IResultFilter
{
public void OnResultExecuting(ResultExecutingContext context)
{
var user = viewResult.ViewData.Model as User;
var viewResult = context.Result as ViewResult;
if (user != null)
if (viewResult != null)
{
user.Name += "**" + user.Name + "**";
var user = viewResult.ViewData.Model as User;
if (user != null)
{
user.Name += "**" + user.Name + "**";
}
}
}
await next();
public void OnResultExecuted(ResultExecutedContext context)
{
}
}
}
}

View File

@ -4,8 +4,12 @@ namespace MvcSample.Web.Filters
{
public class UserNameProvider : IActionFilter
{
private static readonly string[] _userNames = new[] { "Jon", "David", "Goliath" };
private static int _index;
private readonly UserNameService _nameService;
public UserNameProvider(UserNameService nameService)
{
_nameService = nameService;
}
public void OnActionExecuting(ActionExecutingContext context)
{
@ -17,7 +21,7 @@ namespace MvcSample.Web.Filters
if (string.IsNullOrWhiteSpace(userName))
{
context.ActionArguments["userName"] = _userNames[(_index++)%3];
context.ActionArguments["userName"] = _nameService.GetName();
}
}

View File

@ -0,0 +1,15 @@
using Microsoft.AspNet.Mvc;
namespace MvcSample.Web.Filters
{
public class UserNameService
{
private static readonly string[] _userNames = new[] { "Jon", "David", "Goliath" };
private static int _index;
public string GetName()
{
return _userNames[_index++ % 3];
}
}
}

View File

@ -50,6 +50,7 @@
<Compile Include="Filters\InspectResultPageAttribute.cs" />
<Compile Include="Filters\PassThroughAttribute.cs" />
<Compile Include="Filters\UserNameProvider.cs" />
<Compile Include="Filters\UserNameService.cs" />
<Compile Include="Home2Controller.cs" />
<Compile Include="HomeController.cs" />
<Compile Include="LinkController.cs" />

View File

@ -2,6 +2,7 @@
using Microsoft.AspNet.Abstractions;
using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.Routing;
using MvcSample.Web.Filters;
namespace MvcSample.Web
{
@ -13,6 +14,7 @@ namespace MvcSample.Web
{
services.AddMvc();
services.AddSingleton<PassThroughAttribute, PassThroughAttribute>();
services.AddSingleton<UserNameService, UserNameService>();
});
app.UseMvc(routes =>

View File

@ -1,6 +1,7 @@
using System;
using System.Diagnostics.Contracts;
using Microsoft.AspNet.DependencyInjection;
using Microsoft.AspNet.Mvc.Core;
namespace Microsoft.AspNet.Mvc.Filters
{
@ -46,45 +47,23 @@ namespace Microsoft.AspNet.Mvc.Filters
var filter = filterItem.Descriptor.Filter;
var serviceFilterSignature = filter as IServiceFilter;
if (serviceFilterSignature != null)
var filterFactory = filter as IFilterFactory;
if (filterFactory == null)
{
var serviceFilter = ServiceProvider.GetService(serviceFilterSignature.ServiceType) as IFilter;
if (serviceFilter == null)
{
throw new InvalidOperationException("Service filter must be of type IFilter");
}
filterItem.Filter = serviceFilter;
filterItem.Filter = filter;
}
else
{
var typeFilterSignature = filter as ITypeFilter;
if (typeFilterSignature != null)
filterItem.Filter = filterFactory.CreateInstance(ServiceProvider);
if (filterItem.Filter == null)
{
if (typeFilterSignature.ImplementationType == null)
{
throw new InvalidOperationException("Type filter must provide a type to instantiate");
}
if (!typeof (IFilter).IsAssignableFrom(typeFilterSignature.ImplementationType))
{
throw new InvalidOperationException("Type filter must implement IFilter");
}
var typeFilter = (IFilter)_typeActivator.CreateInstance(
ServiceProvider,
typeFilterSignature.ImplementationType,
typeFilterSignature.Arguments);
ApplyFilterToContainer(typeFilter, filter);
filterItem.Filter = typeFilter;
}
else
{
filterItem.Filter = filter;
throw new InvalidOperationException(Resources.FormatTypeMethodMustReturnNotNullValue(
"CreateInstance",
typeof(IFilterFactory).Name));
}
ApplyFilterToContainer(filterItem.Filter, filterFactory);
}
}

View File

@ -0,0 +1,9 @@
using System;
namespace Microsoft.AspNet.Mvc
{
public interface IFilterFactory : IFilter
{
IFilter CreateInstance([NotNull] IServiceProvider serviceProvider);
}
}

View File

@ -1,9 +0,0 @@
using System;
namespace Microsoft.AspNet.Mvc
{
public interface IServiceFilter : IFilter
{
Type ServiceType { get; }
}
}

View File

@ -1,11 +0,0 @@
using System;
namespace Microsoft.AspNet.Mvc
{
public interface ITypeFilter : IFilter
{
object[] Arguments { get; }
Type ImplementationType { get; }
}
}

View File

@ -1,13 +1,14 @@
using System;
using System.Diagnostics;
using Microsoft.AspNet.Mvc.Core;
namespace Microsoft.AspNet.Mvc
{
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
[DebuggerDisplay("ServiceFilter: Type={ServiceType} Order={Order}")]
public class ServiceFilterAttribute : Attribute, IServiceFilter
public class ServiceFilterAttribute : Attribute, IFilterFactory, IOrderedFilter
{
public ServiceFilterAttribute(Type type)
public ServiceFilterAttribute([NotNull] Type type)
{
ServiceType = type;
}
@ -15,5 +16,20 @@ namespace Microsoft.AspNet.Mvc
public Type ServiceType { get; private set; }
public int Order { get; set; }
public IFilter CreateInstance([NotNull] IServiceProvider serviceProvider)
{
var service = serviceProvider.GetService(ServiceType);
var filter = service as IFilter;
if (filter == null)
{
throw new InvalidOperationException(Resources.FormatFilterFactoryAttribute_TypeMustImplementIFilter(
typeof(ServiceFilterAttribute).Name,
typeof(IFilter).Name));
}
return filter;
}
}
}

View File

@ -1,13 +1,15 @@
using System;
using System.Diagnostics;
using Microsoft.AspNet.DependencyInjection;
using Microsoft.AspNet.Mvc.Core;
namespace Microsoft.AspNet.Mvc
{
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
[DebuggerDisplay("TypeFilter: Type={ImplementationType} Order={Order}")]
public class TypeFilterAttribute : Attribute, ITypeFilter, IOrderedFilter
public class TypeFilterAttribute : Attribute, IFilterFactory, IOrderedFilter
{
public TypeFilterAttribute(Type type)
public TypeFilterAttribute([NotNull] Type type)
{
ImplementationType = type;
}
@ -17,5 +19,21 @@ namespace Microsoft.AspNet.Mvc
public Type ImplementationType { get; private set; }
public int Order { get; set; }
public IFilter CreateInstance([NotNull] IServiceProvider serviceProvider)
{
var activator = serviceProvider.GetService<ITypeActivator>();
var obj = activator.CreateInstance(serviceProvider, ImplementationType, Arguments ?? new object[0]);
var filter = obj as IFilter;
if (filter == null)
{
throw new InvalidOperationException(Resources.FormatFilterFactoryAttribute_TypeMustImplementIFilter(
typeof(TypeFilterAttribute).Name,
typeof(IFilter).Name));
}
return filter;
}
}
}

View File

@ -103,8 +103,7 @@
<Compile Include="Filters\IFilterContainer.cs" />
<Compile Include="Filters\IOrderedFilter.cs" />
<Compile Include="Filters\IResultFilter.cs" />
<Compile Include="Filters\IServiceFilter.cs" />
<Compile Include="Filters\ITypeFilter.cs" />
<Compile Include="Filters\IFilterFactory.cs" />
<Compile Include="Filters\ResultExecutedContext.cs" />
<Compile Include="Filters\ResultExecutingContext.cs" />
<Compile Include="Filters\ResultExecutionDelegate.cs" />

View File

@ -10,86 +10,6 @@ namespace Microsoft.AspNet.Mvc.Core
private static readonly ResourceManager _resourceManager
= new ResourceManager("Microsoft.AspNet.Mvc.Core.Resources", typeof(Resources).GetTypeInfo().Assembly);
/// <summary>
/// The provided identity of type '{0}' is marked IsAuthenticated = true but does not have a value for Name. By default, the anti-forgery system requires that all authenticated identities have a unique Name. If it is not possible to provide a unique Name for this identity, consider extending IAdditionalDataProvider by overriding the DefaultAdditionalDataProvider or a custom type that can provide some form of unique identifier for the current user.
/// </summary>
internal static string TokenValidator_AuthenticatedUserWithoutUsername
{
get { return GetString("TokenValidator_AuthenticatedUserWithoutUsername"); }
}
/// <summary>
/// The provided identity of type '{0}' is marked IsAuthenticated = true but does not have a value for Name. By default, the anti-forgery system requires that all authenticated identities have a unique Name. If it is not possible to provide a unique Name for this identity, consider extending IAdditionalDataProvider by overriding the DefaultAdditionalDataProvider or a custom type that can provide some form of unique identifier for the current user.
/// </summary>
internal static string FormatTokenValidator_AuthenticatedUserWithoutUsername(object p0)
{
return string.Format(CultureInfo.CurrentCulture, GetString("TokenValidator_AuthenticatedUserWithoutUsername"), p0);
}
/// <summary>
/// A claim of type '{0}' was not present on the provided ClaimsIdentity.
/// </summary>
internal static string ClaimUidExtractor_ClaimNotPresent
{
get { return GetString("ClaimUidExtractor_ClaimNotPresent"); }
}
/// <summary>
/// A claim of type '{0}' was not present on the provided ClaimsIdentity.
/// </summary>
internal static string FormatClaimUidExtractor_ClaimNotPresent(object p0)
{
return string.Format(CultureInfo.CurrentCulture, GetString("ClaimUidExtractor_ClaimNotPresent"), p0);
}
/// <summary>
/// A claim of type 'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier' or 'http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider' was not present on the provided ClaimsIdentity. To enable anti-forgery token support with claims-based authentication, please verify that the configured claims provider is providing both of these claims on the ClaimsIdentity instances it generates. If the configured claims provider instead uses a different claim type as a unique identifier, it can be configured by setting the static property AntiForgeryConfig.UniqueClaimTypeIdentifier.
/// </summary>
internal static string ClaimUidExtractor_DefaultClaimsNotPresent
{
get { return GetString("ClaimUidExtractor_DefaultClaimsNotPresent"); }
}
/// <summary>
/// A claim of type 'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier' or 'http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider' was not present on the provided ClaimsIdentity. To enable anti-forgery token support with claims-based authentication, please verify that the configured claims provider is providing both of these claims on the ClaimsIdentity instances it generates. If the configured claims provider instead uses a different claim type as a unique identifier, it can be configured by setting the static property AntiForgeryConfig.UniqueClaimTypeIdentifier.
/// </summary>
internal static string FormatClaimUidExtractor_DefaultClaimsNotPresent()
{
return GetString("ClaimUidExtractor_DefaultClaimsNotPresent");
}
/// <summary>
/// The method '{0}' on type '{1}' returned an instance of '{2}'. Make sure to call Unwrap on the returned value to avoid unobserved faulted Task.
/// </summary>
internal static string ActionExecutor_WrappedTaskInstance
{
get { return GetString("ActionExecutor_WrappedTaskInstance"); }
}
/// <summary>
/// The method '{0}' on type '{1}' returned an instance of '{2}'. Make sure to call Unwrap on the returned value to avoid unobserved faulted Task.
/// </summary>
internal static string FormatActionExecutor_WrappedTaskInstance(object p0, object p1, object p2)
{
return string.Format(CultureInfo.CurrentCulture, GetString("ActionExecutor_WrappedTaskInstance"), p0, p1, p2);
}
/// <summary>
/// The method '{0}' on type '{1}' returned a Task instance even though it is not an asynchronous method.
/// </summary>
internal static string ActionExecutor_UnexpectedTaskInstance
{
get { return GetString("ActionExecutor_UnexpectedTaskInstance"); }
}
/// <summary>
/// The method '{0}' on type '{1}' returned a Task instance even though it is not an asynchronous method.
/// </summary>
internal static string FormatActionExecutor_UnexpectedTaskInstance(object p0, object p1)
{
return string.Format(CultureInfo.CurrentCulture, GetString("ActionExecutor_UnexpectedTaskInstance"), p0, p1);
}
/// <summary>
/// The provided anti-forgery token failed a custom data check.
/// </summary>
@ -234,6 +154,70 @@ namespace Microsoft.AspNet.Mvc.Core
return GetString("AntiForgeryWorker_RequireSSL");
}
/// <summary>
/// The method '{0}' on type '{1}' returned an instance of '{2}'. Make sure to call Unwrap on the returned value to avoid unobserved faulted Task.
/// </summary>
internal static string ActionExecutor_WrappedTaskInstance
{
get { return GetString("ActionExecutor_WrappedTaskInstance"); }
}
/// <summary>
/// The method '{0}' on type '{1}' returned an instance of '{2}'. Make sure to call Unwrap on the returned value to avoid unobserved faulted Task.
/// </summary>
internal static string FormatActionExecutor_WrappedTaskInstance(object p0, object p1, object p2)
{
return string.Format(CultureInfo.CurrentCulture, GetString("ActionExecutor_WrappedTaskInstance"), p0, p1, p2);
}
/// <summary>
/// The method '{0}' on type '{1}' returned a Task instance even though it is not an asynchronous method.
/// </summary>
internal static string ActionExecutor_UnexpectedTaskInstance
{
get { return GetString("ActionExecutor_UnexpectedTaskInstance"); }
}
/// <summary>
/// The method '{0}' on type '{1}' returned a Task instance even though it is not an asynchronous method.
/// </summary>
internal static string FormatActionExecutor_UnexpectedTaskInstance(object p0, object p1)
{
return string.Format(CultureInfo.CurrentCulture, GetString("ActionExecutor_UnexpectedTaskInstance"), p0, p1);
}
/// <summary>
/// A claim of type '{0}' was not present on the provided ClaimsIdentity.
/// </summary>
internal static string ClaimUidExtractor_ClaimNotPresent
{
get { return GetString("ClaimUidExtractor_ClaimNotPresent"); }
}
/// <summary>
/// A claim of type '{0}' was not present on the provided ClaimsIdentity.
/// </summary>
internal static string FormatClaimUidExtractor_ClaimNotPresent(object p0)
{
return string.Format(CultureInfo.CurrentCulture, GetString("ClaimUidExtractor_ClaimNotPresent"), p0);
}
/// <summary>
/// The provided identity of type '{0}' is marked IsAuthenticated = true but does not have a value for Name. By default, the anti-forgery system requires that all authenticated identities have a unique Name. If it is not possible to provide a unique Name for this identity, consider extending IAdditionalDataProvider by overriding the DefaultAdditionalDataProvider or a custom type that can provide some form of unique identifier for the current user.
/// </summary>
internal static string TokenValidator_AuthenticatedUserWithoutUsername
{
get { return GetString("TokenValidator_AuthenticatedUserWithoutUsername"); }
}
/// <summary>
/// The provided identity of type '{0}' is marked IsAuthenticated = true but does not have a value for Name. By default, the anti-forgery system requires that all authenticated identities have a unique Name. If it is not possible to provide a unique Name for this identity, consider extending IAdditionalDataProvider by overriding the DefaultAdditionalDataProvider or a custom type that can provide some form of unique identifier for the current user.
/// </summary>
internal static string FormatTokenValidator_AuthenticatedUserWithoutUsername(object p0)
{
return string.Format(CultureInfo.CurrentCulture, GetString("TokenValidator_AuthenticatedUserWithoutUsername"), p0);
}
/// <summary>
/// The class ReflectedActionFilterEndPoint only supports ReflectedActionDescriptors.
/// </summary>
@ -475,19 +459,19 @@ namespace Microsoft.AspNet.Mvc.Core
}
/// <summary>
/// The '{0}' must return a non-null '{1}'.
/// The '{0}' method of type '{1}' cannot return a null value.
/// </summary>
internal static string MethodMustReturnNotNullValue
internal static string TypeMethodMustReturnNotNullValue
{
get { return GetString("MethodMustReturnNotNullValue"); }
get { return GetString("TypeMethodMustReturnNotNullValue"); }
}
/// <summary>
/// The '{0}' must return a non-null '{1}'.
/// The '{0}' method of type '{1}' cannot return a null value.
/// </summary>
internal static string FormatMethodMustReturnNotNullValue(object p0, object p1)
internal static string FormatTypeMethodMustReturnNotNullValue(object p0, object p1)
{
return string.Format(CultureInfo.CurrentCulture, GetString("MethodMustReturnNotNullValue"), p0, p1);
return string.Format(CultureInfo.CurrentCulture, GetString("TypeMethodMustReturnNotNullValue"), p0, p1);
}
/// <summary>
@ -922,6 +906,21 @@ namespace Microsoft.AspNet.Mvc.Core
return GetString("HtmlHelper_TextAreaParameterOutOfRange");
}
/// The type provided to '{0}' must implement '{1}'.
/// </summary>
internal static string FilterFactoryAttribute_TypeMustImplementIFilter
{
get { return GetString("FilterFactoryAttribute_TypeMustImplementIFilter"); }
}
/// <summary>
/// The type provided to '{0}' must implement '{1}'.
/// </summary>
internal static string FormatFilterFactoryAttribute_TypeMustImplementIFilter(object p0, object p1)
{
return string.Format(CultureInfo.CurrentCulture, GetString("FilterFactoryAttribute_TypeMustImplementIFilter"), p0, p1);
}
private static string GetString(string name, params string[] formatterNames)
{
var value = _resourceManager.GetString(name);

View File

@ -150,9 +150,6 @@
<data name="ActionExecutor_UnexpectedTaskInstance" xml:space="preserve">
<value>The method '{0}' on type '{1}' returned a Task instance even though it is not an asynchronous method.</value>
</data>
<!--<data name="ClaimUidExtractor_DefaultClaimsNotPresent" xml:space="preserve">
<value>A claim of type 'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier' was not present on the provided ClaimsIdentity. To enable anti-forgery token support with claims-based authentication, please verify that the configured claims provider is providing both of these claims on the ClaimsIdentity instances it generates. If the configured claims provider instead uses a different claim type as a unique identifier, it can be configured by setting the static property AntiForgeryConfig.UniqueClaimTypeIdentifier.</value>
</data>-->
<data name="ClaimUidExtractor_ClaimNotPresent" xml:space="preserve">
<value>A claim of type '{0}' was not present on the provided ClaimsIdentity.</value>
</data>
@ -204,8 +201,8 @@
<data name="PropertyOfTypeCannotBeNull" xml:space="preserve">
<value>The '{0}' property of '{1}' must not be null.</value>
</data>
<data name="MethodMustReturnNotNullValue" xml:space="preserve">
<value>The '{0}' must return a non-null '{1}'.</value>
<data name="TypeMethodMustReturnNotNullValue" xml:space="preserve">
<value>The '{0}' method of type '{1}' cannot return a null value.</value>
</data>
<data name="ActionSelector_GetCandidateActionsIsAmbiguous" xml:space="preserve">
<value>The supplied route values are ambiguous and can select multiple sets of actions.</value>
@ -288,4 +285,7 @@
<data name="HtmlHelper_TextAreaParameterOutOfRange" xml:space="preserve">
<value>The value must be greater than or equal to zero.</value>
</data>
</root>
<data name="FilterFactoryAttribute_TypeMustImplementIFilter" xml:space="preserve">
<value>The type provided to '{0}' must implement '{1}'.</value>
</data>
</root>

View File

@ -63,7 +63,7 @@ namespace Microsoft.AspNet.Mvc.Razor.Host
var value = _resourceManager.GetString(name);
System.Diagnostics.Debug.Assert(value != null);
if (formatterNames != null)
{
for (var i = 0; i < formatterNames.Length; i++)

View File

@ -31,7 +31,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Test
var value = _resourceManager.GetString(name);
System.Diagnostics.Debug.Assert(value != null);
if (formatterNames != null)
{
for (var i = 0; i < formatterNames.Length; i++)