用于对象映射器的 PostSharp
我正在考虑使用 PostSharp 进行实体到 DTO 和 DTO 到实体映射器。 为大约 100 个实体手动执行该任务将是一场维护噩梦。 我在 codeplex 上查看过 AutoMapper,但我认为在我的情况下开销可能是一个严重的问题,此外我觉得 PostSharp 可以给我一些对映射约定的额外控制。 如果有人可以分享解决这个问题之王的经验,那就太好了。
我想的方向是这样的(请有人告诉我这是否不可能):
我计划坚持一个类的方面将用内容填充接下来的两个方法:
EntityType EntityToDTO(DTOType DTO) {}
DTOType DTOToEntity(EntityType Entity) {}
第一个方法将返回基于实体在 DTO 上,第二个会做相反的事情。 在方面内,我计划循环遍历每个属性,创建新目标并将属性的值分配给目标对象中的对应属性。 这是否可以在编译时完成而没有任何运行时开销?
I'm considering using PostSharp for entity-to-DTO and DTO-to-entity mapper. To do that task manualy for about a 100 entities would be a maintenence nightmare. I've looked at AutoMapper on codeplex, but i think the overhead might be a serious problem in my case, besides i feel that PostSharp could give me some extra control over the mapping convention. If anyone can share any experiences with this king of problems, that would be great.
The direction i'm think in is something like this (please somebody tell me if this is not possible):
The aspect that i am planing to stick to a class would fill the next two methods with content:
EntityType EntityToDTO(DTOType DTO) {}
DTOType DTOToEntity(EntityType Entity) {}
The first method would return entity based on DTO, the second one would do the oposite. Inside the aspect i'm planing to loop through each property, create new target and asign the value of a property to the counterpart from target object. Is this possible to do at compiletime witout any runtime overhead?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的 DTO 字段名称与您的实体字段名称匹配,那么我会使用 Duck Typing
http://www.deftflux.net/blog/page/Duck-Typing-Project.aspx
http://haacked.com/archive/2007/08/19/why-duck-typing-matters-to-c-developers .aspx
您的代码将像这样工作
基本上,鸭子类型库将通过匹配名称来映射字段。 他们使用动态生成的 IL 来对此进行归档。
如果这可能太慢,我可能会尝试让 CodeSmith 为我生成方法。
If your DTOs field names match your entity field names, then I'd use Duck Typing
http://www.deftflux.net/blog/page/Duck-Typing-Project.aspx
http://haacked.com/archive/2007/08/19/why-duck-typing-matters-to-c-developers.aspx
Your code would work like this
Basically, the duck typing library will be mapping over the fields by matching the names. They use dynamically generated IL to archive this.
If that has the potential of being too slow, I'd probably try to get CodeSmith to generate the methods for me.
如果有帮助,有一个名为 PostSharp4ET 的项目,它基本上实现了对实体框架 1 的 POCO 对象的支持。请参阅 http:// /www.codeplex.com/efcontrib。
请注意,PostSharp 不太擅长生成新代码。 它擅长将新代码与现有代码混合。 如果您需要生成代码,我建议编写一个基于反射的 C# 代码生成器,并编译生成的代码。 或者使用像前面提到的 CodeSmith 这样的工具。
If it helps, there is a project called PostSharp4ET that basically implements support for POCO objects to Entity Framework 1. See http://www.codeplex.com/efcontrib.
Note that PostSharp is not very good at generating new code. It is good at mixing new code with existing one. If you need to generate code, I would recommend writing a C# code generator based on reflection, and compile the resulting code. Or use a tool like CodeSmith, as mentioned previously.