Los Techies : Blogs about software and anything tech!

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.

 

image

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.

image

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

Kick It on DotNetKicks.com
Posted Nov 04 2009, 10:24 PM by erichexter

Comments

Darrell Mozingo wrote re: Automapper Auto Profile Registration.
on 11-05-2009 12:16 AM

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.

Reflective Perspective - Chris Alcock » The Morning Brew #470 wrote Reflective Perspective - Chris Alcock » The Morning Brew #470
on 11-05-2009 3:33 AM

Pingback from  Reflective Perspective - Chris Alcock  » The Morning Brew #470

Craig Cavalier wrote re: Automapper Auto Profile Registration.
on 11-05-2009 3:41 AM

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?

erichexter wrote re: Automapper Auto Profile Registration.
on 11-05-2009 5:01 AM

@Craig  here are some examples of some mappings with a TypeConverter in Code Camp Server http://bit.ly/4qHEQU

Ryan Riley wrote re: Automapper Auto Profile Registration.
on 11-05-2009 11:29 AM

After seeing reading a bit about performance differences in foreach over a List<T> and for over an array, I made the following adjustments to <a href="gist.github.com/227172">my implementation</a>. 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.

Twitter Trackbacks for Automapper Auto Profile Registration. - Eric Hexter - Los Techies : Blogs about software and anything tech! [lostechies.com] on Topsy.com wrote Twitter Trackbacks for Automapper Auto Profile Registration. - Eric Hexter - Los Techies : Blogs about software and anything tech! [lostechies.com] on Topsy.com
on 11-05-2009 2:14 PM

Pingback from  Twitter Trackbacks for                 Automapper Auto Profile Registration. - Eric Hexter - Los Techies : Blogs about software and anything tech!         [lostechies.com]        on Topsy.com

erichexter wrote re: Automapper Auto Profile Registration.
on 11-05-2009 2:49 PM

@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.

Ryan Riley wrote re: Automapper Auto Profile Registration.
on 11-06-2009 12:51 AM

@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.

Tyres Dealer wrote re: Automapper Auto Profile Registration.
on 11-16-2009 3:37 AM

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

Tyres Dealer wrote re: Automapper Auto Profile Registration.
on 11-16-2009 7:18 AM

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

txvkguusf wrote re: Automapper Auto Profile Registration.
on 11-21-2009 10:27 AM

2lp0WJ  <a href="nxyxouttyolw.com/.../a>, [url=http://ksavcgdtamdh.com/]ksavcgdtamdh[/url], [link=http://metjivcucgmb.com/]metjivcucgmb[/link], http://vfdriybkulua.com/

Copyright Los Techies 2008, 2009. All rights reserved.
Powered by Community Server (Commercial Edition), by Telligent Systems