Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automapper mapping Flags Enum from ViewModel to Domain model

While trying to translate between my ViewModel and my domain model using AutoMapper I noticed that it does not play well with Enums marked the the Flags attribute.

Here is a quick mockup of the classes:

ViewModel:

public class TestViewModel
{
    // array of individual Enum values
    public TestEnum[] TestEnum { get; set; } 
}

Domain Model:

public class TestModel
{
    // single Enum marked with flags attribute
    public TestEnum TestEnum { get; set; }
}

Enum:

[Flags]
public enum TestEnum
{
    Test1,
    Test2,
    Test3,
    Test4
}

This is what I am trying to do. I guess I need a Custom resolver of some sort in my Automapper config because it throws an exception when I do Mapper.Map().

My question: How would I accomplish this?

Bonus question: Is this best practice for handling Flags Enums / Bitmasks in Viewmodel -> Domain models (in a MVVM respect)? If not, what practice would you suggest (using AutoMapper or otherwise)?

like image 240
Brad C Avatar asked Jun 09 '26 10:06

Brad C


1 Answers

When mapping to the view model, you can try to use Enum.GetValues() and LINQ to get a list of enum values. To map back to the model, try using Aggregate()...

Mapper.CreateMap<TestModel, TestViewModel>()
    .ForMember(v => v.TestEnum, 
        x => x.MapFrom(m => Enum.GetValues(typeof(TestEnum))
                            .Cast<TestEnum>()
                            .Where(e => (e & m) > 0)
                            .ToList()))
    .ReverseMap()
    .ForMember(m => m.TestEnum,
        x => x.MapFrom(v => v.Aggregate((i, j) => i | j));

As for whether this is the best approach, it really depends on how the view model is being used. Currently, the view model doesn't contain flags that aren't set; do you need them for rendering the view?

like image 93
Anthony Chu Avatar answered Jun 11 '26 23:06

Anthony Chu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!