-
Notifications
You must be signed in to change notification settings - Fork 97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactored Action Filters #261
Merged
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4a33a21
Refactored Action Filters
exyi 9d94100
fixed little issues in DotvvmRoutingMiddleware
exyi de391c8
binding expression's ActionFilters changed to IActionFilter
exyi 531d070
AuthorizeAttribute and AuthorizeFilterAttribute merge on Asp.NET Core…
exyi e4e1ee3
added missing [NotAuthorized] to DotvvmPresenter
exyi ac2ba6b
Fixed AuthorizedAttribute and added simple sample for authorized IDot…
exyi 071f7f4
Added missing inheritance
tomasherceg e45b741
Added missing inheritance
tomasherceg File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
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 |
---|---|---|
@@ -1,6 +1,8 @@ | ||
using System; | ||
using DotVVM.Framework.Runtime.Filters; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Threading.Tasks; | ||
|
||
namespace DotVVM.Framework.Hosting.Middlewares | ||
|
@@ -61,14 +63,28 @@ public async Task<bool> Handle(IDotvvmRequestContext context) | |
context.Route = route; | ||
context.Parameters = parameters; | ||
|
||
var presenter = route.GetPresenter(); | ||
var filters = ActionFilterHelper.GetActionFilters<IRequestActionFilter>(presenter.GetType().GetTypeInfo()); | ||
filters.AddRange(context.Configuration.Runtime.GlobalFilters.OfType<IRequestActionFilter>()); | ||
try | ||
{ | ||
await route.ProcessRequest(context); | ||
foreach (var f in filters) await f.OnPageLoadingAsync(context); | ||
await presenter.ProcessRequest(context); | ||
foreach (var f in filters) await f.OnPageLoadedAsync(context); | ||
} | ||
catch (DotvvmInterruptRequestExecutionException) | ||
{ | ||
// the response has already been generated, do nothing | ||
} | ||
catch(Exception exception) | ||
{ | ||
foreach (var f in filters) | ||
{ | ||
await f.OnPageExceptionAsync(context, exception); | ||
if (context.IsCommandExceptionHandled) context.InterruptRequest(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't we check |
||
} | ||
throw; | ||
} | ||
return true; | ||
} | ||
} | ||
|
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
33 changes: 27 additions & 6 deletions
33
src/DotVVM.Framework/Runtime/Filters/ActionFilterAttribute.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,43 +1,64 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using DotVVM.Framework.Hosting; | ||
using DotVVM.Framework.Utils; | ||
|
||
namespace DotVVM.Framework.Runtime.Filters | ||
{ | ||
/// <summary> | ||
/// Allows to add custom logic before and after a command is executed on a ViewModel. | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = true)] | ||
public abstract class ActionFilterAttribute : Attribute | ||
public abstract class ActionFilterAttribute : Attribute, IRequestActionFilter, ICommandActionFilter, IViewModelActionFilter | ||
{ | ||
/// <summary> | ||
/// Called after the viewmodel object is created. | ||
/// </summary> | ||
protected internal virtual Task OnViewModelCreatedAsync(IDotvvmRequestContext context) | ||
=> Task.FromResult(0); | ||
=> TaskUtils.GetCompletedTask(); | ||
|
||
/// <summary> | ||
/// Called before the command is executed. | ||
/// </summary> | ||
protected internal virtual Task OnCommandExecutingAsync(IDotvvmRequestContext context, ActionInfo actionInfo) | ||
=> Task.FromResult(0); | ||
=> TaskUtils.GetCompletedTask(); | ||
|
||
/// <summary> | ||
/// Called after the command is executed. | ||
/// </summary> | ||
protected internal virtual Task OnCommandExecutedAsync(IDotvvmRequestContext context, ActionInfo actionInfo, Exception exception) | ||
=> Task.FromResult(0); | ||
=> TaskUtils.GetCompletedTask(); | ||
|
||
/// <summary> | ||
/// Called before the response is rendered. | ||
/// </summary> | ||
protected internal virtual Task OnResponseRenderingAsync(IDotvvmRequestContext context) | ||
=> Task.FromResult(0); | ||
=> TaskUtils.GetCompletedTask(); | ||
|
||
/// <summary> | ||
/// Called when an exception occurs during the processing of the page events. | ||
/// </summary> | ||
protected internal virtual Task OnPageExceptionAsync(IDotvvmRequestContext context, Exception exception) | ||
=> Task.FromResult(0); | ||
=> TaskUtils.GetCompletedTask(); | ||
|
||
/// <summary> | ||
/// Called before page is processed. | ||
/// </summary> | ||
protected internal virtual Task OnPageLoadingAsync(IDotvvmRequestContext context) | ||
=> TaskUtils.GetCompletedTask(); | ||
|
||
/// <summary> | ||
/// Called after page is processed and ready to be sent to client. | ||
/// </summary> | ||
protected internal virtual Task OnPageLoadedAsync(IDotvvmRequestContext context) | ||
=> TaskUtils.GetCompletedTask(); | ||
|
||
Task IRequestActionFilter.OnPageExceptionAsync(IDotvvmRequestContext context, Exception exception) => OnPageExceptionAsync(context, exception); | ||
Task ICommandActionFilter.OnCommandExecutingAsync(IDotvvmRequestContext context, ActionInfo actionInfo) => OnCommandExecutingAsync(context, actionInfo); | ||
Task ICommandActionFilter.OnCommandExecutedAsync(IDotvvmRequestContext context, ActionInfo actionInfo, Exception exception) => OnCommandExecutedAsync(context, actionInfo, exception); | ||
Task IViewModelActionFilter.OnViewModelCreatedAsync(IDotvvmRequestContext context) => OnViewModelCreatedAsync(context); | ||
Task IViewModelActionFilter.OnResponseRenderingAsync(IDotvvmRequestContext context) => OnResponseRenderingAsync(context); | ||
Task IRequestActionFilter.OnPageLoadingAsync(IDotvvmRequestContext context) => OnPageLoadingAsync(context); | ||
Task IRequestActionFilter.OnPageLoadedAsync(IDotvvmRequestContext context) => OnPageLoadedAsync(context); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/DotVVM.Framework/Runtime/Filters/ActionFilterHelper.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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Threading.Tasks; | ||
using DotVVM.Framework.Utils; | ||
|
||
namespace DotVVM.Framework.Runtime.Filters | ||
{ | ||
public static class ActionFilterHelper | ||
{ | ||
public static List<T> GetActionFilters<T>(MemberInfo memberInfo, bool includeParents = true) | ||
{ | ||
var result = new List<T>(); | ||
do | ||
{ | ||
result.AddRange(memberInfo.CastTo<ICustomAttributeProvider>().GetCustomAttributes<T>()); | ||
} while (includeParents && (memberInfo = memberInfo.DeclaringType?.GetTypeInfo()) != null); | ||
return result; | ||
} | ||
} | ||
} |
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,11 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace DotVVM.Framework.Runtime.Filters | ||
{ | ||
public interface IActionFilter | ||
{ | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/DotVVM.Framework/Runtime/Filters/ICommandActionFilter.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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using DotVVM.Framework.Hosting; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace DotVVM.Framework.Runtime.Filters | ||
{ | ||
public interface ICommandActionFilter | ||
{ | ||
/// <summary> | ||
/// Called before the command is executed. | ||
/// </summary> | ||
Task OnCommandExecutingAsync(IDotvvmRequestContext context, ActionInfo actionInfo); | ||
|
||
/// <summary> | ||
/// Called after the command is executed. | ||
/// </summary> | ||
Task OnCommandExecutedAsync(IDotvvmRequestContext context, ActionInfo actionInfo, Exception exception); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/DotVVM.Framework/Runtime/Filters/IRequestActionFilter.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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using DotVVM.Framework.Hosting; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace DotVVM.Framework.Runtime.Filters | ||
{ | ||
public interface IRequestActionFilter: IActionFilter | ||
{ | ||
/// <summary> | ||
/// Called before page is processed. | ||
/// </summary> | ||
Task OnPageLoadingAsync(IDotvvmRequestContext context); | ||
/// <summary> | ||
/// Called after page is processed and ready to be sent to client. | ||
/// </summary> | ||
Task OnPageLoadedAsync(IDotvvmRequestContext context); | ||
/// <summary> | ||
/// Called when an exception occurs during the processing of the page. | ||
/// </summary> | ||
Task OnPageExceptionAsync(IDotvvmRequestContext context, Exception exception); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/DotVVM.Framework/Runtime/Filters/IViewModelActionFilter.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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using DotVVM.Framework.Hosting; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace DotVVM.Framework.Runtime.Filters | ||
{ | ||
public interface IViewModelActionFilter | ||
{ | ||
/// <summary> | ||
/// Called after the viewmodel object is created. | ||
/// </summary> | ||
Task OnViewModelCreatedAsync(IDotvvmRequestContext context); | ||
/// <summary> | ||
/// Called before the response is rendered. | ||
/// </summary> | ||
Task OnResponseRenderingAsync(IDotvvmRequestContext context); | ||
} | ||
} |
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.
The method is now called also for
DotvvmControlException
andDotvvmHttpException
rethrown inDotvvmPresenter
(L66, L221). Is this intended?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.
DotvvmPresenter rethrows only
HttpException
,DotvvmControlException
should be handled like any other. I'll fix it.