如何使用automapper复杂类型 [英] How to use automapper with complex types

查看:707
本文介绍了如何使用automapper复杂类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的观点我的域模型我有一个作为后盾的田野上我的属性。

Within my domain model for my view I have the following object that is serving as backing fields for my properties

public class ModelProperty<T>// where t:struct
{
    public T Value { get; set; }
    public string Description { get;  set; }
    public string LabelName { get;  set; }
}

在将对象是作为:

    public partial class Incident : BaseEntityModel
    {
        private ModelProperty<string> typeCode = new ModelProperty<string>{Description="1-C", LabelName =  "Type Code"};
        private ModelProperty<string> typeText = new ModelProperty<string>{Description="1-C", LabelName =  "Type Text"};

        public ModelProperty<string> TypeCode { get {return typeCode;}}
        public ModelProperty<string> TypeText { get {return typeText;}}
    }



业务对象(我的源) 。不是那么复杂

The business object (my source) is not as complex.

public partial class Incident : ObjectBase
{
    public string TypeCode { get; set; }
    public string TypeText { get; set; }
}



是有可能的值从源到目标映射。使用Automapper我有以下映射设置

is it possible to map the values from the source to the target. Using Automapper I have the following mapping setup

//note SrcObj is not an object but a namespace alias since the domain and business objects are of the same name
Mapper.CreateMap<SrcObj.Incident, Incident>()
                .ForMember(ui => ui.TypeText.Value,
                           opt => opt.MapFrom(src => src.TypeText));



不过,我得到异常表达式必须解析到顶级成员,并没有任何子对象的属性。使用对孩子的类型或代替AfterMap选项自定义解析。

But I am getting the exception Expression must resolve to top-level member and not any child object's properties. Use a custom resolver on the child type or the AfterMap option instead.

我是新来automapper但在看文档,我有太多复杂的工作对象(基于这样的思想,有确实三种这里不是两个)?

I'm new to automapper but in looking at the documentation is the object I am working with too complex (based on the idea that there are really three types here and not two)?

如果有可能处理这种类型的映射这是怎么做的?

If it is possible to handle this type of mapping how is this done?

更新

根据在从吉米的建议,我已经更新了我的代码如下:

Based upon the suggestion from Jimmy I have updated my code as follows:

Mapper.CreateMap<SrcObj.Incident, Incident>();
Mapper.CreateMap<string, ModelProperty<string>>()
                .ConvertUsing(src => new ModelProperty<string> { Value = src });

Mapper.AssertConfigurationIsValid();     
SrcObj.Incident viewModelDto = md.GenerateMockIncident(); //populate the business object with mock data    
uibase = Mapper.Map<SrcObj.Incident, Incident>(viewModelDto);



中的代码执行和我没有得到任何异常,但是正在被设置的值,并在返回业务对象仍没有得到分配给后盾财产它仍然是空。

我在想什么?

-cheers

推荐答案

这是更简单的方法是创建一个类型转换器:

An easier way is to create a type converter:

Mapper.CreateMap<string, ModelProperty<string>>()
    .ConvertUsing(src => new ModelProperty<string> { Value = src });



然后你就会有这样的每AutoMapper看到的字符串时 - > ModelProperty。你不会在所有做特定成员的配置。

Then you'll have this for every time AutoMapper sees string -> ModelProperty. You won't have to do member-specific configuration at all.

这篇关于如何使用automapper复杂类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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