- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
更新 1-13-10我已经能够使用下面的代码进行映射找到一些成功。我基本上忽略了任何没有映射的属性,然后再映射它们。对于我是否以最佳方式解决此问题,我将不胜感激。此外,我不确定如何对该映射进行单元测试。我的印象是使用 AutoMapper 应该有助于减轻检查每个属性的繁琐。
这是我的新代码:
Mapper.CreateMap<MoveEntity, MoveEntityDto>()
.ForMember(dest => dest.PrimaryOriginTransferee, opt => opt.Ignore())
.ForMember(dest => dest.PrimaryDestinationTransferee, opt => opt.Ignore())
.ForMember(dest => dest.Customer, opt => opt.Ignore())
.ForMember(dest => dest.DestinationAddress, opt => opt.Ignore())
.ForMember(dest => dest.OriginAddress, opt => opt.Ignore())
.ForMember(dest => dest.Order, opt => opt.Ignore())
.ForMember(dest => dest.Shipment, opt => opt.Ignore())
.ForMember(dest => dest.SourceSystemName, opt => opt.Ignore());
Mapper.CreateMap<ContactEntity, TransfereeEntityDto>();
Mapper.CreateMap<CustomerEntity, CustomerEntityDto>();
Mapper.CreateMap<AddressEntity, AddressEntityDto>();
Mapper.CreateMap<OrderEntity, OrderEntityDto>()
.ForMember(dest => dest.OrderForwarding, opt => opt.Ignore())
.ForMember(dest => dest.Forwarder, opt => opt.Ignore());
Mapper.CreateMap<ShipmentEntity, ShipmentEntityDto>()
.ForMember(dest => dest.Services, opt => opt.Ignore());
Mapper.CreateMap<ServiceEntity, ServiceEntityDto>()
.ForMember(dest => dest.ServiceTypeCode, opt => opt.Ignore()) //TODO: ServiceTypeCode not being mapped, should it?
.ForMember(dest => dest.SourceSystemName, opt => opt.MapFrom(src => Enum.GetName(typeof(SourceSystemName), src.SourceSystemName)));
Mapper.CreateMap<OrderForwardingEntity, OrderForwardingEntityDto>();
Mapper.AssertConfigurationIsValid();
MoveEntityDto moveEntityDto = Mapper.Map<MoveEntity, MoveEntityDto>(moveEntity);
moveEntityDto.PrimaryDestinationTransferee = Mapper.Map<ContactEntity, TransfereeEntityDto>(moveEntity.PrimaryDestinationTransferee);
moveEntityDto.PrimaryOriginTransferee = Mapper.Map<ContactEntity, TransfereeEntityDto>(moveEntity.PrimaryOriginTransferee);
moveEntityDto.Customer = Mapper.Map<CustomerEntity, CustomerEntityDto>(moveEntity.Customer);
moveEntityDto.DestinationAddress = Mapper.Map<AddressEntity, AddressEntityDto>(moveEntity.DestinationAddress);
moveEntityDto.OriginAddress = Mapper.Map<AddressEntity, AddressEntityDto>(moveEntity.OriginAddress);
moveEntityDto.Order = Mapper.Map<OrderEntity, OrderEntityDto>(moveEntity.Order);
moveEntityDto.Order.OrderForwarding = Mapper.Map<OrderForwardingEntity, OrderForwardingEntityDto>(moveEntity.Order.OrderForwarding);
//moveEntityDto.Order.Forwarder = Mapper.Map<ForwarderEntity, ForwarderEntityDto>(moveEntity.Order.Forwarder); //Apparently there is no forwarder entity for an Order
moveEntityDto.Shipment = Mapper.Map<ShipmentEntity, ShipmentEntityDto>(moveEntity.Shipment);
moveEntityDto.Shipment.Services = Mapper.Map<ServiceEntity[], ServiceEntityDto[]>(moveEntity.Shipment.ServiceEntities);
原帖:
我第一次尝试使用 AutoMapper,以便从业务对象映射到 DTO。我遇到了我不知道如何解决的问题,包括以下异常:
AutoMapper.AutoMapperMappingException: Trying to map Graebel.SP.BO.MoveEntity to Graebel.SOA.Contracts.DataContracts.SP.MoveEntity. Exception of type 'AutoMapper.AutoMapperMappingException' was thrown
这是我正在运行的 AutoMapper 代码:
public MoveEntityDto MapMoveEntityToMoveEntityDto(MoveEntity moveEntity)
{
Mapper.CreateMap<MoveEntity, MoveEntityDto>()
.ForMember(dest => dest.PrimaryOriginTransferee, opt => opt.MapFrom(src => src.PrimaryOriginTransferee))
.ForMember(dest => dest.PrimaryDestinationTransferee,opt => opt.MapFrom(src => src.PrimaryDestinationTransferee))
.ForMember(dest => dest.Customer, opt => opt.MapFrom(src => src.Customer))
.ForMember(dest => dest.DestinationAddress, opt => opt.MapFrom(src => src.DestinationAddress))
.ForMember(dest => dest.Order, opt => opt.MapFrom(src => src.Order))
.ForMember(dest => dest.OriginAddress, opt => opt.MapFrom(src => src.OriginAddress))
.ForMember(dest => dest.Shipment, opt => opt.MapFrom(src => src.Shipment))
.ForMember(dest => dest.SourceSystemName, opt => opt.Ignore());
Mapper.AssertConfigurationIsValid();
MoveEntityDto moveEntityDto = Mapper.Map<MoveEntity, MoveEntityDto>(moveEntity);
return moveEntityDto;
}
这是我尝试映射的 DTO (MoveEntityDto):
public class MoveEntityDto
{
public bool IsOrderDetailPageModified { get; set; }
public bool IsRoutingPageModified { get; set; }
public bool IsServicePageModified { get; set; }
public bool IsContentAndContainerPageModified { get; set; }
public string FamilyRange { get; set; }
public string Office { get; set; }
public string ActivityType { get; set; }
public string ActivitySubject { get; set; }
public string ActivityNote { get; set; }
public TransfereeEntity PrimaryOriginTransferee { get; set; }
public TransfereeEntity PrimaryDestinationTransferee { get; set; }
public CustomerEntity Customer { get; set; }
public AddressEntity OriginAddress { get; set; }
public AddressEntity DestinationAddress { get; set; }
public OrderEntity Order { get; set; }
public ShipmentEntity Shipment { get; set; }
public string PortalId { get; set; }
public string SourceSystemId { get; set; }
public EnterpriseEnums.SourceSystemName SourceSystemName { get; set; }
public MoveEntity()
{
PrimaryOriginTransferee = new TransfereeEntity();
PrimaryDestinationTransferee = new TransfereeEntity();
Customer = new CustomerEntity();
OriginAddress = new AddressEntity();
DestinationAddress = new AddressEntity();
Order = new OrderEntity();
Shipment = new ShipmentEntity();
}
public bool HasShipment()
{
if (Shipment.ExternalShipmentId > 0)
{
return true;
}
return false;
}
}
这是我试图映射的业务对象 (MoveEntity)
public class MoveEntity
{
public int SourceId { get; set; }
public int MoveId { get; set; }
public bool IsOrderDetailPageModified { get; set; } // TODO: Internal - Remove from data contract
public bool IsRoutingPageModified { get; set; } // TODO: Internal - Remove from data contract
public bool IsServicePageModified { get; set; } // TODO: Internal - Remove from data contract
public bool IsContentAndContainerPageModified { get; set; } // Rmove from data contract
public string FamilyRange { get; set; } // TODO: Is this being used?
public string Office { get; set; }
public string ActivityType { get; set; }
public string ActivitySubject { get; set; }
public string ActivityNote { get; set; }
public ContactEntity PrimaryOriginTransferee { get; set; }
public ContactEntity PrimaryDestinationTransferee { get; set; }
public CustomerEntity Customer { get; set; }
public AddressEntity OriginAddress { get; set; }
public AddressEntity DestinationAddress { get; set; }
public OrderEntity Order { get; set; }
public ShipmentEntity Shipment { get; set; }
public string CreatedBy { get; set; }
public DateTime CreatedDate { get; set; }
public string ModifiedBy { get; set; }
public DateTime ModifiedDate { get; set; }
public string SourceSystemId { get; set; }
public string SourceSystemName { get; set; }
public string Version { get; set; }
public string PortalId { get; set; }
public MoveEntity()
{
PrimaryOriginTransferee = new ContactEntity
{
ContactTypeId = ContactEntity.ContactType.PrimaryOriginationTransferee
};
PrimaryDestinationTransferee = new ContactEntity
{
ContactTypeId = ContactEntity.ContactType.PrimaryDestinationTransferee
};
OriginAddress = new AddressEntity
{
AddressTypeId = AddressEntity.AddressType.Origination
};
DestinationAddress = new AddressEntity
{
AddressTypeId = AddressEntity.AddressType.Destination
};
Order = new OrderEntity();
Customer = new CustomerEntity();
Shipment = new ShipmentEntity();
}
public bool HasShipment()
{
if (Shipment.ShipmentId > 0)
{
return true;
}
return false;
}
}
每个类中的属性在名称上几乎完全匹配,但它们的类型不同。因此,我尝试使用“MapFrom”表达式执行自定义映射。但是,AutoMapper 似乎无法让我毫无怨言地从一种对象类型指向另一种对象类型。
我也尝试过映射属性到属性,但没有成功。它看起来像这样:
.ForMember(dest => dest.PrimaryOriginTransferee.Email, opt => opt.MapFrom(src => src.PrimaryOriginTransferee.Email))
但是,在尝试此操作时,我收到以下异常消息:
must resolve to top-level member. Parameter name: lambdaExpression.
我发现 AutoMapper 的可用文档很难理解。有人可以为我指明正确的方向,告诉我如何正确使用此实用程序吗?
在此先感谢您的帮助!
亚当
最佳答案
我终于让这个独立工作了。我最终使用的代码发布在下面。事实证明,以正确的顺序创建对象 map 非常重要。我通过这件事学到了很多东西。
我已经将我的映射组织到一个配置文件中,我不会在这里讨论,只要说如果你可以在从 AutoMapper Profile 类继承的类之外使用我的示例,你就会想要使用 Mapper .CreateMap 而不仅仅是创建 map 。
private void CreateMaps()
{
CreateMap<ContactEntity, TransfereeEntityDto>();
//ContactEntity Mapping
CreateMap<ContactEntity, TransfereeEntityDto>();
//CustomerEntity Mapping
CreateMap<CustomerEntity, CustomerEntityDto>();
//AddressEntity Mapping
CreateMap<AddressEntity, AddressEntityDto>();
//ServiceEntity Mapping
CreateMap<ServiceEntity, ServiceEntityDto>()
.ForMember(dto => dto.ServiceTypeCode, opt => opt.MapFrom(source => source.TypeCode))
.ForMember(dto => dto.ServiceDescriptionCode, opt => opt.MapFrom(source => source.DescriptionCode))
.ForMember(dest => dest.SourceSystemName, opt => opt.ResolveUsing<SourceSystemNameResolver>().FromMember(entity => entity.SourceSystemName));
//VehicleEntity Mapping
CreateMap<VehicleEntity, VehicleEntityDto>()
.ForMember(dest => dest.SourceSystemName, opt => opt.ResolveUsing<SourceSystemNameResolver>().FromMember(entity => entity.SourceSystemName))
.ForMember(dto => dto.PortalId, option => option.Ignore()); //TODO: Should PortalID be mapped to anything? It is not in the entity.
//ContentEntity Mapping
CreateMap<ContentEntity, ContentEntityDto>()
.ForMember(dest => dest.SourceSystemName, opt => opt.ResolveUsing<SourceSystemNameResolver>().FromMember(entity => entity.SourceSystemName));
//OrderForwardingEntity Mapping
CreateMap<OrderForwardingEntity, OrderForwardingEntityDto>();
//ContainerEntity Mapping
CreateMap<ContainerEntity, ContainerEntityDto>()
.ForMember(dest => dest.SourceSystemName, opt => opt.ResolveUsing<SourceSystemNameResolver>().FromMember(entity => entity.SourceSystemName));
//ShipmentForwardingEntity Mapping
CreateMap<ShipmentForwardingEntity, ShipmentForwardingEntityDto>();
//ShipmentRouting Mapping
CreateMap<ShipmentRoutingEntity, ShipmentRoutingEntityDto>();
//ShipmentEntity Mapping
CreateMap<ShipmentEntity, ShipmentEntityDto>()
.ForMember(dest => dest.SourceSystemName, opt => opt.ResolveUsing<SourceSystemNameResolver>().FromMember(entity => entity.SourceSystemName))
.ForMember(dto => dto.Services, option => option.MapFrom(source => source.ServiceEntities));
//Forwarder mapping
CreateMap<ContactEntity, ForwarderEntityDto>();
//TODO: This property doesn't have any properties in the data contract
//OrderEntity Mapping
CreateMap<OrderEntity, OrderEntityDto>()
.ForMember(dest => dest.SourceSystemName,
opt => opt.ResolveUsing<SourceSystemNameResolver>().FromMember(entity => entity.SourceSystemName));
//.ForMember(dto => dto.Forwarder, option => option.MapFrom(entity=>entity.Forwarder)
//MoveEntityMapping
CreateMap<MoveEntity, MoveEntityDto>()
.ForMember(dto => dto.SourceSystemName, opt => opt.ResolveUsing<SourceSystemNameResolver>().FromMember(entity => entity.SourceSystemName));
}
关于c# - Automapper 自定义映射异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2053253/
请看一下我的代码。 int main () { Program* allcommand = new Program; allcommand->addCommand("add", new
因此,当我遇到调试断言时,我正在编写代码。现在我很想知道为什么这段代码不起作用: for(Model::MeshMap::iterator it = obj1->GetMeshes().begin()
这是我上一个问题的延续 Group, Sum byType then get diff using Java streams . 按照建议,我应该作为单独的线程发布,而不是更新原始线程。 因此,通过我
我正在实现一些非常适合 map 的代码。但是,我要迭代的列表中有大量对象,所以我的问题是哪种方法是解决此问题的最佳方法: var stuff = $.map(listOfMyObjects, some
我正在尝试创建一个包含不同类的成员函数指针的映射。成员函数都具有相同的签名。为了做到这一点,我所有的类都继承了一个 Object 类,它只有默认构造函数、虚拟析构函数和一个虚拟 ToString()
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: how do you make a heterogeneous boost::map? 有可能在 C++ 中
我有一个 Mysql 查询,请检查以下内容: SELECT `tbl_classSubjects`.`classID` , `tbl_classSubjects`.`sectionID` , `tbl
抱歉,这可能是一个基本问题。 JNA直接映射和接口(interface)映射有什么区别? 我的解释是否正确: 直接映射 : 直接使用库对象(如 Java 中的静态 main) 接口(interface
在 Twitter's Scala school collections section ,它们显示了一个带有偏函数作为值的 Map: // timesTwo() was defined earlie
很难说出这里问的是什么。这个问题是模棱两可的、模糊的、不完整的、过于宽泛的或修辞的,无法以目前的形式得到合理的回答。如需帮助澄清这个问题以便重新打开它,visit the help center .
据我了解,从 scala stdlib 声明一个映射并没有将其专门用于原始类型。我要的不是付出装箱/拆箱的代价,而是同时拥有scala map 的接口(interface)。一个明显的选择是使用 tr
如何为这样的 JSON 响应创建对象映射,它只是一个整数数组: [ 565195, 565309, 565261, 565515, 565292, 565281, 566346, 5
是否可以为 DTO 对象创建映射然后查询它们 而不是域?如果不解释为什么? 如果我需要几个 dtos 怎么办? DTos 是只读的 ID 由 NH 自动生成 将来这些 dtos 将设置映射到链接的 d
我有一个返回的函数(常规代码) [words: "one two", row: 23, col: 45] 在 Scala 中,我将上面更改为 Scala Map,但随后我被迫将其声明为 Map[Str
我有一组与 Vanilla 磅蛋糕烘焙相关的数据(200 行),具有 27 个特征,如下所示。标签caketaste是衡量烤蛋糕的好坏程度,由 bad(0) 定义, neutral(1) , good
我有试图映射到新代码的遗留代码。 OLD_PERSON pid sid name age NEW_PERSON pid sid fid age RESOLVE_PERSON pid fid statu
我有一个表,其中一个字段可以指向其他 3 个表之一中的外键,具体取决于鉴别器值是什么(Project、TimeKeep 或 CostCenter。通常这是用子类实现的,我想知道我有什么 注意子类名称与
我有一个类型 [ST s (Int, [Int])] 的绑定(bind)我正在尝试申请runST使用映射到每个元素,如下所示: name :: [ST s (Int, [Int])] --Of Cou
在我正在进行的项目中,我有以下实体:分析师、客户 和承包商。每个都继承自基类 User。 public abstract class User { public virtual int Id
我想知道是否可以在 Vim 中创建一个映射(对于普通模式),允许用户在映射执行之前输入。 我想为我最常用的 grep 命令创建一个快捷方式的映射。我希望命令允许输入我正在搜索的内容,然后在输入时执行。
我是一名优秀的程序员,十分优秀!