-
-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Co-authored-by: Steve Smith <steve@kentsmiths.com>
- Loading branch information
1 parent
7838112
commit ab7d6a0
Showing
5 changed files
with
143 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net5</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\src\SmartEnum\SmartEnum.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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,20 @@ | ||
using Microsoft.AspNetCore.Mvc.ModelBinding; | ||
using Microsoft.AspNetCore.Mvc.ModelBinding.Binders; | ||
using System; | ||
|
||
namespace Ardalis.SmartEnum.ModelBinding | ||
{ | ||
public class SmartEnumBinderProvider : IModelBinderProvider | ||
{ | ||
public IModelBinder GetBinder(ModelBinderProviderContext context) | ||
{ | ||
if (context == null) | ||
throw new ArgumentNullException(nameof(context)); | ||
|
||
if (TypeUtil.IsDerived(context.Metadata.ModelType, typeof(SmartEnum<,>))) | ||
return new BinderTypeModelBinder(typeof(SmartEnumModelBinder)); | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using Microsoft.AspNetCore.Mvc.ModelBinding; | ||
using System; | ||
using System.Reflection; | ||
using System.Threading.Tasks; | ||
|
||
namespace Ardalis.SmartEnum.ModelBinding | ||
{ | ||
public class SmartEnumModelBinder : IModelBinder | ||
{ | ||
public Task BindModelAsync(ModelBindingContext bindingContext) | ||
{ | ||
if (bindingContext == null) | ||
throw new ArgumentNullException(nameof(bindingContext)); | ||
|
||
Type modelType = bindingContext.ModelMetadata.ModelType; | ||
|
||
if (!TypeUtil.IsDerived(modelType, typeof(SmartEnum<,>))) | ||
throw new ArgumentException($"{modelType} is not a SmartEnum"); | ||
|
||
string propertyName = bindingContext.ModelName; | ||
ValueProviderResult valueProviderResult = bindingContext.ValueProvider.GetValue(propertyName); | ||
if (valueProviderResult == ValueProviderResult.None) | ||
return Task.CompletedTask; | ||
|
||
bindingContext.ModelState.SetModelValue(propertyName, valueProviderResult); | ||
|
||
string enumKeyName = valueProviderResult.FirstValue; | ||
|
||
if (string.IsNullOrEmpty(enumKeyName)) | ||
return Task.CompletedTask; | ||
|
||
// Create smart enum instance from enum key name by calling the FromName static method on the SmartEnum Class | ||
Type baseSmartEnumType = TypeUtil.GetTypeFromGenericType(modelType, typeof(SmartEnum<,>)); | ||
foreach (MethodInfo methodInfo in baseSmartEnumType.GetMethods()) | ||
{ | ||
if (methodInfo.Name == "FromName") | ||
{ | ||
ParameterInfo[] methodsParams = methodInfo.GetParameters(); | ||
if (methodsParams.Length == 2) | ||
{ | ||
if (methodsParams[0].ParameterType == typeof(string) && methodsParams[1].ParameterType == typeof(bool)) | ||
{ | ||
var enumObj = methodInfo.Invoke(null, new object[] { enumKeyName, true }); | ||
bindingContext.Result = ModelBindingResult.Success(enumObj); | ||
return Task.CompletedTask; | ||
} | ||
} | ||
} | ||
} | ||
bindingContext.ModelState.TryAddModelError(propertyName, $"unable to call FromName on the SmartEnum of type {modelType}"); | ||
return Task.CompletedTask; | ||
} | ||
} | ||
} |
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,47 @@ | ||
using System; | ||
|
||
namespace Ardalis.SmartEnum.ModelBinding | ||
{ | ||
internal static class TypeUtil | ||
{ | ||
public static bool IsDerived(Type objectType, Type mainType) | ||
{ | ||
Type currentType = objectType.BaseType; | ||
|
||
if (currentType == null) | ||
{ | ||
return false; | ||
} | ||
|
||
while (currentType != typeof(object)) | ||
{ | ||
if (currentType.IsGenericType && currentType.GetGenericTypeDefinition() == mainType) | ||
return true; | ||
|
||
currentType = currentType.BaseType; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public static Type GetTypeFromGenericType(Type objectType, Type mainType) | ||
{ | ||
Type currentType = objectType.BaseType; | ||
|
||
if (currentType == null) | ||
{ | ||
return null; | ||
} | ||
|
||
while (currentType != typeof(object)) | ||
{ | ||
if (currentType.IsGenericType && currentType.GetGenericTypeDefinition() == mainType) | ||
return currentType; | ||
|
||
currentType = currentType.BaseType; | ||
} | ||
|
||
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