Wednesday, January 20, 2010

Diving a Little Deeper into AutoMapper Part 2

Okay, so what I’m going to do in this post is actually do the opposite of what I did in the last post. This method is actually a better practice because you would go from domain model to view model. I’ll be using the same domain & view model from Part 1.

So, let’s dive in…here is my DomainToViewProfile:

public class DomainToViewProfile : Profile
public const string NameOfProfile = "DomainToViewProfile";
protected override string ProfileName
get { return NameOfProfile; }

protected override void Configure()

private void CreateMaps()
CreateMap<Person, PersonView>()
.ForMember(dest => dest.Gender, opts => opts.MapFrom(src =>
.ForMember(dest => dest.BirthDate, opts => opts.MapFrom(src =>
.ForMember(dest => dest.Ethnicity, opts => opts.MapFrom(src =>
.ForMember(dest => dest.HomePhoneNumber, opts => opts.MapFrom(src =>
.ForMember(dest => dest.MailingAddressCity, opts => opts.MapFrom(src =>
.ForMember(dest => dest.MailingAddressPostalCode, opts => opts.MapFrom(src =>
.ForMember(dest => dest.MailingAddressState, opts => opts.MapFrom(src =>
.ForMember(dest => dest.MailingAddressStreetLine, opts => opts.MapFrom(src =>

Okay, so obviously this one is MUCH more basic than going the other route. You’ll notice that I didn’t have to map the ContactInfoEmailAddress because AutoMapper is smart enough to map it based on the naming convention.

AutoMapper also knows to map things like GetWhatever() to flattened views like the GetAge() on the Demographics class is not mapped above, but it is mapped! Very cool!

Another interesting thing is that I didn’t have to map PersonName or Gender AND I don’t have a converter for either of them…so what does AutoMapper do? It calls the ToString()…pretty smart AutoMapper!

On to the tests! I setup the fixturesetup and all that like I did in Part 1, so I won’t re-paste it here.

So first thing I tested was the demographics mapping and it looks like this:

public void personview_demographics_should_equal_person_demographics()
var person = new Person();
person.Demographics.Gender =
person.Demographics.BirthDate =
new DateTime(1990, 1, 1);
person.Demographics.Ethnicity =
var personview = Mapper.Map<Person, PersonView>(person);

Assert.AreEqual(personview.Gender, "Male");
Assert.AreEqual(personview.BirthDate, person.Demographics.BirthDate);
Assert.AreEqual(personview.Ethnicity, person.Demographics.Ethnicity);
Assert.AreEqual(personview.DemographicsAge, 20);

Then person name like this:

public void personview_name_should_equal_person_name()
var person = new Person();
person.PersonName =
new PersonName {FirstName = "John", MiddleName = "Test", LastName = "Smith"};

var personview = Mapper.Map<Person, PersonView>(person);

Assert.AreEqual(person.PersonName.ToString(), personview.PersonName);

And finally address like this:

public void personview_address_should_equal_person_address()
var person = new Person();
person.ContactInfo.MailingAddress.StreetLine =
"1234 Test St.";
person.ContactInfo.MailingAddress.City =
person.ContactInfo.MailingAddress.State =
person.ContactInfo.MailingAddress.PostalCode =
person.ContactInfo.EmailAddress =
person.ContactInfo.HomePhoneNumber =

var personview = Mapper.Map<Person, PersonView>(person);

Assert.AreEqual(personview.ContactInfoEmailAddress, person.ContactInfo.EmailAddress);
Assert.AreEqual(personview.HomePhoneNumber, person.ContactInfo.HomePhoneNumber);
Assert.AreEqual(personview.MailingAddressStreetLine, person.ContactInfo.MailingAddress.StreetLine);
Assert.AreEqual(personview.MailingAddressCity, person.ContactInfo.MailingAddress.City);
Assert.AreEqual(personview.MailingAddressState, person.ContactInfo.MailingAddress.State);
Assert.AreEqual(personview.MailingAddressPostalCode, person.ContactInfo.MailingAddress.PostalCode);

It’s funny because typically I think on Part 2 of a two part lesson the second part would be more difficult…apparently that’s not how I operate :)

So there you have it…AutoMapper from domain model to view model. Good stuff!

Thanks for reading!

Shout it

kick it on

blog comments powered by Disqus
Related Posts Plugin for WordPress, Blogger...