-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add StronglyTyped, LambdaExtensions and JsonModelBinder
- Loading branch information
Showing
5 changed files
with
141 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System.Linq.Expressions; | ||
|
||
namespace DNTCommon.Web.Core; | ||
|
||
/// <summary> | ||
/// Expression Helpers | ||
/// </summary> | ||
public static class LambdaExtensions | ||
{ | ||
/// <summary> | ||
/// Get the actual return type of expr | ||
/// </summary> | ||
public static Type? GetObjectType<TEntity>(this Expression<Func<TEntity, object?>>? expression) | ||
{ | ||
if (expression is null) | ||
{ | ||
return null; | ||
} | ||
|
||
return expression.Body.NodeType is ExpressionType.Convert or ExpressionType.ConvertChecked && | ||
expression.Body is UnaryExpression unary | ||
? unary.Operand.Type | ||
: expression.Body.Type; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using System.Linq.Expressions; | ||
|
||
namespace DNTCommon.Web.Core; | ||
|
||
/// <summary> | ||
/// Get the string name and type of property or field. | ||
/// </summary> | ||
public class StronglyTyped : ExpressionVisitor | ||
{ | ||
private Stack<string>? _stack; | ||
|
||
/// <summary> | ||
/// Get the string name and type of property or field. | ||
/// </summary> | ||
public static string PropertyPath<TEntity>(Expression<Func<TEntity, object?>>? expression, string separator) | ||
=> expression is null ? string.Empty : new StronglyTyped().Name(expression, separator); | ||
|
||
/// <summary> | ||
/// Get the string name and type of property or field. | ||
/// </summary> | ||
public string Path(Expression expression, string separator) | ||
{ | ||
_stack = new Stack<string>(); | ||
Visit(expression); | ||
|
||
return _stack.Aggregate((s1, s2) => $"{s1}{separator}{s2}"); | ||
} | ||
|
||
/// <summary> | ||
/// Visits the children of the System.Linq.Expressions.MemberExpression. | ||
/// </summary> | ||
protected override Expression VisitMember(MemberExpression node) | ||
{ | ||
ArgumentNullException.ThrowIfNull(node); | ||
|
||
_stack?.Push(node.Member.Name); | ||
|
||
return base.VisitMember(node); | ||
} | ||
|
||
/// <summary> | ||
/// Get the string name and type of property or field. | ||
/// </summary> | ||
public string Name<TEntity>(Expression<Func<TEntity, object?>> expression, string separator) | ||
=> Path(expression, separator); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using System.Text.Json; | ||
using Microsoft.AspNetCore.Mvc.ModelBinding; | ||
|
||
namespace DNTCommon.Web.Core; | ||
|
||
/// <summary> | ||
/// Json ModelBinder | ||
/// </summary> | ||
public class JsonModelBinder : IModelBinder | ||
{ | ||
/// <summary> | ||
/// Attempts to bind a model. | ||
/// </summary> | ||
public Task BindModelAsync(ModelBindingContext bindingContext) | ||
{ | ||
ArgumentNullException.ThrowIfNull(bindingContext); | ||
|
||
var valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName); | ||
|
||
if (valueProviderResult != ValueProviderResult.None) | ||
{ | ||
bindingContext.ModelState.SetModelValue(bindingContext.ModelName, valueProviderResult); | ||
|
||
var valueAsString = valueProviderResult.FirstValue; | ||
|
||
if (string.IsNullOrWhiteSpace(valueAsString)) | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
|
||
var result = JsonSerializer.Deserialize(valueAsString, bindingContext.ModelType); | ||
|
||
if (result is not null) | ||
{ | ||
bindingContext.Result = ModelBindingResult.Success(result); | ||
|
||
return Task.CompletedTask; | ||
} | ||
} | ||
|
||
return Task.CompletedTask; | ||
} | ||
} |