2011-10-31 1 views
1

2 개의 하위 유형이있는 Address 엔터티가 있습니다. 여기 내 간단한 코드입니다 :AutoMapper를 사용하여 상속 모델과 중첩 모델이있는 모델에 매핑하는 방법

여기
public class Address { 
    public string Street1 { get; set; } 
    public string Country { get; set; } 
} 
public class UsAddress : Address { 
    public string State { get; set; } 
} 
public class CandianAddress : Address { 
    public string Providence { get; set; } 
} 

의 내 단순화보기 모델 : 여기

public class LocationModel { 
    public string Street1 { get; set; } 
} 
public class UsLocationModel : LocationModel { 
    public string State { get; set; } 
} 
public class CaLocationModel : LocationModel { 
    public string Providence { get; set; } 
} 
public class AddressModel { 
    public int? Country { get; set; } 
    public UsLocationModel UsLocation { get; set; } 
    public CaLocationModel CaLocation { get; set; } 
} 

의 내 단순화 AutoMapper의 설정 :

Mapper.CreateMap<Address, AddressModel>() 
      .Include<UsAddress, AddressModel>() 
      .Include<CanadianAddress, AddressModel>(); 
Mapper.CreateMap<UsAddress, AddressModel>(); 
Mapper.CreateMap<CanadianAddress, AddressModel>(); 
Mapper.CreateMap<Address, LocationModel>() 
      .Include<UsAddress, USLocationModel>() 
      .Include<CanadianAddress, CALocationModel>(); 
Mapper.CreateMap<UsAddress, USLocationModel>(); 
Mapper.CreateMap<CanadianAddress, CALocationModel>(); 

내가 해결하는 방법을 알아낼 수 없습니다 UsLocationCaLocation 등록 정보 AddressModel ...

답변

1

나는 그것을 알아 냈다. 여기 내 Automapper 설정의 단순화 된 버전입니다 : 여기

Mapper.CreateMap<Address, AddressModel>() 
    .Include<UsAddress, AddressModel>() 
    .Include<CanadianAddress, AddressModel>() 
    .ForMember(x => x.USLocation, a => a.Ignore()) 
    .ForMember(x => x.CALocation, a => a.Ignore()) 
    .ForMember(x => x.Country, a => a.ResolveUsing<HaveIdValueResolver<Country, int>>().FromMember(x => x.Country); 
Mapper.CreateMap<UsAddress, AddressModel>() 
    .ForMember(x => x.USLocation, a => a.MapFrom(Mapper.Map<UsAddress, USLocationModel>)) 
    .ForMember(x => x.CALocation, a => a.Ignore()) 
    .ForMember(x => x.Country, a => a.ResolveUsing<HaveIdValueResolver<Country, int>>().FromMember(x => x.Country)); 
Mapper.CreateMap<CanadianAddress, AddressModel>() 
    .ForMember(x => x.USLocation, a => a.Ignore()) 
    .ForMember(x => x.CALocation, a => a.MapFrom(Mapper.Map<CanadianAddress, CALocationModel>)) 
    .ForMember(x => x.Country, a => a.ResolveUsing<HaveIdValueResolver<Country, int>>().FromMember(x => x.Country)); 
Mapper.CreateMap<Address, LocationModel>() 
    .Include<UsAddress, USLocationModel>() 
    .Include<CanadianAddress, CALocationModel>(); 
Mapper.CreateMap<UsAddress, USLocationModel>(); 
Mapper.CreateMap<CanadianAddress, CALocationModel>(); 

이 있어요 단순화 된 샘플 테스트 :

var usAddress = new FakeUsAddress("111 L St", new FakeState(id: 17)); 
var addressModel = Mapper.Map<UsAddress, AddressModel>(usAddress); 
Assert.IsNotNull(addressModel.USLocationModel); 
Assert.IsNull(addressModel.CALocationModel); 
Assert.AreEqual(usAddress.Street1, addressModel.USLocationModel.Street1); 
Assert.AreEqual(usAddress.State.Id, addressModel.USLocationModel.State); 
Assert.AreEqual(usAddress.Country.Id, addressModel.USLocationModel.Country);