DevExpress / DevExpress/DevExtreme.AspNet.Data

Support SmartEnums

Open
#491 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement has-workaround
Dominant language
C#
Stars
165
Forks
140
Avg merge
1d 2h
Merged PRs (30d)
4

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the DevExtreme.AspNet.Data library and the CustomFilterCompilers.RegisterBinaryExpressionCompiler example in the issue. Determine the supported SmartEnum scenarios and how native integration should cover them; done means SmartEnums work through the data layer without the custom compiler workaround.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
backend-api-design
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.