自动映射器,通过链接关联映射对象? [英] Automapper, mapping objects by linked association?

查看:105
本文介绍了自动映射器,通过链接关联映射对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我说我有这些模型

   public class Mouse
    {
        public string Cheese { get; set; }
    }

    public class Cat
    {
        public string Hairball { get; set; }
    }

    public class Dog
    {
        public string ChewyToy { get; set; }
    }

然后我将鼠标映射到猫,然后将猫映射到狗:

And I map a Mouse to a Cat then a Cat to a Dog:

    Mapper.CreateMap<Mouse, Cat>().ForMember(m => m.Hairball, o => o.MapFrom(src => src.Cheese));
    Mapper.CreateMap<Cat, Dog>().ForMember(m => m.ChewyToy, o => o.MapFrom(src => src.Hairball));

通过扩展,鼠标也应该映射到狗,对吗? 当我尝试在两者之间映射时:

By extension, a Mouse should also be mapped to a Dog, right? When I try to map between the two:

    var mouse = new Mouse() { Cheese = "Eat me" };

    Dog dog = Mapper.Map<Mouse, Dog>(mouse);

我得到一个例外:

尝试将App_Start.MapConfig + Mouse映射到 App_Start.MapConfig + Dog.类型异常 抛出了"AutoMapper.AutoMapperMappingException".

Trying to map App_Start.MapConfig+Mouse to App_Start.MapConfig+Dog. Exception of type 'AutoMapper.AutoMapperMappingException' was thrown.

如何在不显式创建鼠标与狗之间的映射的情况下执行此操作?这是一个更大问题的简单示例.如果它们支持这种行为,我也愿意接受诸如Value Injector,Glue等交换框架的支持.

How do I do this with out explicitly creating a map between a Mouse to a Dog? This is a simple example of a bigger issue. I'm also open to switching frameworks such as Value Injector, Glue, etc if they support this behavior.

推荐答案

该如何在不创建鼠标到鼠标之间的映射的情况下执行此操作 狗吗?

How do I do this with out explicitly creating a map between a Mouse to a Dog?

答案很简单:你不能.

Automapper具有映射对象的非常简单的逻辑.添加新映射时:

Automapper has pretty straightforward logic for mapping objects. When you are adding new mapping:

Mapper.CreateMap<Mouse, Cat>();

将创建新的TypeMap,并将其添加到类型映射列表中.是的,这里有简单的清单.每个TypeMap具有两个属性:DestinationTypeSourceType.中间没东西.当您尝试映射某些对象时,只需在列表中搜索第一个TypeMap,它具有与映射中指定的完全相同的源和目标类型.它是通过以下方法完成的( ConfigurationStore 类负责用于保存已配置的映射):

New TypeMap is created and added to list of type maps. Yes, there is simple list. And each TypeMap has two properties: DestinationType and SourceType. Nothing in the middle. When you are trying to map some object, then list is simply searched for first TypeMap which has exactly same source and destination types, as specified in your mapping. It is done with following method (ConfigurationStore class is responsible for holding configured mappings):

TypeMap FindExplicitlyDefinedTypeMap(Type sourceType, Type destinationType)
{
    return this._typeMaps.FirstOrDefault<TypeMap>(x => 
               ((x.DestinationType == destinationType) && 
                (x.SourceType == sourceType)));
}

因此,对于您的示例,列表中将有两个TypeMap对象,当然它们都不符合DestinationType等于DogSourceType等于Mouse的条件.

So, for your sample there will be two TypeMap objects in list, and of course none of them matches condition of DestinationType equalt to Dog and SourceType equal to Mouse.

这篇关于自动映射器,通过链接关联映射对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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