Search code examples
model-view-controllerautomappersimple-injectorprofiles

Using a specific Automapper profile


I'm using AutoMappper 5.2 in my MVC project. I have made use of IMapper creating profiles which I understand profiles are a way to organise mappings. I am injecting IMapper into my controllers using Simple Injector to register an instance.

What I would like to know is can you use them in a way where you only retrieve/set up the profile you need for a specific controller? If so, how would you go about that? If you have to add all the profiles into one mapping configuration object does that have a performance impact or is it marginal?

I cannot find any resources or questions that deals with using a specific type of profile, they only deal with creating and registering them.


Solution

  • I think my answer to a similar question might help you. It is the last answer here: How to register AutoMapper 4.2.0 with Simple Injector.

    It's basically what Steven said.. you need to create a generic profile wrapper that implements the IMapper interface, with the generic argument being a specific profile. This allows you to create any number of profiles, batch-register them all, and to inject only the one that you need in your controller:

    ProfileMapper<ApplicationProfile> appProfileMapper;
    ProfileMapper<MvcProfile> mvcProfileMapper;
    ProfileMapper<GuestProfile> guestProfile;