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

Support SmartEnums #491

Open
mkalinski93 opened this issue Sep 14, 2021 · 0 comments
Open

Support SmartEnums #491

mkalinski93 opened this issue Sep 14, 2021 · 0 comments

Comments

@mkalinski93
Copy link

Hello,

I wanted to submit a feature request for strongly typed enumerations, such as SmartEnum from Ardalis.

SmartEnums, or enhanced enums, have several advantages and can be a good alternative to default enumerations within data controls such as DataGrid, SelectBoxes and so on.

SmartEnums can be integrated into EntityFramework Core with conversions

protected override void OnModelCreating(ModelBuilder builder)
{
    base.OnModelCreating(builder);

    builder.Entity<Policy>()
        .Property(p => p.PolicyStatus)
        .HasConversion(
            p => p.Value,
            p => PolicyStatus.FromValue(p));
}

In the DevExpress Support Center, I have asked for help and Andrew did a great start on implementing this function

 DevExtreme.AspNet.Data.Helpers.CustomFilterCompilers.RegisterBinaryExpressionCompiler(info => {
    if (info.AccessorText.Contains(".") && info.Operation == "=" && info.Value is Int64) {
        string accessorText = info.AccessorText.Substring(0, info.AccessorText.IndexOf("."));
        var memberExpr = Expression.PropertyOrField(info.DataItemExpression, accessorText);
        var memberType = memberExpr.Type;
        if (typeof(Ardalis.SmartEnum.SmartEnum<>).MakeGenericType(memberType).IsAssignableFrom(memberType)) {
            return Expression.MakeBinary(
                ExpressionType.Equal,
                Expression.Convert(Expression.Convert(memberExpr, typeof(object)), typeof(long)),
                Expression.Constant(info.Value)
            );
        }
    }
    return null;
});

The problem is that it may cannot covers all possible scenarios.
The key solution would be a native integration in the DevExtreme.AspNet.Data library.

By the way, SmartEnums can also be integrated within DevExpress XPO using a custom ValueConverter

public class ProductStateConverter : ValueConverter {
        public override Type StorageType => typeof(int);

        public override object ConvertFromStorageType(object value) {
            int.TryParse(value.ToString(), out int number);
            return ProductState.TryFromValue(number, out ProductState retVal) ? retVal : null;
        }

        public override object ConvertToStorageType(object value) {
            if (value is ProductState ruleType)
                return ruleType.Value;
            return null;
        }
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants