Reputation: 2535
I have complex model (SyncBillToPartyMaster) and I want to customize the mapping into my simple POCO class.
Mapper.CreateMap<SyncBillToPartyMaster, CustomerAddress>()
.ForMember(d => d.CustomerId, o => o.MapFrom(src => src.DataArea.BillToPartyMaster.CustomerParty.PartyIDs.ID.Value))
.ForMember(d => d.CustomerAddressId, o => o.MapFrom(src => src.DataArea.BillToPartyMaster.PartyIDs.ID.Value))
.ForMember(d => d.City, o => o.MapFrom(src => src.DataArea.BillToPartyMaster.Location.Address.CityName))
.ForMember(d => d.State, o => o.MapFrom(src => src.DataArea.BillToPartyMaster.Location.Address.CountrySubDivisionCode.Value))
.ForMember(d => d.Country, o => o.MapFrom(src => src.DataArea.BillToPartyMaster.Location.Address.CountryCode.Value))
.ForMember(d => d.Zip, o => o.MapFrom(src => src.DataArea.BillToPartyMaster.Location.Address.PostalCode.Value))
.ForMember(d => d.Address1, o => o.MapFrom(src => src.DataArea.BillToPartyMaster.Location.Address.AddressLine[0].Value))
.ForMember(d => d.Address2, o => o.MapFrom(src => src.DataArea.BillToPartyMaster.Location.Address.AddressLine[1].Value))
.ForMember(d => d.Address3, o => o.MapFrom(src => src.DataArea.BillToPartyMaster.Location.Address.AddressLine[2].Value))
.ForMember(d => d.Phone1, o => o.MapFrom(src => GetContact(src.DataArea.BillToPartyMaster, "phone")))
.ForMember(d => d.Fax1, o => o.MapFrom(src => GetContact(src.DataArea.BillToPartyMaster, "fax")))
.ForMember(d => d.MaintenanceCustomerId, o => o.MapFrom(src => src.DataArea.BillToPartyMaster.LastModificationPerson.IDs[0].Value))
.ForMember(d => d.MaintenanceUser, o => o.MapFrom(src => src.DataArea.BillToPartyMaster.LastModificationPerson.Name.Value))
.ForMember(d => d.MaintenanceDate, o => o.MapFrom(src => DateTime.UtcNow));
As you can see, it is quite tedious to map my complex model SyncBillToPartyMaster to CustomerAddress using the ForMember method of AutoMapper. Is there any alternative way to make it elegant aside from using the ForMember method?
By the way, I still have more, and much complex, models aside from SyncBillToPartyMaster. I don't want to do the them same way if there is another way to accomplish my goal.
Upvotes: 1
Views: 1221
Reputation: 2535
I read a few context in Automapper and I found ITypeConverter<in T, out TU>
interface where I can implement to my custom mapper class and do my custom mapping. And for registering my Custom mapper I used Mapper.CreateMap<T,TU>().ConvertUsing()
.
Here was my custom mapper :
public class CustomerAddressBillToPartyMasterMapper : ITypeConverter<SyncBillToPartyMaster, CustomerAddress>
{
public CustomerAddress Convert(ResolutionContext context)
{
var syncBillToPartyMaster = context.SourceValue as SyncBillToPartyMaster;
if (syncBillToPartyMaster == null)
return null;
var customerAddressesSource = syncBillToPartyMaster.DataArea.BillToPartyMaster;
return new CustomerAddress
{
CustomerId = customerAddressesSource.CustomerParty.PartyIDs.ID.Value,
CustomerAddressId = customerAddressesSource.PartyIDs.ID.Value,
City = customerAddressesSource.Location.Address.CityName,
State = customerAddressesSource.Location.Address.CountrySubDivisionCode.Value,
Country = customerAddressesSource.Location.Address.CountryCode.Value,
Zip = customerAddressesSource.Location.Address.PostalCode.Value,
Address1 = customerAddressesSource.Location.Address.AddressLine[0].Value,
Address2 = customerAddressesSource.Location.Address.AddressLine[1].Value,
Address3 = customerAddressesSource.Location.Address.AddressLine[2].Value,
Phone1 = GetContact(customerAddressesSource, "phone"),
Fax1 = GetContact(customerAddressesSource, "fax"),
MaintenanceCustomerId = customerAddressesSource.LastModificationPerson.IDs[0].Value,
MaintenanceUser = customerAddressesSource.LastModificationPerson.Name.Value,
MaintenanceDate = DateTime.UtcNow
};
}
}
And this was how I register it :
Mapper.CreateMap<SyncBillToPartyMaster, CustomerAddress>().ConvertUsing(new CustomerAddressBillToPartyMasterMapper());
I think this was much cleaner than my previous code I posted.
Upvotes: 0
Reputation: 26785
Don't use AutoMapper, there's nothing that matches up here. You're not saving anything over just using a "new" operator and setting everything yourself.
Upvotes: 13