简体   繁体   English

AutoMapper - 如何使用ConstructedBy方法将参数传递到自定义解析器?

[英]AutoMapper - How to pass parameters into a Custom Resolver using ConstructedBy method?

In my ASP.NET MVC 2 (RC) project - I'm using AutoMapper to map between a Linq to Sql class (Media) and a view model (MediaVM). 在我的ASP.NET MVC 2(RC)项目中 - 我使用AutoMapper在Linq到Sql类(Media)和视图模型(MediaVM)之间进行映射。 The view model has a SelectList property for a drop down in the view. 视图模型具有SelectList属性,用于视图中的下拉列表。 I have a custom value resolver to populate the SelectList property items from the db, but am wondering if there's a way to pass a couple values from the source model into the resolver (using ConstructedBy method?) to a) define the selected item and b) filter the items from the db. 我有一个自定义值解析器来填充数据库中的SelectList属性项,但我想知道是否有办法将源模型中的几个值传递给解析器(使用ConstructedBy方法?)来a)定义所选项目和b )过滤数据库中的项目。 The source object gets passed into the custom resolver - but the resolver is used on several different view models with different types of source objects, so would rather define where to get the values from in my mapping config. 源对象被传递到自定义解析器 - 但解析器用于具有不同类型源对象的几个不同视图模型,因此宁愿定义从映射配置中获取值的位置。 Here is my view model: 这是我的视图模型:

public class MediaVM
{
    public bool Active { get; set; }
    public string Name { get; set; }

    [UIHint("DropDownList")]
    [DisplayName("Users")]
    public SelectList slUsers { get; private set; }
}        

The automapper mapping config: automapper映射配置:

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

It would be nice to be able to do something like this on the .ForMember mapping clause: 能够在.ForMember映射子句上执行类似的操作会很高兴:

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

Is there a way to accomplish this? 有没有办法实现这个目标?

I like that idea as a feature request. 我喜欢这个想法作为功能请求。 You can do something like that right now, with MapFrom: 您可以使用MapFrom立即执行此类操作:

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

I found your posting trying to do the same thing. 我发现你的帖子试图做同样的事情。 I decided on a simple approach and skip trying to map to my select list directly via AutoMaper. 我决定采用一种简单的方法,并尝试直接通过AutoMaper映射到我的选择列表。 I simply return an array into my ViewModel and reference that object for my select list. 我只是将一个数组返回到我的ViewModel中,并为我的选择列表引用该对象。 The array gets mapped, select list object does not. 数组被映射,选择列表对象没有。 Simple, effective. 简单,有效。 And, IMHO each is doing it's intended task - the mapper maps, the ViewModel does the layout 并且,恕我直言各自正在做它的预期任务 - 映射器映射,ViewModel进行布局

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());     

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM