There’s certain mundane thing’s I do often in my day to day development life that I find myself wondering, if I were to log every minute of this, how many hours/days/months/years will I have wasted over the course of my life.
For example, how many total days of my life will be spent waiting around for my code to compile, I’m estimate it to be depressingly high, but unfortunately this is something that is unavoidable so I take it on the chin.
Writing object to object mapping code is another task I find hugely mundane, It’s obviously important work in a multi layered architecture, but it takes what seems to be forever and requires little to no brain power, there has got to be a better way, a team of trained monkeys perhaps? Perhaps not.
Automapper is an open source object to object property mapping library which has not only saved me incredible amounts of time, which I‘ve been able better spend solving more interesting problems, but it has also greatly reduced the amount of mundane code in my code base.
“AutoMapper uses a fluent configuration API to define an object-object mapping strategy. AutoMapper uses a convention-based matching algorithm to match up source to destination values. Currently, AutoMapper is geared towards model projection scenarios to flatten complex object models to DTOs and other simple objects, whose design is better suited for serialization, communication, messaging, or simply an anti-corruption layer between the domain and application layer.”
Basic Mapping
So let’s jump in and have a look at how easy it is to get going with AutoMapper. Lets say for the case of this exercise we have a Song object which contains a reference to an Album object, which in turn contains a reference to a Artist object.
{
public int Id { get; set; }
public string Name { get; set; }
public TimeSpan Duration { get; set;}
public IAlbum Album { get; set; }
}
What we’re trying to do is map the properties on the Song object and the related Album and Artist objects to a View Model object that will be used to bind the information to some sort of view. Traditionally we would have achieved this by newing up a new View Model object and manually mapping the fields that are of interest to us,
{
Song song = (Song)_musicRepository.GetSongById(songId);
return new SongViewModel()
{
Name = song.Name,
Duration = song.Duration,
AlbumName = song.Album.Name
};
}
And this seems pretty straight forward and painless, but in a large domain model, containing many many more objects that require mapping between business objects and view models, dto’s and business objects, and any other combination of these, we can often find ourselves writing what seems like endless amounts of mapping code.
Using AutoMapper, with two lines of code, we can automatically map our properties and build up a new SongViewModel object based on the ISong object we wish to map to it.
{
ISong song = _musicRepository.GetSongById(songId);
Mapper.CreateMap<ISong, SongViewModel>();
return Mapper.Map<ISong, SongViewModel>(song);
}
So what’s happened here is we’ve told AutoMapper that we want create a map between the properties of the object ISong and the properties of the object SongViewModel and secondly, we’ve passed in an object of type ISong and Automapper has spat out an object of type SongViewModel. It’s achieved this by assuming that our naming conventions on both objects are the same.
We passed in the ISong object, which was represented as this,
And we got back our SongViewModel object with our properties mapped as we would expect them to have been mapped had we done it manually.
What’s most interesting about this mapping is the fact that our SongViewModel has a property named AlbumName, where our ISong object does not, rather it contains a reference to an IAlbum object. Automapper has worked out from our naming convention that the AlbumName property must mapped to the Name property on the Album object. This is all for free, we didn’t need to explicitly tell AutoMapper anything about this relationship.
Getting A Little More Complex
What if we had a requirement to pass up the artists name as a property of the SongViewModel object as well? We know that we can get the artists name from the relationships of Song to Album and Album to Artist, but how do we tell AutoMapper to go into our object relationship graph to pull out this value and map it to the property on our View Model?
{
ISong song = _musicRepository.GetSongById(songId);
Mapper.CreateMap<ISong, SongViewModel>()
.ForMember(svm => svm.ArtistName, s => s.MapFrom(a => a.Album.Artist.Name));
return Mapper.Map<ISong, SongViewModel>(song);
}
What we’ve had to do is add some extra information when we call, CreateMap. What’s happening is fluently we’re telling AutoMapper that for the Field ArtistName on the SongViewModel object, map it to the value contained in the Name property of the Artist object which is related to the Album object, related to the Song object.
And the resulting View Model object has it’s fields populated just as you would expect it to,
So in just two lines of code, AutoMapper has allowed us to flatten out a graph of objects three levels deep and represent the information in a meaningful View Model object that can be passed to the interface.
The time this is going to save me as a developer is unfathomable, time I can now spend focusing on more important/interesting problems.
Buy:Tramadol.Cialis Super Active+.Cialis Soft Tabs.Propecia.Viagra.Zithromax.Viagra Professional.Viagra Super Force.Super Active ED Pack.VPXL.Cialis Professional.Cialis.Soma.Maxaman.Levitra.Viagra Super Active+.Viagra Soft Tabs….