Как использовать AutoMapper для отображения моделей с наследованием и вложенными моделями - PullRequest
1 голос
/ 31 октября 2011

У меня есть объект Address с 2 подтипами. Вот мой упрощенный код:

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 и CaLocation в AddressModel ...

1 Ответ

1 голос
/ 31 октября 2011

Я понял это.Вот упрощенная версия моей конфигурации 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);
...