-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
293 additions
and
73 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,12 @@ | ||
using System; | ||
|
||
namespace ODatalizer.EFCore.Converters | ||
{ | ||
public interface ITypeConverter | ||
{ | ||
Type ModelType { get; } | ||
bool CanConvertFrom(Type type); | ||
object Convert(object value); | ||
bool TryParse(string str, out object value); | ||
} | ||
} |
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,60 @@ | ||
using Microsoft.AspNetCore.Mvc.ModelBinding; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace ODatalizer.EFCore.Converters | ||
{ | ||
public class ODatalizerModelBinder : IModelBinder | ||
{ | ||
private readonly IEnumerable<ITypeConverter> _typeConverters; | ||
|
||
public ODatalizerModelBinder(IEnumerable<ITypeConverter> typeConverters) | ||
{ | ||
_typeConverters = typeConverters; | ||
} | ||
|
||
public Task BindModelAsync(ModelBindingContext bindingContext) | ||
{ | ||
if (bindingContext == null) | ||
{ | ||
throw new ArgumentNullException(nameof(bindingContext)); | ||
} | ||
var modelName = bindingContext.ModelName; | ||
|
||
var valueProviderResult = bindingContext.ValueProvider.GetValue(modelName); | ||
|
||
if (valueProviderResult == ValueProviderResult.None) | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
|
||
bindingContext.ModelState.SetModelValue(modelName, valueProviderResult); | ||
|
||
var value = valueProviderResult.FirstValue; | ||
|
||
if (string.IsNullOrEmpty(value)) | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
|
||
var converter = _typeConverters.FirstOrDefault(o => o.ModelType == bindingContext.ModelType); | ||
|
||
if (converter == null) | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
|
||
if (converter.TryParse(value, out var result)) | ||
{ | ||
bindingContext.Result = ModelBindingResult.Success(result); | ||
return Task.CompletedTask; | ||
} | ||
|
||
bindingContext.ModelState.TryAddModelError(modelName, "Invalid DateTime format."); | ||
|
||
return Task.CompletedTask; | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/ODatalizer.EFCore/Converters/ODatalizerModelBinderProvider.cs
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,32 @@ | ||
using Microsoft.AspNetCore.Mvc.ModelBinding; | ||
using Microsoft.AspNetCore.Mvc.ModelBinding.Binders; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace ODatalizer.EFCore.Converters | ||
{ | ||
public class ODatalizerModelBinderProvider : IModelBinderProvider | ||
{ | ||
public IModelBinder GetBinder(ModelBinderProviderContext context) | ||
{ | ||
if (context == null) | ||
{ | ||
throw new ArgumentNullException(nameof(context)); | ||
} | ||
|
||
var typeConverters = context.Services.GetService<IEnumerable<ITypeConverter>>(); | ||
|
||
if (typeConverters != null) | ||
{ | ||
if (typeConverters.Any(o => context.Metadata.ModelType == o.ModelType)) | ||
{ | ||
return new BinderTypeModelBinder(typeof(ODatalizerModelBinder)); | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.