Skip to content

Instantly share code, notes, and snippets.

@thunklife
Created September 24, 2012 14:05
Show Gist options
  • Select an option

  • Save thunklife/3776115 to your computer and use it in GitHub Desktop.

Select an option

Save thunklife/3776115 to your computer and use it in GitHub Desktop.
Mapping a Collection of Interfaces With Their Own Unique Properties
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AutoMapper;
using NUnit.Framework;
namespace InterfaceMapping
{
[TestFixture]
public class PersonViewModelTests
{
const string First = "Joe";
const string Last = "Schmoe";
const string DOB = "01/01/1980";
const string HomePhone = "555-555-5555";
public PersonViewModelTests()
{
Mapper.CreateMap<PersonViewModel, Person>().ForAllMembers(mapper=>mapper.Ignore());
Mapper.CreateMap<NameInformation, Person>()
.ForMember(x => x.DateOfBirth, mapper => mapper.Ignore())
.ForMember(x => x.HomePhoneNumber, mapper => mapper.Ignore())
.ForMember(x => x.CellPhoneNumber, mapper => mapper.Ignore())
.ForMember(x => x.WorkPhoneNumber, mapper => mapper.Ignore());
Mapper.CreateMap<DateOfBirthInformation, Person>()
.ForMember(x => x.FirstName, mapper => mapper.Ignore())
.ForMember(x => x.LastName, mapper => mapper.Ignore())
.ForMember(x => x.HomePhoneNumber, mapper => mapper.Ignore())
.ForMember(x => x.CellPhoneNumber, mapper => mapper.Ignore())
.ForMember(x => x.WorkPhoneNumber, mapper => mapper.Ignore());
Mapper.CreateMap<PhoneNumberInformation, Person>()
.ForMember(x => x.FirstName, mapper => mapper.Ignore())
.ForMember(x => x.LastName, mapper => mapper.Ignore())
.ForMember(x => x.DateOfBirth, mapper => mapper.Ignore());
Mapper.AssertConfigurationIsValid();
}
[Test]
public void Should_Map_To_Person()
{
var nameInfo = new NameInformation
{
FirstName = First,
LastName = Last
};
var dateInfo = new DateOfBirthInformation
{
DateOfBirth = DOB
};
var phoneInfo = new PhoneNumberInformation
{
HomePhoneNumber = HomePhone
};
var viewModel = new PersonViewModel()
{
PageTitle = "Page",
Fields = new List<IPageField>
{
nameInfo,
dateInfo,
phoneInfo
}
};
var person = Mapper.Map<PersonViewModel, Person>(viewModel);
Assert.IsNotNull(person);
Assert.AreEqual(First, person.FirstName);
Assert.AreEqual(Last,person.LastName);
Assert.AreEqual(DOB, person.DateOfBirth);
Assert.AreEqual(HomePhone, person.HomePhoneNumber);
}
}
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string DateOfBirth { get; set; }
public string HomePhoneNumber { get; set; }
public string WorkPhoneNumber { get; set; }
public string CellPhoneNumber { get; set; }
}
public class PersonViewModel
{
public string PageTitle { get; set; }
public IEnumerable<IPageField> Fields { get; set; }
}
public interface IPageField
{
bool Matches(PageName pageName);
int Order { get; }
}
public class NameInformation : IPageField
{
public bool Matches(PageName pageName)
{
return pageName == PageName.PersonalInformation;
}
public int Order
{
get { return 0; }
}
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class DateOfBirthInformation : IPageField
{
public bool Matches(PageName pageName)
{
return pageName == PageName.PersonalInformation;
}
public int Order
{
get { return 1; }
}
public string DateOfBirth { get; set; }
}
public class PhoneNumberInformation : IPageField
{
public bool Matches(PageName pageName)
{
return pageName == PageName.PersonalInformation;
}
public int Order
{
get { return 2; }
}
public string HomePhoneNumber { get; set; }
public string WorkPhoneNumber { get; set; }
public string CellPhoneNumber { get; set; }
}
public enum PageName
{
PersonalInformation,
OtherInformation
}
}
public class PersonConverter : ITypeConverter<PersonViewModel, Person>
{
public Person Convert(ResolutionContext context)
{
var person = new Person();
var vm = (PersonViewModel) context.SourceValue;
foreach (var field in vm.Fields)
{
Mapper.Map(field, person);
}
return person;
}
}
//Mapping stuff
...
Mapper.CreateMap<PersonViewModel, Person>().ConvertUsing(new PersonConverter());
Mapper.CreateMap<IPageField, Person>()
.Include<NameInformation, Person>()
.Include<DateOfBirthInformation, Person>()
.Include<PhoneNumberInformation, Person>()
.ForAllMembers(x=>x.Ignore());
Mapper.CreateMap<NameInformation, Person>();
Mapper.CreateMap<DateOfBirthInformation, Person>();
Mapper.CreateMap<PhoneNumberInformation, Person>();
Mapper.AssertConfigurationIsValid();
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment