Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #20

Merged
merged 2 commits into from
Aug 4, 2023
Merged

Dev #20

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions src/MAOToolkit.Web/Utilities/ModelBinders/FlagsEnumModelBinder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding;

namespace MAOToolkit.Utilities.ModelBinders
{
public class FlagsEnumModelBinder : IModelBinder
{
public Task BindModelAsync(ModelBindingContext bindingContext)
{
if (bindingContext is null)
{
throw new ArgumentNullException(nameof(bindingContext));
}

// Only accept enum values.
if (!bindingContext.ModelMetadata.IsFlagsEnum)
return Task.CompletedTask;

var valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);

if (valueProviderResult == ValueProviderResult.None)
{
return Task.CompletedTask;
}

// Get the real enum type.
var enumType = bindingContext.ModelType;
enumType = Nullable.GetUnderlyingType(enumType) ?? enumType;

// Each value self may contains a series of actual values, split it with comma.
var strs = valueProviderResult.Values.SelectMany(s => s?.Split(',') ?? Array.Empty<string>());

// Convert all items into enum items.
var actualValues = strs.Select(valueString => Enum.Parse(enumType, valueString));

// Merge to final result.
var result = actualValues.Aggregate(0, (current, value) => current | Convert.ToInt32(value));

// Convert to Enum object.
var realResult = Enum.ToObject(enumType, result);

// Result
bindingContext.Result = ModelBindingResult.Success(realResult);
return Task.CompletedTask;
}
}

/// <inheritdoc />
/// <summary>
/// An <see cref="IModelBinderProvider" /> used to provider <see cref="FlagsEnumModelBinder" /> instances.
/// </summary>
public class FlagsEnumModelBinderProvider : IModelBinderProvider
{
/// <inheritdoc />
/// <summary>
/// Creates a <see cref="IModelBinder" /> based on <see cref="ModelBinderProviderContext" />.
/// </summary>
/// <param name="context">The <see cref="ModelBinderProviderContext" />.</param>
/// <returns>An <see cref="IModelBinder" />.</returns>
public IModelBinder? GetBinder(ModelBinderProviderContext context)
{
if (context is null)
{
throw new ArgumentNullException(nameof(context));
}

return context.Metadata.IsFlagsEnum ? new FlagsEnumModelBinder() : null;
}
}

public static class FlagsEnumModelBinderExtensions
{
public static void AddFlagsEnumProvider(this MvcOptions option)
{
option.ModelBinderProviders.Insert(0, new FlagsEnumModelBinderProvider());
}
}
}
19 changes: 17 additions & 2 deletions src/MAOToolkit/Extensions/QueryableExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
using System.Linq.Expressions;

using System.Reflection;

namespace MAOToolkit.Extensions
{
public static class QueryableExtensions
{
{
public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> queryable, string orderByProperty, bool desc = false)
{
string command = desc ? "OrderByDescending" : "OrderBy";
var type = typeof(T);
var property = type.GetProperty(orderByProperty, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
ArgumentNullException.ThrowIfNull(property);

var parameter = Expression.Parameter(type, "p");
var propertyAccess = Expression.MakeMemberAccess(parameter, property);
var orderByExpression = Expression.Lambda(propertyAccess, parameter);
var resultExpression = Expression.Call(typeof(Queryable), command, new Type[] { type, property.PropertyType }, queryable.Expression, Expression.Quote(orderByExpression));
return (IOrderedQueryable<T>)queryable.Provider.CreateQuery<T>(resultExpression);
}

public static IQueryable<T> WhereAny<T>(this IQueryable<T> queryable, params Expression<Func<T, bool>>[] predicates)
{
var parameter = Expression.Parameter(typeof(T));
Expand Down