简体   繁体   English

除了AutoMapper中的ForMember方法以外,还有其他自定义映射的方法吗?

[英]Is there any alternative way of custom mapping aside from the ForMember method in AutoMapper?

I have complex model (SyncBillToPartyMaster) and I want to customize the mapping into my simple POCO class. 我有一个复杂的模型(SyncBillToPartyMaster),我想自定义到我的简单POCO类的映射。

Mapper.CreateMap<SyncBillToPartyMaster, CustomerAddress>()
.ForMember(d => d.CustomerId, o => o.MapFrom(src => src.DataArea.BillToPartyMaster.CustomerParty.PartyIDs.ID.Value))
.ForMember(d => d.CustomerAddressId, o => o.MapFrom(src => src.DataArea.BillToPartyMaster.PartyIDs.ID.Value))
.ForMember(d => d.City, o => o.MapFrom(src => src.DataArea.BillToPartyMaster.Location.Address.CityName))
.ForMember(d => d.State, o => o.MapFrom(src => src.DataArea.BillToPartyMaster.Location.Address.CountrySubDivisionCode.Value))
.ForMember(d => d.Country, o => o.MapFrom(src => src.DataArea.BillToPartyMaster.Location.Address.CountryCode.Value))
.ForMember(d => d.Zip, o => o.MapFrom(src => src.DataArea.BillToPartyMaster.Location.Address.PostalCode.Value))
.ForMember(d => d.Address1, o => o.MapFrom(src => src.DataArea.BillToPartyMaster.Location.Address.AddressLine[0].Value))
.ForMember(d => d.Address2, o => o.MapFrom(src => src.DataArea.BillToPartyMaster.Location.Address.AddressLine[1].Value))
.ForMember(d => d.Address3, o => o.MapFrom(src => src.DataArea.BillToPartyMaster.Location.Address.AddressLine[2].Value))
.ForMember(d => d.Phone1, o => o.MapFrom(src => GetContact(src.DataArea.BillToPartyMaster, "phone")))
.ForMember(d => d.Fax1, o => o.MapFrom(src => GetContact(src.DataArea.BillToPartyMaster, "fax")))
.ForMember(d => d.MaintenanceCustomerId, o => o.MapFrom(src => src.DataArea.BillToPartyMaster.LastModificationPerson.IDs[0].Value))
.ForMember(d => d.MaintenanceUser, o => o.MapFrom(src => src.DataArea.BillToPartyMaster.LastModificationPerson.Name.Value))
.ForMember(d => d.MaintenanceDate, o => o.MapFrom(src => DateTime.UtcNow));

As you can see, it is quite tedious to map my complex model SyncBillToPartyMaster to CustomerAddress using the ForMember method of AutoMapper. 如您所见,使用AutoMapper的ForMember方法将我的复杂模型SyncBillToPartyMaster映射到CustomerAddress非常繁琐。 Is there any alternative way to make it elegant aside from using the ForMember method? 除了使用ForMember方法之外,还有其他替代方法可以使它变得优雅吗?

By the way, I still have more, and much complex, models aside from SyncBillToPartyMaster. 顺便说一下,除了SyncBillToPartyMaster,我还有更多,更复杂的模型。 I don't want to do the them same way if there is another way to accomplish my goal. 如果有另一种方法可以实现我的目标,我不想以相同的方式进行操作。

Don't use AutoMapper, there's nothing that matches up here. 不要使用AutoMapper,这里没有匹配的东西。 You're not saving anything over just using a "new" operator and setting everything yourself. 您不会仅仅使用“新”运算符并自行设置所有内容来节省任何费用。

I read a few context in Automapper and I found ITypeConverter<in T, out TU> interface where I can implement to my custom mapper class and do my custom mapping. 我读了几方面Automapper ,我发现ITypeConverter<in T, out TU>界面,我可以实现我的自定义映射器类,做我的自定义映射。 And for registering my Custom mapper I used Mapper.CreateMap<T,TU>().ConvertUsing() . 为了注册我的自定义映射器,我使用了Mapper.CreateMap<T,TU>().ConvertUsing()

Here was my custom mapper : 这是我的自定义映射器:

public class CustomerAddressBillToPartyMasterMapper : ITypeConverter<SyncBillToPartyMaster, CustomerAddress>
    {
        public CustomerAddress Convert(ResolutionContext context)
        {
            var syncBillToPartyMaster = context.SourceValue as SyncBillToPartyMaster;

            if (syncBillToPartyMaster == null)
                return null;

            var customerAddressesSource = syncBillToPartyMaster.DataArea.BillToPartyMaster;

            return new CustomerAddress
            {
                CustomerId = customerAddressesSource.CustomerParty.PartyIDs.ID.Value,
                CustomerAddressId = customerAddressesSource.PartyIDs.ID.Value,
                City = customerAddressesSource.Location.Address.CityName,
                State = customerAddressesSource.Location.Address.CountrySubDivisionCode.Value,
                Country = customerAddressesSource.Location.Address.CountryCode.Value,
                Zip = customerAddressesSource.Location.Address.PostalCode.Value,
                Address1 = customerAddressesSource.Location.Address.AddressLine[0].Value,
                Address2 = customerAddressesSource.Location.Address.AddressLine[1].Value,
                Address3 = customerAddressesSource.Location.Address.AddressLine[2].Value,
                Phone1 = GetContact(customerAddressesSource, "phone"),
                Fax1 = GetContact(customerAddressesSource, "fax"),
                MaintenanceCustomerId = customerAddressesSource.LastModificationPerson.IDs[0].Value,
                MaintenanceUser = customerAddressesSource.LastModificationPerson.Name.Value,
                MaintenanceDate = DateTime.UtcNow
            };
        }
 }

And this was how I register it : 这就是我注册的方式:

Mapper.CreateMap<SyncBillToPartyMaster, CustomerAddress>().ConvertUsing(new CustomerAddressBillToPartyMasterMapper());

I think this was much cleaner than my previous code I posted. 我认为这比我以前发布的代码干净得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM