Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 除了AutoMapper中的FormMember方法外,还有其他自定义映射方法吗?_C#_Automapper - Fatal编程技术网

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

C# 除了AutoMapper中的FormMember方法外,还有其他自定义映射方法吗?,c#,automapper,C#,Automapper,我有一个复杂的模型(SyncBillToPartyMaster),我想定制映射到我的简单POCO类中 Mapper.CreateMap<SyncBillToPartyMaster, CustomerAddress>() .ForMember(d => d.CustomerId, o => o.MapFrom(src => src.DataArea.BillToPartyMaster.CustomerParty.PartyIDs.ID.Value)) .ForMemb

我有一个复杂的模型(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));
Mapper.CreateMap()
.ForMember(d=>d.CustomerId,o=>o.MapFrom(src=>src.DataArea.BillToPartyMaster.CustomerParty.PartyIDs.ID.Value))
.FormMember(d=>d.CustomerAddressId,o=>o.MapFrom(src=>src.DataArea.BillToPartyMaster.PartyId.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.countrySubsectionCode.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))
.FormMember(d=>d.Address1,o=>o.MapFrom(src=>src.DataArea.BillToPartyMaster.Location.Address.AddressLine[0].Value))
.FormMember(d=>d.Address2,o=>o.MapFrom(src=>src.DataArea.BillToPartyMaster.Location.Address.AddressLine[1].Value))
.FormMember(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”))
.FormMember(d=>d.MaintenanceCustomerId,o=>o.MapFrom(src=>src.DataArea.BillToPartyMaster.LastModificationPerson.IDs[0].Value))
.FormMember(d=>d.MaintenanceUser,o=>o.MapFrom(src=>src.DataArea.BillToPartyMaster.LastModificationPerson.Name.Value))
.ForMember(d=>d.MaintenanceDate,o=>o.MapFrom(src=>DateTime.UtcNow));
如您所见,使用AutoMapper的FormMember方法将我的复杂模型SyncBillToPartyMaster映射到CustomerAddress是相当繁琐的。除了使用FormMember方法外,还有没有其他方法可以使它变得优雅


顺便说一下,除了SyncBillToPartyMaster之外,我还有更多更复杂的模型。如果有其他方法来实现我的目标,我不想用同样的方法

不要使用AutoMapper,这里没有匹配的。只使用一个“新”操作符并自己设置所有内容并不能节省任何内容。

不要使用AutoMapper,这里没有匹配的内容。仅使用“new”操作符并自行设置,您不会保存任何内容。

我在中阅读了一些上下文,并找到了
ITypeConverter
接口,可以在其中实现自定义映射器类并执行自定义映射。为了注册我的自定义映射器,我使用了
mapper.CreateMap().ConvertUsing()

这是我的自定义映射器:

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
            };
        }
 }
公共类CustomerAddressBillToPartyMasterMapper:ITypeConverter
{
公共CustomerAddress转换(ResolutionContext上下文)
{
var syncBillToPartyMaster=context.SourceValue作为syncBillToPartyMaster;
如果(syncBillToPartyMaster==null)
返回null;
var customeraddressssource=syncBillToPartyMaster.DataArea.BillToPartyMaster;
返回新的CustomerAddress
{
CustomerId=CustomerAddressessSource.CustomerParty.PartyId.ID.Value,
CustomerAddressId=CustomerAddressSSource.PartyId.ID.Value,
城市=CustomerAddressessSource.Location.Address.CityName,
状态=CustomerAddressessSource.Location.Address.CountrySubsectionCode.Value,
Country=CustomerAddressessSource.Location.Address.CountryCode.Value,
Zip=CustomerAddressessSource.Location.Address.PostalCode.Value,
Address1=CustomerAddressessSource.Location.Address.AddressLine[0]。值,
Address2=CustomerAddressessSource.Location.Address.AddressLine[1]。值,
Address3=CustomerAddressessSource.Location.Address.AddressLine[2]。值,
Phone1=GetContact(客户地址来源,“电话”),
传真1=GetContact(客户地址来源,“传真”),
MaintenanceCustomerId=CustomerAddressessSource.LastModificationPerson.IDs[0]。值,
MaintenanceUser=CustomerAddressessSource.LastModificationPerson.Name.Value,
MaintenanceDate=DateTime.UtcNow
};
}
}
我就是这样注册的:

Mapper.CreateMap<SyncBillToPartyMaster, CustomerAddress>().ConvertUsing(new CustomerAddressBillToPartyMasterMapper());
Mapper.CreateMap().ConvertUsing(新的CustomerAddressBillToPartyMasterMapper());
我认为这比我以前发布的代码要干净得多。

我在中阅读了一些上下文,我找到了
ITypeConverter
接口,在那里我可以实现自定义映射器类并进行自定义映射。为了注册我的自定义映射器,我使用了
mapper.CreateMap().ConvertUsing()

这是我的自定义映射器:

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
            };
        }
 }
公共类CustomerAddressBillToPartyMasterMapper:ITypeConverter
{
公共CustomerAddress转换(ResolutionContext上下文)
{
var syncBillToPartyMaster=context.SourceValue作为syncBillToPartyMaster;
如果(syncBillToPartyMaster==null)
返回null;
var customeraddressssource=syncBillToPartyMaster.DataArea.BillToPartyMaster;
返回新的CustomerAddress
{
CustomerId=CustomerAddressessSource.CustomerParty.PartyId.ID.Value,
CustomerAddressId=CustomerAddressSSource.PartyId.ID.Value,
城市=CustomerAddressessSource.Location.Address.CityName,
状态=CustomerAddressessSource.Location.Address.CountrySubsectionCode.Value,
Country=CustomerAddressessSource.Location.Address.CountryCode.Value,
Zip=CustomerAddressessSource.Location.Address.PostalCode.Value,
Address1=客户地址来源。Loca