@@ -16,14 +16,13 @@ public class HttpModelHandler : IModelHandler
1616 {
1717 private readonly IWebContext _context ;
1818
19+ private object ? _model ;
20+
1921 /// <summary>
2022 /// Initializes a new instance of the <see cref="HttpModelHandler"/> class.
2123 /// </summary>
2224 /// <param name="context">The context.</param>
23- public HttpModelHandler ( IWebContext context )
24- {
25- _context = context ;
26- }
25+ public HttpModelHandler ( IWebContext context ) => _context = context ;
2726
2827 /// <summary>
2928 /// Gets the model binders types.
@@ -54,33 +53,34 @@ public HttpModelHandler(IWebContext context)
5453 typeof ( ValidationAttributesExecutor )
5554 } ;
5655
56+ /// <summary>
57+ /// Gets a value indicating whether model has been processed (parsed/validated).
58+ /// </summary>
59+ public bool Processed { get ; private set ; }
60+
5761 /// <summary>
5862 /// Adds the model binder into model binders list, this type should be registered in Simplify.DI container.
5963 /// </summary>
6064 /// <typeparam name="T"></typeparam>
6165 public static void RegisterModelBinder < T > ( )
62- where T : IModelBinder
63- {
66+ where T : IModelBinder =>
6467 ModelBindersTypes . Add ( typeof ( T ) ) ;
65- }
6668
6769 /// <summary>
6870 /// Adds the model validator into model validators list, this type should be registered in Simplify.DI container.
6971 /// </summary>
7072 /// <typeparam name="T"></typeparam>
7173 public static void RegisterModelValidator < T > ( )
72- where T : IModelValidator
73- {
74+ where T : IModelValidator =>
7475 ModelValidatorsTypes . Add ( typeof ( T ) ) ;
75- }
7676
7777 /// <summary>
7878 /// Parses model and validates it asynchronously
7979 /// </summary>
8080 /// <typeparam name="T">Model type</typeparam>
8181 /// <param name="resolver">The resolver.</param>
8282 /// <returns></returns>
83- public async Task < T > ProcessAsync < T > ( IDIResolver resolver )
83+ public async Task ProcessAsync < T > ( IDIResolver resolver )
8484 {
8585 var args = new ModelBinderEventArgs < T > ( _context ) ;
8686
@@ -93,12 +93,29 @@ public async Task<T> ProcessAsync<T>(IDIResolver resolver)
9393
9494 Validate ( args , resolver ) ;
9595
96- return args . Model ;
96+ _model = args . Model ;
97+ Processed = true ;
98+
99+ return ;
97100 }
98101
99102 throw new ModelBindingException ( $ "Unrecognized request content type for binding: { _context . Request . ContentType } ") ;
100103 }
101104
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+
102119 private static void Validate < T > ( ModelBinderEventArgs < T > args , IDIResolver resolver )
103120 {
104121 foreach ( var validator in ModelValidatorsTypes . Select ( x => ( IModelValidator ) resolver . Resolve ( x ) ) )
0 commit comments