Open
Description
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;
}
}