13

I have a simple question: How to use Mapper.Map inside ConstructUsing? I'm using AutoMapper v4.1.1 and I have this piece of code that I want to cleanup by reusing the code.

Mapper.CreateMap<SKU, SKUViewModel>()
    .ConstructUsing(m => new SKUViewModel(
    (from texts in m.DescriptionTranslation.TranslationTexts
        select new TranslationTuple
        {
            LanguageId = texts.LanguageId,
            LanguageDisplayName = texts.Language.DisplayName,
            TranslationText = texts.Text,
            NeutralText = texts.NeutralText,
            ThreeLetterIsoLanguageName = texts.Language.ThreeLetterISOLanguageName
        }).ToList(),
        (from texts in m.DisplayNameTranslation.TranslationTexts
        select new TranslationTuple
        {
             LanguageId = texts.LanguageId,
             LanguageDisplayName = texts.Language.DisplayName,
             TranslationText = texts.Text,
             NeutralText = texts.NeutralText,
             ThreeLetterIsoLanguageName = texts.Language.ThreeLetterISOLanguageName
        }).ToList(),
        (from texts in m.PaypalDescriptionTranslation.TranslationTexts
        select new TranslationTuple
        {
             LanguageId = texts.LanguageId,
             LanguageDisplayName = texts.Language.DisplayName,
             TranslationText = texts.Text,
             NeutralText = texts.NeutralText,
             ThreeLetterIsoLanguageName = texts.Language.ThreeLetterISOLanguageName
        }).ToList()));

I know we can use Mapper.Map with the AfterMap method like this .AfterMap((s, d) => Mapper.Map(s.CompanyProfile, d));

But I'm not able to do the same inside ConstructUsing.

Any suggestion ?

David

1 Answer 1

12

Since you have mappings defined for these entities, you could call Mapper.Map on it. For sample:

Mapper.CreateMap<SKU, SKUViewModel>()
    .ConstructUsing(m => 
    {
        var descriptions = Mapper.Map<List<TranslationTuple>>(m.DescriptionTranslation.TranslationTexts);
        var displays = Mapper.Map<List<TranslationTuple>>(m.DisplayNameTranslation.TranslationTexts);
        var paypals = Mapper.Map<List<TranslationTuple>>(m.PaypalDescriptionTranslation.TranslationTexts);
        
        return new SKUViewModel(descriptions, displays, paypals);
    });

Then, when you need to create an object mapped by automapper, just use:

var viewModel = Mapper.Map<List<SKUViewModel>>(sku);

Methods like ConstructUsing, AfterMap, BeforeMap are methods that is executed after you have everything defined. So, following this logic, it should execute Mapper.Map<> without problems.

Sign up to request clarification or add additional context in comments.

5 Comments

From version 4.2 of Automapper it's deprecated to use static method Mapper.Map(). So you should use ConstructUsing() with ResolutionContext and call ResolutionContext.Engine.Map().
@AndreyBurykin do you have an example implementation?
@Jack Here is a good article with examples: lostechies.com/jimmybogard/2016/01/21/…
example: CreateMap<S, D>().ConstructUsing( context => { var entity = (S)context.SourceValue; return new D { Id = entity.Id, c = context.Engine.Mapper.Map<C>entity.c) }; });
i had to go things like .ConstructUsing((s, c) => c.Mapper.Map<Order, OrderViewModel>(s.Order))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.