如何映射对象

本文关键字:对象 映射 何映射 | 更新日期: 2023-09-27 18:02:10

这是我的绑定当前的样子:

MerchantAccountRequest request = new MerchantAccountRequest
                {
                    Individual = new IndividualRequest
                    {
                        FirstName = merchant.MerchantIndividual.FirstName,
                        LastName = merchant.MerchantIndividual.LastName,
                        Email = merchant.MerchantIndividual.Email,
                        Phone = merchant.MerchantIndividual.Phone,
                        DateOfBirth = merchant.MerchantIndividual.DateOfBirth,
                        Ssn = merchant.MerchantIndividual.Ssn,
                        Address = new AddressRequest
                        {
                            StreetAddress = merchant.MerchantIndividual.StreetAddress,
                            Locality = merchant.MerchantIndividual.Locality,
                            Region = merchant.MerchantIndividual.Region,
                            PostalCode = merchant.MerchantIndividual.PostalCode
                        }
                    },
                    Business = new BusinessRequest
                    {
                        LegalName = merchant.MerchantBusiness.LegalName,
                        DbaName = merchant.MerchantBusiness.DbaName,
                        TaxId = merchant.MerchantBusiness.TaxId,
                        Address = new AddressRequest
                        {
                            StreetAddress = merchant.MerchantBusiness.StreetAddress,
                            Locality = merchant.MerchantBusiness.Locality,
                            Region = merchant.MerchantBusiness.Region,
                            PostalCode = merchant.MerchantBusiness.PostalCode
                        }
                    },
                    Funding = new FundingRequest
                    {
                        Descriptor = merchant.MerchantFunding.Descriptor,
                        Destination = FundingDestination.BANK,
                        Email = merchant.MerchantFunding.Email,
                        MobilePhone = merchant.MerchantFunding.MobilePhone,
                        AccountNumber = merchant.MerchantFunding.AccountNumber,
                        RoutingNumber = merchant.MerchantFunding.RoutingNumber
                    },
                    TosAccepted = merchant.TosAccepted,
                    MasterMerchantAccountId = merchant.MasterMerchantAccountId,
                    Id = merchant.MerchantId
                };

我需要使用AutoMapper来实现以上。

这是我尝试过的:

CreateMapperProfile

编辑:

    CreateMap<Classes.Merchant, MerchantAccountRequest>()
        .ForMember(dest => dest.Individual, source => source.MapFrom(s => s.MerchantIndividual))
        .ForMember(dest => dest.Business, source => source.MapFrom(s => s.MerchantBusiness))
        .ForMember(dest => dest.Funding, source => source.MapFrom(s => s.MerchantFunding))
        .ForMember(dest => dest.Id, source => source.MapFrom(s => s.MerchantId));

这是Merchant类:

public partial class Merchant :
    {
        public int Id { get; set; }
        public string MerchantId { get; set; }
        public virtual MerchantIndividual MerchantIndividual { get; set; }
        public virtual MerchantBusiness MerchantBusiness { get; set; }
        public virtual MerchantFunding MerchantFunding { get; set; }
        public bool TosAccepted { get; set; }
        public string MasterMerchantAccountId { get; set; }
        public bool isSubMerchant { get; set; }
     }

和映射;

编辑:

        MerchantAccountRequest request = _mapper.Map<MerchantAccountRequest>(merchant);
        request.Individual = _mapper.Map<IndividualRequest>(merchant.MerchantIndividual);
        request.Business = _mapper.Map<BusinessRequest>(merchant.MerchantBusiness);
        request.Funding = _mapper.Map<FundingRequest>(merchant.MerchantFunding);

上面的第一行代码MerchantAccountRequest request = _mapper.Map<MerchantAccountRequest>(merchant);可以完成所有的映射吗?

. ...... .

如何创建正确的映射?

如何映射对象

您不需要在request的属性上调用_mapper.Map

只要调用MerchantAccountRequest request = _mapper.Map<MerchantAccountRequest>(merchant);,假设你有每种类型的映射,你应该没问题。

我认为下面的事情应该会让你走上正确的道路。

class Program
{
    static void Main(string[] args)
    {
        var config = new MapperConfiguration(cfg => {
            cfg.CreateMap<Merchant, MerchantAccountRequest>()
                .ForMember(dest => dest.Individual, c => c.MapFrom(source => source.MerchantIndividual));
            cfg.CreateMap<MerchantIndividual, IndividualRequest>();
        });
        config.AssertConfigurationIsValid();
        var mapper = config.CreateMapper();
        var merchant = new Merchant
        {
            Id = 1,
            MerchantIndividual = new MerchantIndividual { FirstName = "John Doe" }
        };
        var merchantAccountRequest = mapper.Map<Merchant, MerchantAccountRequest>(merchant);            
    }
}
public class Merchant
{
    public int Id { get; set; }
    public MerchantIndividual MerchantIndividual { get; set; }
}
public class MerchantIndividual
{
    public string FirstName { get; set; }
}
public class MerchantAccountRequest
{
    public int Id { get; set; }
    public IndividualRequest Individual { get; set; }
}
public class IndividualRequest
{
    public string FirstName { get; set; }
}