-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathHttpModelHandler.cs
More file actions
114 lines (95 loc) · 3.24 KB
/
HttpModelHandler.cs
File metadata and controls
114 lines (95 loc) · 3.24 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Simplify.DI;
using Simplify.Web.Model.Binding;
using Simplify.Web.Model.Binding.Binders;
using Simplify.Web.Model.Validation;
using Simplify.Web.Modules.Context;
namespace Simplify.Web.Model;
/// <summary>
/// Provides the model handling.
/// </summary>
/// <seealso cref="Simplify.Web.Model.IModelHandler" />
/// <remarks>
/// Initializes a new instance of the <see cref="HttpModelHandler" /> class.
/// </remarks>
/// <seealso cref="IModelHandler" />
/// <param name="resolver">The resolver.</param>
/// <param name="context">The context.</param>
public class HttpModelHandler(IDIResolver resolver, IWebContext context) : IModelHandler
{
private object? _model;
/// <summary>
/// Gets the model binders types.
/// </summary>
public static IList<Type> ModelBindersTypes { get; } =
[
// Default model binders
typeof(JsonModelBinder),
typeof(HttpQueryModelBinder),
typeof(HttpFormModelBinder)
];
/// <summary>
/// Gets the model validators types.
/// </summary>
public static IList<Type> ModelValidatorsTypes { get; } =
[
// Default model validators
typeof (ValidationAttributesExecutor)
];
/// <summary>
/// Gets a value indicating whether model has been processed (parsed/validated).
/// </summary>
public bool Processed { get; private set; }
/// <summary>
/// Adds the model binder into model binders list, this type should be registered in Simplify.DI container.
/// </summary>
/// <typeparam name="T">The model type.</typeparam>
public static void RegisterModelBinder<T>()
where T : IModelBinder =>
ModelBindersTypes.Add(typeof(T));
/// <summary>
/// Adds the model validator into model validators list, this type should be registered in Simplify.DI container.
/// </summary>
/// <typeparam name="T">The model type.</typeparam>
public static void RegisterModelValidator<T>()
where T : IModelValidator =>
ModelValidatorsTypes.Add(typeof(T));
/// <summary>
/// Parses model and validates it asynchronously
/// </summary>
/// <typeparam name="T">The model type.</typeparam>
public async Task ProcessAsync<T>()
{
var args = new ModelBinderEventArgs<T>(context);
foreach (var binder in ModelBindersTypes.Select(binderType => (IModelBinder)resolver.Resolve(binderType)))
{
await binder.BindAsync(args);
if (!args.IsBound)
continue;
Validate(args, resolver);
_model = args.Model;
Processed = true;
return;
}
throw new ModelBindingException($"Unrecognized request content type for binding: {context.Request.ContentType}");
}
/// <summary>
/// Gets the model.
/// </summary>
/// <typeparam name="T">The model type.</typeparam>
/// <exception cref="InvalidOperationException">Error getting model, model should be processed via ProcessAsync<T> method first</exception>
public T GetModel<T>()
{
if (_model == null)
throw new InvalidOperationException("Error getting model, model should be processed via ProcessAsync<T> method first");
return (T)_model;
}
private static void Validate<T>(ModelBinderEventArgs<T> args, IDIResolver resolver)
{
foreach (var validator in ModelValidatorsTypes.Select(x => (IModelValidator)resolver.Resolve(x)))
validator.Validate(args.Model, resolver);
}
}