1

I need to map domain objects to DTO objects in my SPA, built on Angular and WebAPI. For this feature I have decided to use some auto-mapping tool. During my mapping I need to provide some contextual information which extends me global mapping settings. In fact I want to implement this:

int i = 10;
var mapper = config.CreateMapper();
mapper.Map<Source, Dest>(src, opt => {
    opt.BeforeMap((src, dest) => src.Value = src.Value + i);
    opt.AfterMap((src, dest) => dest.Name = HttpContext.Current.Identity.Name);
});

This is Automapper code. But I do not want to use Atomapper because it is very slow.

I have looked through other tools like (Mapster, ExpressMapper). This tools are cool and quick, but they do not support inline settings as far as I know.

Can somebody advice me some other automapping tool, which supports this feature. Or maybe somebody gove me advice, how to implement this with one of the mentioned tools?

Stalso
  • 1,468
  • 2
  • 12
  • 21
  • 1
    The reason why AutoMapper is slow is because of the exact feature you're highlighting right now. Those features are expensive, and make things slower. – Jimmy Bogard Feb 22 '16 at 23:14

1 Answers1

0

ValueInjecter can create custom maps:

Mapper.AddMap<Customer, CustomerInput>(src =>
{
    var res = new CustomerInput();
    res.InjectFrom(src); // maps properties with same name and type
    res.FullName = src.FirstName + " " + src.LastName;
    return res;
});

EmitMapper has a PostProcess method:

var mapper = ObjectMapperManager
    .DefaultInstance
    .GetMapper<Source, Destination>(
        new DefaultMapConfig()
        .PostProcess<Destination>( (destination, state) => { destination.list.Sort(); return destination;} )
    );

If AutoMapper is too slow for you then EmitMapper adaptation maybe a good choice. Look on this benchmark: Emit mapper vs valueinjecter or automapper performance.

Unfortunately EmitMapper is abandonded years ago.

Community
  • 1
  • 1
Ilya Chumakov
  • 23,161
  • 9
  • 86
  • 114
  • Thank you. It is real trouble. Maybe you know some techniques to implement this with handwritten mapping? I mean automatically map part of the object, and map the rest by myself. Can you suggest some convenient way to do this – Stalso Feb 22 '16 at 17:17
  • @Stalso, you may use `FastMapper` for auto-map part. Or similar tool which is much faster than AutoMapper and stills alive. Then leave another part handwritten and write a wrapper for combining these two parts, like `MyMapper.Map(source, dest)`. Sorry if it is an obvious advice. – Ilya Chumakov Feb 22 '16 at 17:27
  • Yes it is obvious.) Yet, thank you. I have implemented some tecnique with Mapster and I will publish it, after I test it. – Stalso Feb 23 '16 at 11:02