An AutoMapper success story

I got a cool message on the AutoMapper mailing list from Howard Van Rooijen on how they used AutoMapper in a site they recently launched to production:

Hello AutoMapper Community,

I just wanted to let you know that an e-commerce site which uses AutoMapper as part of it’s core architecture has just been released into the wild:

http://www.fancydressoutfitters.co.uk

And I wanted to say a HUGE thank-you to Jimmy & the Community for this wonderful tool – that helps remove so much commodity plumbing code from the solution.

We were a little sceptical at the start of the project that AutoMapper would "cut the mustard" when it came to the performance requirements of a public facing, high load, e-commerce site because of the amount of reflection AutoMapper uses at its core, but we have been incredibly impressed with the performance of the solution under load.

The site is based on the S#arp Architecture Framework and its become very apparent how well AutoMapper fits into MVC style architecture as it enables easy separation of concerns with regards to object conversion (entities & ViewModels). Once we moved from hand-cranked converters to AutoMapper it was amazing how much cleaner our code became – so much so that we modified the overall solution architecture to incorporate explicit mapping layers (see attached image). Our general pattern of usage within MVC is as follows:

1. Map input into Domain Entities in the Controller

2. Pass Domain Entities into Task Layer to "do stuff"

3. Map output of Task Layer (Domain Entities) into ViewModel

4. Pass ViewModel to ViewEngine

Simple, slick and clean.

To formalise the Mapping Layer and make it testable we implemented a simple interface:

public interface IMapper<TInput, TOutput>

{

TOutput MapFrom(TInput input);

}

Next we’d implement a custom marker interface so that we could resolve the mapper from the DI container and we adopted the naming convention <Input Type><Output Type>Mapper:

public interface IEditModelEntityMapper : IMapper<EditModel, Entity>

{

}

Then finally implement the interface:

public class EditModelEntityMapper : IEditModelEntityMapper

{

public EditModelEntityMapper()

{

Mapper.CreateMap<EditModel, Entity>()

.ForMember(x => x.Property, y => y.MapFrom(z => z.Property));

}

public Entity MapFrom(EditModel input)

{

return Mapper.Map<EditModel, Entity>(input);

}

}

Then to actually use it in the MVC app:

public class CustomController : Controller

{

private readonly ITasks tasks;

private readonly IEditModelEntityMapper editModelEntityMapper;

private readonly IOutputViewModelMapper outputViewModelMapper;

public CustomController(ITasks tasks,

                                           IEditModelEntityMapper editModelEntityMapper,

                                           IOutputViewModelMapper outputViewModelMapper)

{

this.tasks = tasks;

this.editModelEntityMapper = editModelEntityMapper;

this.outputViewModelMapper = outputViewModelMapper;

}

public ActionResult Index(EditEntity input)

{

var entity = this.editModelEntityMapper.MapFrom(input);

                var output = this.tasks.DoSomething(entity)

                var viewModel = this.outputViewModelMapper.MapFrom(output);

return View(viewModel);

}

}

For those who are interested – here’s a little more info:

- We ran the project using Scrum and delivered in 20 weeks: 10 x 2 week iterations

- It’s based on the S#arp Architecture framework, which we extended to support, AutoMapper, Spark and ViewModels
- Solution performs very well: 1000 concurrent users per web server, generating around 180 pages per second across 2x single quad core 64bit servers.

Again, many thanks,

Howard

Howard also shared a neat little diagram of his architecture:

image

This is one of the greatest feelings from doing OSS – that something you created basically just for yourself can also help out other folks out there trying to deliver value for their customers.  Thanks for all the feedback everyone, as well as kudos to the S#arp Architecture team for building such a great framework for MVC.

Related Articles:

Post Footer automatically generated by Add Post Footer Plugin for wordpress.

About Jimmy Bogard

I'm a technical architect with Headspring in Austin, TX. I focus on DDD, distributed systems, and any other acronym-centric design/architecture/methodology. I created AutoMapper and am a co-author of the ASP.NET MVC in Action books.
This entry was posted in AutoMapper. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • http://devlicio.us Billy McCafferty

    Thanks for sharing Jimmy! I intend on using AutoMapper more on future projects…it’s great to see examples of its use with S#arp Architecture as well.

  • Tad

    Hi,
    I would like to use automapper like tool in mapping from my domain objects to MongoDB Documents (document oriented database),
    is is possible to extend Automapper in that way ? I would really like to use MongoDB in my future project but i don’t want to deal with magic strings which i have to use if i user mongoDB c# driver

  • http://www.lostechies.com/members/bogardj/default.aspx bogardj

    @Tad

    I’ve never used MongoDB, but I did get a patch to be able to map from IDataReader/DataSets.

  • Sam

    Trying to follow this… did you end up making a EditModelEntityMapper and OutputViewModelMapper for each Entity class * NoOfViewModel Classes for that entity?

    LLBLGen has made me lazy :(

  • http://www.lostechies.com/members/bogardj/default.aspx bogardj

    @Sam

    I dunno – it wasn’t my success story. Try Howard’s original post, I’m sure he can clear it up!

  • Petr

    Seems we’ll use AutoMapper as well. Thanks for such nice piece of software, Jimmy!

    See detailed info http://stackoverflow.com/questions/1734599/nhibernate-set-entity-from-transient-object-based-on-unique-field