Skip to content

Commit 7ea65f3

Browse files
committed
Fixes #145
[R] Expresion-body methods
1 parent ca29b17 commit 7ea65f3

File tree

5 files changed

+54
-19
lines changed

5 files changed

+54
-19
lines changed

src/Simplify.Web/AsyncController{T}.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,12 @@ public virtual T Model
3838
/// </summary>
3939
public virtual async Task ReadModelAsync()
4040
{
41-
_model = await Resolver.Resolve<IModelHandler>().ProcessAsync<T>(Resolver);
41+
var handler = Resolver.Resolve<IModelHandler>();
42+
43+
if (!handler.Processed)
44+
await handler.ProcessAsync<T>(Resolver);
45+
46+
_model = handler.GetModel<T>();
4247
}
4348
}
4449
}

src/Simplify.Web/Controller{T}.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,12 @@ public virtual T Model
3838
/// </summary>
3939
public virtual async Task ReadModelAsync()
4040
{
41-
_model = await Resolver.Resolve<IModelHandler>().ProcessAsync<T>(Resolver);
41+
var handler = Resolver.Resolve<IModelHandler>();
42+
43+
if (!handler.Processed)
44+
await handler.ProcessAsync<T>(Resolver);
45+
46+
_model = handler.GetModel<T>();
4247
}
4348
}
4449
}

src/Simplify.Web/Model/Binding/ModelBinderEventArgs.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@ public class ModelBinderEventArgs<T> : EventArgs
1414
/// Initializes a new instance of the <see cref="ModelBinderEventArgs{T}"/> class.
1515
/// </summary>
1616
/// <param name="context">The context.</param>
17-
public ModelBinderEventArgs(IWebContext context)
18-
{
19-
Context = context;
20-
}
17+
public ModelBinderEventArgs(IWebContext context) => Context = context;
2118

2219
/// <summary>
2320
/// Gets the model.

src/Simplify.Web/Model/HttpModelHandler.cs

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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&lt;T&gt; 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)))

src/Simplify.Web/Model/IModelHandler.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,23 @@ namespace Simplify.Web.Model
88
/// </summary>
99
public interface IModelHandler
1010
{
11+
/// <summary>
12+
/// Gets a value indicating whether model has been processed (parsed/validated).
13+
/// </summary>
14+
bool Processed { get; }
15+
1116
/// <summary>
1217
/// Parses model and validates it asynchronously
1318
/// </summary>
1419
/// <typeparam name="T">Model type</typeparam>
1520
/// <param name="resolver">The resolver.</param>
1621
/// <returns></returns>
17-
Task<T> ProcessAsync<T>(IDIResolver resolver);
22+
Task ProcessAsync<T>(IDIResolver resolver);
23+
24+
/// <summary>
25+
/// Gets the model.
26+
/// </summary>
27+
/// <typeparam name="T">The model</typeparam>
28+
T GetModel<T>();
1829
}
1930
}

0 commit comments

Comments
 (0)