AutoMapper - 如何将参数传递到自定义解析使用ConstructedBy方法? [英] AutoMapper - How to pass parameters into a Custom Resolver using ConstructedBy method?

查看:615
本文介绍了AutoMapper - 如何将参数传递到自定义解析使用ConstructedBy方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的ASP.NET MVC 2(RC)项目 - 我使用AutoMapper到LINQ to SQL类(媒体)和视图模型(MediaVM)之间进行映射。视图模型的选择列表属性下降的观点了。我有一个自定义值解析器来填充从数据库中选择列表属性的项目,但我不知道是否有一种方法来传递从源模型一对夫妇的值到解析器(使用ConstructedBy方法?)到)定义选择的项目和b )过滤来自分贝的项目。源对象获取传递到自定义解析器 - 但解析器上使用几个不同的视图模型与不同类型的源对象,所以宁愿确定从哪里我的映射配置获取值。这里是我的视图模型:

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). The view model has a SelectList property for a drop down in the view. 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. 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; }
}        

在automapper映射配置:

The automapper mapping config:

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

这将是很好能够做这样的事情在.ForMember映射子句:

It would be nice to be able to do something like this on the .ForMember mapping clause:

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

有没有办法做到这一点?

Is there a way to accomplish this?

推荐答案

我发现你的帖子试图做同样的事情。我决定做一个简单的方法,并跳过试图映射到我的选择直接通过AutoMaper列表。我只是返回一个数组到我的视图模型和参考的对象为我的选择列表中。该数组被映射,选择列表中的对象没有。简单,有效。而且,恕我直言,每次做它的目标任务 - 映射器,视图模型做布局

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. I simply return an array into my ViewModel and reference that object for my select list. 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

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");
            }
        }  

我的映射:

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

这篇关于AutoMapper - 如何将参数传递到自定义解析使用ConstructedBy方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆