Automapper Auto Profile Registration.


On some of our projects we have been experimenting with smaller AutoMapper profiles.  The idea is that it is easier to digest a smaller profile. We have gone so far as creating a profile for each Domain object and handle all of the mappings to and from the domain object.  We are also trying out a Profile per scenario.  While these smaller profiles are easier to dig in and understand, the registration of them are a little painful.  So I put together a quick way to auto register all the profiles for automapper.  Below is the code to discover all the profiles in an assembly than register them with AutoMapper.  There is nothing fancy here and I could certainly spend more time making it better performing, but realistically this is startup code that runs once at application start up.  That being said I would rather focus performance optimization efforts on places that actually make a difference to the End User Experience.


Here is the sample.



It is important to know that we have full code coverage over our application including integration tests, so that if something were to break as a result of loading this in a non deterministic order, we would know before we commit our changes to source control.



Here is the code for the ForEach extension method, since I breezed over it.


This is a smaller post than I normally put together.. is this small of a post useful?

About Eric Hexter

I am the CTO for QuarterSpot. I (co)Founded MvcContrib, Should, Solution Factory, and Pstrami open source projects. I have co-authored MVC 2 in Action, MVC3 in Action, and MVC 4 in Action. I co-founded online events like mvcConf, aspConf, and Community for MVC. I am also a Microsoft MVP in ASP.Net.
This entry was posted in .Net, AutoMapper, c#, Open Source Software. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • Direct and to the point – I like it.

    Our profile is growing quite unwieldy too, but we only use it to map domain models to view models, so per-scenario wouldn’t help us much. Per domain object sounds interesting, but 95% of our domain models map to a single view model (at least at the root – their hierarchy gets collapsed down).

    Interesting idea though.

  • I find posts like this really handy, especially when they are as relevant to me as this one!

    I’m also finding my profiles to be a bit unwieldy, however I suspect I’m not using the Custom Type Converters, Custom Value Resolvers, Custom Value Formatters effectively.

    I notice you said “handle all of the mappings to and from the domain object”. Do you have two-way mapping then? In the project I am working on, I am attemping Jimmy’s suggestion of mapping EditModel to CommandMessages, but am still ironing out some of the rough edges in my implementation. Do you know if there an example of this in CodeCampServer?

  • @Craig here are some examples of some mappings with a TypeConverter in Code Camp Server

  • After seeing reading a bit about performance differences in foreach over a List and for over an array, I made the following adjustments to my implementation. Obviously, that destroys the lazy loading and enumerates immediately, but I figure you’re going to hit that at some point. All that to say, what was your thought behind just using a simple foreach (other than it’s simple)? Or was that it? Just curious. Mine is probably overkill. I haven’t even tested the performance implications (bad me); just trying stuff out.

  • @Ryan Honestly I went with the .ForEach extension method because it is already in our project and it reads like a sentence which is a big win, for me.

  • @Eric Sorry, I wasn’t questioning the use of ForEach, which I also use regularly, just the implementation. I probably over-engineered mine; just curious what you thought about the approach.

  • Great post! I’ve been very interested in Automapper Auto Profile Registration for a long time.

  • Great post! I’ve been very interested in Automapper Auto Profile Registration for a long time.

  • 2lp0WJ nxyxouttyolw, [url=]ksavcgdtamdh[/url], [link=]metjivcucgmb[/link],