-
Notifications
You must be signed in to change notification settings - Fork 0
/
ActionShell.cs
114 lines (96 loc) · 8.11 KB
/
ActionShell.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*————————————————————————————————————————————————————————————————————————————
——————————————————————————————————————————————
| ActionShell : Endpoint entry exit. |
——————————————————————————————————————————————
© Copyright 2023 İhsan Volkan Töre.
Author : IVT. (İhsan Volkan Töre)
Version : 202304111012
License : MIT.
History :
202304111012: IVT : Upgraded to .NET 7.0 with nullable enabled.
202201070900: IVT : added.
————————————————————————————————————————————————————————————————————————————*/
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
namespace Tore.Service {
/**———————————————————————————————————————————————————————————————————————————
CLASS: ActionShell. <summary>
USAGE: <br/>
Builds a request scoped context data. <br/>
enter delegate is called before entry to endpoint. <br/>
leave delegate is called after endpoint is run. <br/>
The request scope is passed to these delegates and <br/>
since it is also stored into HttpContext, it can be <br/>
obtained from any place where HttpContext is accessible.</summary>
————————————————————————————————————————————————————————————————————————————*/
public class ActionShell : ActionFilterAttribute, IActionFilter {
private static Type _requestScopeType = typeof(RequestScopeBase); // Default.
/**———————————————————————————————————————————————————————————————————————————
TYPE: ActionShellDelegate <summary>
TASK: Method delegate type to call before and after endpoint. <br/>
ARGS: scope: RequestScopeBase : Current request scope instance. </summary>
————————————————————————————————————————————————————————————————————————————*/
public delegate void ActionShellDelegate(RequestScopeBase? scope);
/**———————————————————————————————————————————————————————————————————————————
PROP: enter. <summary>
TASK: Method delegate to call before entry to endpoint. <br/>
Method delegate type must be ActionShellDelegate. <br/>
The method should be static. </summary>
————————————————————————————————————————————————————————————————————————————*/
public static ActionShellDelegate? enter {get; set;}
/**———————————————————————————————————————————————————————————————————————————
PROP: leave. <summary>
TASK: Method delegate to call after leaving the endpoint. <br/>
Method delegate type must be ActioShellDelegate. <br/>
The method should be static. </summary>
————————————————————————————————————————————————————————————————————————————*/
public static ActionShellDelegate? leave {get; set;}
/**———————————————————————————————————————————————————————————————————————————
PROP: requestScopeType. <summary>
TASK: This must be a descendant of class RequestScopeBase. <br/>
This allows developers to define a class that gathers <br/>
data tailored for the project from the context. <br/>
Instance will be created by OnActionExecuting, <br/>
Just before ActionShell.enter is called. <para/>
This is an assignment because attribute classes does not<br/>
allow generics. </summary>
————————————————————————————————————————————————————————————————————————————*/
public static Type requestScopeType {
get => _requestScopeType;
set {
if (value == null)
throw new ArgumentNullException("requestScopeType.");
if (!typeof(RequestScopeBase).IsAssignableFrom(value))
throw new ArgumentException("Type Mismatch.");
_requestScopeType = value;
}
}
/**———————————————————————————————————————————————————————————————————————————
FUNC: fetchScope <summary>
TASK: Accesses to scope data stored in HttpContext. <br/>
ARGS: httpContext : HttpContext : Current http context. <br/>
RETV: : RequestScopeBase : Scope if any or null. </summary>
————————————————————————————————————————————————————————————————————————————*/
public static RequestScopeBase? fetchScope(HttpContext httpContext){
if (!httpContext.Items.ContainsKey("RequestScope"))
return null;
return (RequestScopeBase?) httpContext.Items["RequestScope"] ?? null;
}
/**<inheritdoc/>*/
public override void OnActionExecuting(ActionExecutingContext context) {
RequestScopeBase? scope;
scope = (RequestScopeBase?)
Activator.CreateInstance(requestScopeType, new object[]{context});
context.HttpContext.Items.Add("RequestScope", scope);
enter?.Invoke(scope);
}
/**<inheritdoc/>*/
public override void OnActionExecuted(ActionExecutedContext context) {
RequestScopeBase? scope = fetchScope(context.HttpContext);
leave?.Invoke(scope);
if (scope != null)
scope.leftAction = true;
}
} // End class ActionShell.
} // End namespace.