2

I am trying to map from Vehicle object to the Motor object using Automapper

public class Range<T>
{
    public T Min { get; set; }
    public T Max { get; set; }
}

public Enum SpeedType
{
    [Display(Name = "-")] Unknown = 0,
    [Display(Name = "M")] Manual= 1,
    [Display(Name = "A")] Automatic= 2
}

public class Vehicle
{
    public Range<string> Speed { get; set; }
}

public class Motor
{
    public Range<SpeedType?> Speed { get; set; }
}

I have tried using the MapFrom (reading the documentation) without any success. Can someone point me in the right direction. I am not even sure if this can be even mapped using Automapper. I have used automapper in the past for simple mappings.

3
  • Using Range<T> with an Enum as you've set it doesn't make sense. Why is Automatic max and Manual min or the other way around, There is no concept in the real world of one being greater than the other. Commented Dec 27, 2018 at 20:06
  • I have simplified the example Suppose for vehicle Min ="M", Max="A" then I want to convert it to Min=1,Max=2 Commented Dec 27, 2018 at 20:13
  • To be frank I'm not a big fan of AutoMapper. I had to use it in a project for more than a year but actually it is only good for making simple things more complicated than necessary. Some FromDto and ToDto [extension] methods are just simpler, faster, easier to debug and more reactive to code changes. Mapping between different class layouts often results practically as much code (or even more with tons of lambdas) as simply writing the mapping manually. See also: cezarypiatek.github.io/post/why-i-dont-use-automapper Commented Dec 27, 2018 at 20:22

2 Answers 2

2

This works for me:

Mapper.Initialize(cfg =>
{
    cfg.CreateMap<string, SpeedType?>().ConvertUsing(speed =>
    {
        switch (speed)
        {
            case "M": return SpeedType.Manual;
            case "A": return SpeedType.Automatic;
            default: return SpeedType.Unknown;
        }
    });

    cfg.CreateMap<Range<string>, Range<SpeedType?>>();
    cfg.CreateMap<Vehicle, Motor>();
});

var vehicle = new Vehicle
{
    Speed = new Range<string>
    {
        Min = "M",
        Max = "A"
    }
};

var motor = Mapper.Map<Vehicle, Motor>(vehicle);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your reply. Learned that switch could be added to Automapper.
FYI, you can basically add anything you want inside the curly braces. This is a lambda expression, so any code is valid as long as all the code path returns something
0

I came up with the following mapping to solve my problem. I wrote a GetEnum custom method

CreateMap<Vehicle,Motor>()
.ForMember(g => g.Speed, opt => opt.MapFrom(u=> new Range<SpeedType?>
                {
                    Min = EnumHelper.GetEnum<SpeedType?>(u.Speed.Min),
                    Max = EnumHelper.GetEnum<SpeedType?>(u.Speed.Max),
                }))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.