This repository was archived by the owner on Dec 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Adding IFilterFactory #345
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
29 changes: 19 additions & 10 deletions
29
samples/MvcSample.Web/Filters/InspectResultPageAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
{ | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using System; | ||
|
||
namespace Microsoft.AspNet.Mvc | ||
{ | ||
public interface IFilterFactory : IFilter | ||
{ | ||
IFilter CreateInstance([NotNull] IServiceProvider serviceProvider); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
20 changes: 18 additions & 2 deletions
20
src/Microsoft.AspNet.Mvc.Core/Filters/ServiceFilterAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,35 @@ | ||
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; | ||
} | ||
|
||
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe filter factory needs order, otherwise you can't apply it globally and get it ordered against other filters
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you need order you can just also implement
IOrderedFilter
. Our two implementations have both interfaces.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As discussed, will open a follow up by for global filters, will include details about the need to include ordering on
IFilterFactory
when that's implemented.Edit: #349 filed