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

发布于 2024-12-12 21:39:15 字数 1507 浏览 3 评论 0原文

我有一个具有 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>();

我不知道如何解析 AddressModel 上的 UsLocationCaLocation 属性...

I have an Address entity with 2 sub types. Here's my simplified code:

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; }
}

Here's my simplified view models:

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; }
}

Here's my simplified AutoMapper config:

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

I can't figure out how to resolve the UsLocation and CaLocation properties on AddressModel...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寒冷纷飞旳雪 2024-12-19 21:39:15

我想通了。这是我的 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);

I figured it out. Here's a simplified version of my Automapper config:

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

Here's a simplified sample test:

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文