Automapper, mapping objects by linked association?

会有一股神秘感。 提交于 2019-12-12 02:59:57

问题


Lets say I have these models

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

I get an exception:

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

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.


回答1:


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

The answer is simple: you can't.

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

Mapper.CreateMap<Mouse, Cat>();

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

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.



来源:https://stackoverflow.com/questions/15617250/automapper-mapping-objects-by-linked-association

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!