chobo2
chobo2

Reputation: 85755

Can auto mapper do this?

I am wondering how I can auto map this or if it can be done at all(I am thinking not but I just wanted to check).

I have something like this

public class RemindersViewModel()
{
   public List<SelectListItem> Reminder { get; set; }
   public ReminderTypes SelectedReminder { get; set; }
}

First I need to populate the SelectListItems

Mapper.CreateMap<ReminderTypes, SelectListItem>().ForMember(dest => dest.Text,
                                           opt =>opt.MapFrom(src => src.ReminderTypes))
             .ForMember(dest => dest.Value, opt => opt.MapFrom(src => src.ReminderTypes));

I also want to point out that ReminderTypes is a enum. I am wondering so it seems I can't do src.ReminderTypes. I am wondering how can I do that as well?

// controller

  List<ReminderTypes> types = Service.GetReminderTypes();
  List<RemindersViewModel> viewModel = new List<RemindersViewModel>();
  RemindersViewModel map = Mapper.Map<List<ReminderTypes>, List<SelectListItem>>(types);

So now I got the selectlistitems mapped. Now I want to add "map" to viewModel. I want the same "map" to be added to the viewModel 5 times.

Is it possible to somehow use automapper to say take this "map" and map it 5 times to my viewModel?

Or do I have to use a for loop?

for(int i =0; i < 5; i++)
{
   viewModel.Add(map);
}

Like I said I don't think it is possible but I am curious to know since I not been using atuomapper for long.

Upvotes: 1

Views: 2185

Answers (1)

Rookian
Rookian

Reputation: 20539

To your 1st question: I also want to point out that ReminderTypes is a enum. I am wondering so it seems I can't do src.ReminderTypes. I am wondering how can I do that as well?

CreateMap<ReminderType, SelectListItem>()
    .ForMember(d => d.Text, o => o.MapFrom(x => x))
    .ForMember(d => d.Value, o => o.MapFrom(x => (int) x));

Then you have to loop through each ReminderType and set the value for SelectListItem. I do this within a wrapper ("MappingService") around AutoMapper.

Here is a part of it:

public object Map(object source, Type sourceType, Type destinationType)
{
    if (source is IEnumerable)
    {
        IEnumerable<object> input = ((IEnumerable)source).OfType<object>();
        Array a = Array.CreateInstance(destinationType.GetElementType(), input.Count());

        int index = 0;
        foreach (object data in input)
        {
            a.SetValue(AutoMap(data, data.GetType(), destinationType.GetElementType()), index);
            index++;
        }
        return a;
    }

    return AutoMap(source, sourceType, destinationType);
}

AutoMap is a public static delegate to the real implementation Mapper.Map. Keep attention that this code does only work with arrays!

In your controller you now could write:

public ActionResult Reminders()
{
    ReminderService reminderService = new ReminderService();
    ReminderType[] reminderTypes = reminderService.GetReminderTypes();
    SelectListItem[] selectListItems = _mappingService.Map(reminderTypes, reminderTypes.GetType(), typeof(SelectListItem[])) as SelectListItem[];

    RemindersViewModel remindersViewModel = new RemindersViewModel
                                                {
                                                    Reminder = selectListItems,
                                                    SelectedReminder = ReminderType.CReminder
                                                };

    return View(remindersViewModel);

}

View:

<% =Html.DropDownListFor(x => x.SelectedReminder, new SelectList(Model.Reminder, "Value", "Text")) %>

2nd question: Is it possible to somehow use automapper to say take this "map" and map it 5 times to my viewModel?

As far as I know this is not possible. But let me know if there is a way of doing this.

Upvotes: 1

Related Questions