@@ -16,14 +16,13 @@ public class HttpModelHandler : IModelHandler
16
16
{
17
17
private readonly IWebContext _context ;
18
18
19
+ private object ? _model ;
20
+
19
21
/// <summary>
20
22
/// Initializes a new instance of the <see cref="HttpModelHandler"/> class.
21
23
/// </summary>
22
24
/// <param name="context">The context.</param>
23
- public HttpModelHandler ( IWebContext context )
24
- {
25
- _context = context ;
26
- }
25
+ public HttpModelHandler ( IWebContext context ) => _context = context ;
27
26
28
27
/// <summary>
29
28
/// Gets the model binders types.
@@ -54,33 +53,34 @@ public HttpModelHandler(IWebContext context)
54
53
typeof ( ValidationAttributesExecutor )
55
54
} ;
56
55
56
+ /// <summary>
57
+ /// Gets a value indicating whether model has been processed (parsed/validated).
58
+ /// </summary>
59
+ public bool Processed { get ; private set ; }
60
+
57
61
/// <summary>
58
62
/// Adds the model binder into model binders list, this type should be registered in Simplify.DI container.
59
63
/// </summary>
60
64
/// <typeparam name="T"></typeparam>
61
65
public static void RegisterModelBinder < T > ( )
62
- where T : IModelBinder
63
- {
66
+ where T : IModelBinder =>
64
67
ModelBindersTypes . Add ( typeof ( T ) ) ;
65
- }
66
68
67
69
/// <summary>
68
70
/// Adds the model validator into model validators list, this type should be registered in Simplify.DI container.
69
71
/// </summary>
70
72
/// <typeparam name="T"></typeparam>
71
73
public static void RegisterModelValidator < T > ( )
72
- where T : IModelValidator
73
- {
74
+ where T : IModelValidator =>
74
75
ModelValidatorsTypes . Add ( typeof ( T ) ) ;
75
- }
76
76
77
77
/// <summary>
78
78
/// Parses model and validates it asynchronously
79
79
/// </summary>
80
80
/// <typeparam name="T">Model type</typeparam>
81
81
/// <param name="resolver">The resolver.</param>
82
82
/// <returns></returns>
83
- public async Task < T > ProcessAsync < T > ( IDIResolver resolver )
83
+ public async Task ProcessAsync < T > ( IDIResolver resolver )
84
84
{
85
85
var args = new ModelBinderEventArgs < T > ( _context ) ;
86
86
@@ -93,12 +93,29 @@ public async Task<T> ProcessAsync<T>(IDIResolver resolver)
93
93
94
94
Validate ( args , resolver ) ;
95
95
96
- return args . Model ;
96
+ _model = args . Model ;
97
+ Processed = true ;
98
+
99
+ return ;
97
100
}
98
101
99
102
throw new ModelBindingException ( $ "Unrecognized request content type for binding: { _context . Request . ContentType } ") ;
100
103
}
101
104
105
+ /// <summary>
106
+ /// Gets the model.
107
+ /// </summary>
108
+ /// <typeparam name="T">The model</typeparam>
109
+ /// <returns></returns>
110
+ /// <exception cref="InvalidOperationException">Error getting model, model should be processed via ProcessAsync<T> method first</exception>
111
+ public T GetModel < T > ( )
112
+ {
113
+ if ( _model == null )
114
+ throw new InvalidOperationException ( "Error getting model, model should be processed via ProcessAsync<T> method first" ) ;
115
+
116
+ return ( T ) _model ;
117
+ }
118
+
102
119
private static void Validate < T > ( ModelBinderEventArgs < T > args , IDIResolver resolver )
103
120
{
104
121
foreach ( var validator in ModelValidatorsTypes . Select ( x => ( IModelValidator ) resolver . Resolve ( x ) ) )
0 commit comments