0

Is it possible to configure Automapper so it can map from a collection to a single instance based on a run-time parameter?

In our application our entities have labels for each language. Depending on the preference of the current user, a single value should be shown.

DepartmentEntity
  Labels: Label[]
    Value: string
    Culture: CultureInfo

DepartmentViewModel
  Label: string

Given I know the current user culture when I map my entities, could I configure Automapper so it can map from DepartmentEntity to DepartmentViewModel?

Something like

var department = Mapper.Map<DepartmentEntity, DepartmentViewModel>(user.Culture);

Other answers like this one seem to want to pass a parameter to use as target value straight up. I want to use it as a way to reduce a list to a single field.

Boris Callens
  • 90,659
  • 85
  • 207
  • 305

1 Answers1

3

You can do it the same as the answer you mentioned:

var department = Mapper.Map<DepartmentEntity, DepartmentViewModel>(departmentEntity,
  opt => opt.AfterMap((src, dest) => dest.Label = src.Labels.FirstOrDefault(x=> 
                                       x.CultureInfo.Name == user.Culture.Name)?.Value));

Keep in mind that you have to make the ignore call when defining the map.

UPDATE

You are correct and after some more research it's actually possible and fairly easy to achieve this with Automapper. The feature I got it from this SO answer.

cfg.CreateMap<DepartmentEntity, DepartmentViewModel>().ForMember(x => x.Label, opt => 
opt.ResolveUsing((src, dest, member, context) => 
                  src.Labels.FirstOrDefault(x=> x.Culture.Name == 
                           context.Items["userCulture"].ToString())?.Value));

And when converting objects use

var userCulture = new CultureInfo("en-US");
var department = Mapper.Map<DepartmentViewModel>(depEntity,
            opt => opt.Items["userCulture"] = userCulture.Name);
Mihail Stancescu
  • 4,088
  • 1
  • 16
  • 21
  • Isn't the whole reason automapper exists so that you can define mapping logic once on one place (preferably by conventions)? What's the advantage of aftermap over just using my source and destination objects in a second line of code? – Boris Callens Dec 08 '17 at 14:58