AutoMapper meets Autofac

November 16, 2009 22:05

For a project I’m currently consulting on I thought it might be a good idea to have a closer look at AutoMapper and since the IOC Container of choice at the project is Autofac, finding a title for this post wasn’t that difficult :-).

First thing we wanted to do was wrap the AutoMapper method calls in a way that would allow us to write our own custom mapping if we wanted to for special cases, so this is what we came up with:

image

And for the implementation:

image

This should allow us to do something like this with Autofac:

image

Since in a lot of cases we wanted to do the mapping both ways, we seemed to be missing this method:

 image

Which should allow us to construct a mapping like this:

image

So far so good...

Comments

# 11/17/2009 10:21:21 AM Nicholas Blumhardt

Looks nice.

Mapper's 'Engine' might be a good candidate for constructor injection, since it is a required dependency.

PersonService could also be registered by type, i.e. Register(typeof(PersonService)), if you're happy for its dependency on IMapper can be autowired.

Just my preferences though Smile

Quite a few people have asked about the AutoMapper/Autofac combo. I don't know much about AutoMapper (looking forward to a chance to use it in anger) so reading more about your experiences should be enlightening.

# 11/17/2009 7:47:58 PM Roby Van Damme

the IMappingEngine will probably end up being private and constructor injected as you suggest. Still looking at what's possible with Autofac so good advice is always welcome ;)

I'll be digging a bit deeper into it and get my findings out here as I progress...

# 12/6/2009 2:57:39 PM Vietnam Tours

Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with more information? It is extremely helpful and beneficial to your readers.

# 12/7/2009 8:03:27 PM Roby Van Damme

AutoMapper meets Autofac, part 2

AutoMapper meets Autofac, part 2

Comments are closed

Roby Van Damme

I'm an independent .NET application development consultant, currently living in Antwerp.

View Roby Van Damme's profile on LinkedIn

Follow robyvandamme on Twitter