如何使用AutoMapper映射到具有继承和嵌套模型的模型

时间:2011-10-31 15:36:56

标签: entity viewmodel automapper

我有一个包含2个子类型的Address实体。这是我的简化代码:

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>();

我无法弄清楚如何解决UsLocation上的CaLocationAddressModel属性......

1 个答案:

答案 0 :(得分: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);