使用AutoMapper从列表向下映射到对象 [英] Mapping from list down to object with AutoMapper

查看:87
本文介绍了使用AutoMapper从列表向下映射到对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是AutoMapper的新手,有一个我要解决的问题.

I'm new with AutoMapper and have a problem I'm trying to solve.

如果我有这样的源类:

public class Membership
{
    public int MembershipId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string OrganizationName { get; set; }
    public List<Address> Addresses { get; set; }
}

Address类如下:

And the Address class looks like this:

public class Address
{
    public int AddressId{ get; set; }
    public int RefAddressTypeId { get; set; }
    public string AddressLine1 { get; set; }
    public string AddressLine2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Zip { get; set; }
    public bool IsPreferredAddress { get; set; }
}

我的目的地是:

public class UserInformationModel
{
    public string UserName { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Organization { get; set; }
    public string EmailAddress { get; set; }
    public PhysicalAddress BillingAddress { get; set; }
    public PhysicalAddress ShippingAddress { get; set; }
}

目标地址类别为:

public class PhysicalAddress
{
    public AddressType AddressType{get; set;}
    public string AddressLine1 { get; set; }
    public string AddressLine2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string PostalCode { get; set; }

}

我已经建立了这样的映射:

I have set up a mapping like this:

Mapper.CreateMap<MinistryMattersIntegration.BusinessObjects.Entities.Cokesbury.Membership, UserInformationModel>()
      .ForMember(dest => dest.Organization, opt => opt.MapFrom(src=>src.OrganizationName));

这适用于UserInformationModel的成员资格,但是现在我需要获取地址.但是,要注意的一件事是目的地是一个帐单地址和一个送货地址,而在原始模型中,所有地址都存储为一个列表.从列表中查找送货地址和帐单地址的方法是查看RefAddressTypdId和IsPreferredAddress.只能存在一个带有特定RefAddressTypeId的首选地址.

This is working for Membership to UserInformationModel, but now I need to get addresses working. One important thing to note, though, is that the destination is a single billing address and a single shipping address while in the original model, all the address are stored as a list. The way you find the shipping and billing addresses out of the list is by looking at the RefAddressTypdId and the IsPreferredAddress. Only one preferred address may exist with a particular RefAddressTypeId.

所以,我的问题是,如何使AutoMapper进行这种映射?有可能吗?还是我最好只使用常规的映射代码?

So, my question is, how do you get AutoMapper to do this kind of mapping? Is it possible, or am I better off just going with regular mapping code?

推荐答案

您将要使用

You'll want to use the Custom Value Resolvers feature of AutoMapper. So you'd setup a Custom Resolver to map from your list to your single entity using the IsPreferredAddress flag to find it.

该文档对于自定义解析器"非常有用,因此您应该从那里确定它.

The documentation is pretty good for the Custom Resolvers so you should be fine figuring it out from there.

这篇关于使用AutoMapper从列表向下映射到对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆