AutoMapper - how to pass parameters to a custom resolver using the ConstructedBy method?

In my ASP.NET MVC 2 (RC) project - I use AutoMapper to map between the Linq to Sql class (Media) and the view model (MediaVM). The view model has a SelectList property for the drop-down list. I have a custom value converter to populate the elements of the SelectList property from db, but I'm wondering if there is a way to pass a pair of values ​​from the original model to resolver (using the ConstructedBy method?) Q a) define the selected element and b) filter the elements from db. The source object is passed to the custom resolver, but the recognizer is used in several different view models with different types of source objects, so it’s more likely to determine where to get the values ​​from my mapping configuration. Here is my model:

public class MediaVM { public bool Active { get; set; } public string Name { get; set; } [UIHint("DropDownList")] [DisplayName("Users")] public SelectList slUsers { get; private set; } } 

Automata mapping configuration:

  Mapper.CreateMap<Media, MediaVM>() .ForMember(dest => dest.slUsers, opt => opt.ResolveUsing<UsersSelectListResolver>()); 

It would be nice to be able to do something similar in the .ForMember conversion clause:

 .ConstructedBy(src => new UsersSelectListResolver(src.UserID, src.FilterVal)) 

Is there any way to do this?

+6
c # asp.net-mvc viewmodel automapper
source share
2 answers

I found your post trying to do the same. I decided to use a simple approach and skip trying to match my favorites list directly with AutoMaper. I simply return the array to my ViewModel and reference this object for my select list. The array gets mapped; the choice of the list object is not. Simple, effective. And, IMHO, everyone does this conceived task - map maps, ViewModel does the layout

 View Model code: [DisplayName("Criterion Type")] public virtual CriterionType[] CriterionTypes { get; set; } [DisplayName("Criterion Type")] public SelectList CriterionTypeList { get { return new SelectList(CriterionTypes, "Id", "Key"); } } 

my mapper:

  Mapper.CreateMap<Criterion, CriterionForm>() .ForMember(dest => dest.CriterionTypeList, opt => opt.Ignore()); 
+2
source share

I like this idea as a function request. You can do something similar right now using MapFrom:

 ForMember(dest => dest.slUsers, opt => opt.MapFrom(src => new UsersSelectListResolver(src).Resolve(src)); 
+8
source share

All Articles