开发者

Automapper: using BeforeMap and AfterMap

I am us开发者_如何转开发ing automapper (successfully up to a point) to perform a polymorphic map between two interfaces like so:

configure.CreateMap<IFrom, ITo>()
    .Include<FromImplementation1, ToImplementation1>()
    .Include<FromImplementation2, ToImplementation2>()
    ... ;

This works fine. In addition however, the interfaces include method signatures, the implementations of which are intended to modify the objects before mapping:

public interface IFrom
{
    void PrepareForMapping();
}

As you can see the method has no return but is designed to modify the state of the object before the mapping is performed. At present this method is called manually before the object is mapped, but my intention was to execute the method automatically before the mapping takes place. I attempted to use it as follows:

configure.CreateMap<IFrom, ITo>()
    .BeforeMap((x,y) => x.PrepareForMapping())
    .Include<FromImplementation1, ToImplementation1>()
    .Include<FromImplementation2, ToImplementation2>()
    ... ;

However the method is never being called, although the mapping itself is still working fine. I have placed breakpoints on every implementation of the PrepareForMapping() method and none of them are getting hit. So I came to the conclusion that I have either misunderstood how BeforeMap/AfterMap work, or I am doing something wrong (or both).

Many thanks.


For this one, you'll have to put the Before/After map on the derived types. This is because Include redirects the map to the polymorphic types. It's not an additive configuration, the Included maps replace the configuration.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜