0

我还没有依赖 Mapster 或 AutoMapper。现在我正在使用手写映射,因为我找不到可以用更小的代码做到这一点的映射器。

问题是我们如何将扁平结构映射到复杂对象?我认为很多人都可以从这样一个复杂对象的良好映射示例中受益。CopyOfficeAddressAsInvoiceAddress根据是否需要将办公地址复制为发票地址,我什至有一个映射条件。我到处看了看,但无法让它工作。

也许我也应该使用不同的命名来使映射算法更清晰?!

最大的问题是这样的地图是否可以由映射器解决,或者这是否复杂?我见过的所有演示都使用彼此非常相似的 dto 和模型对象。我没有得到将一个对象映射到另一个 99% 相似的对象的意义。

我有一个Command(我正在使用 Mediatr),如下所示:

public class Command  : IRequest<IActionResult>
{
            public string AccountName { get; set; }

            public string ContactFirstName { get; set; } 
            public string ContactLastName { get; set; } 
            public string ContactEMail { get; set; } 
            public string ContactPhoneNumber { get; set; } 

            public string BankAccount { get; set; } 
            public string Bank { get; set; } 

            public string OfficeName { get; set; } 

            public string OfficeAddressStreet { get; set; } 
            public int OfficeAddressStreetNumber { get; set; } 
            public string? OfficeAddressStreetNumberAddition { get; set; } 
            public string OfficeAddressPostalcode { get; set; } 
            public string OfficeAddressCity { get; set; } 
            public string OfficeAddressCountry { get; set; } 

            public string? OfficeInvoiceAddressStreet { get; set; }  = null;
            public int? OfficeInvoiceAddressStreetNumber { get; set; }  = null;
            public string? OfficeInvoiceAddressStreetNumberAddition { get; set; }  = null;
            public string? OfficeInvoiceAddressPostalcode { get; set; }  = null;
            public string? OfficeInvoiceAddressCity { get; set; }  = null;
            public string? OfficeInvoiceAddressCountry { get; set; }  = null;

            //[Ignore]
            public bool? CopyOfficeAddressAsInvoiceAddress  { get; set; } = false;
            
            public string? AssociationIdentifier { get; set; } = null;
}

我希望它映射到以下内容models

public class Account
    {
        public int Id { get; set; }

        public string AccountName { get; set; }

        public IList<Contact> Users { get; set; }

        public IList<Office> Offices { get; set; }

        public string Bank { get; set; }
        public string BankAccount { get; set; }

        public string? AssociationIdentifier { get; set; }
}
public class Office
    {
        public int Id { get; set; }

        public string Name { get; set; }
        public Address ContactAddress { get; set; }
        public Address InvoiceAddress { get; set; }
        public bool HeadQuarter { get; set; }
    }
public class Address
    {
        public int Id { get; set; }

        public string Street { get; set; }
        public string Postalcode { get; set; }
        public int StreetNumber { get; set; }
        public string StreetNumberAddition { get; set; }
        public string City { get; set; }
        public string Country { get; set; }
    }
public class Contact
    {
        public int Id { get; set; }

        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string EMail { get; set; }
        public string PhoneNumber { get; set; }
   }
4

1 回答 1

1

首先,我的经验主要是使用Automapper,像这样映射复杂类型肯定是可以的。

但是您的命令不需要完全平坦。DTO 与您的域模型相似并没有本质上的错误。使用 Automapper 这相当容易,因为具有相同名称的属性是 1:1 映射的。

可能是您提交了一个表单,其中所有属性都平展在一个对象中。在这种情况下,您可以为该对象和每个域对象定义一个单独的映射。

CreateMap<AccountDto, Account>(); // mapping logic omitted
CreateMap<AccountDto, Office>();
...

或者,您可以使用元组将一个对象映射到一系列对象。

CreateMap<AccountDto, (Account, Office, ...)>(); // mapping logic omitted

但是,如果您定义单独的 DTO 并为它们制作映射配置文件,它可能会简化您的整个映射体验。对于复制地址,在这种情况下,您可以简单地执行类似的操作。

if (copyAddress) 
{
    office.InvoiceAddress = _mapper.Map<Address>(addressDto);
}
于 2021-12-28T15:43:41.180 回答