PostSharp 對象映射器 (PostSharp for an object mapper)


問題描述

PostSharp 對象映射器 (PostSharp for an object mapper)

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?

‑‑‑‑‑

參考解法

方法 1:

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 

UserDTO user = DuckTyping.Cast<UserDTO>(userEntity);

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.

方法 2:

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.

(by Miha NecakBobGael Fraiteur)

參考文件

  1. PostSharp for an object mapper (CC BY‑SA 3.0/4.0)

#postsharp #aop #C#






相關問題

拋出自定義異常並使用 Postsharp 捕獲它們 (Throw an custom exception and catch them with Postsharp)

Làm cách nào để loại trừ khỏi ghi nhật ký dựa trên OnMethodBoundaryAspect? (How to exclude from OnMethodBoundaryAspect-based logging?)

在屬性中傳遞動態參數 (PostSharp) (Passing dynamic parameters in attributes (PostSharp))

PostSharp 3.0 中 IAspectProvider 的重大變化? (Breaking change of IAspectProvider in PostSharp 3.0?)

將方面應用於其他程序集類方法調用 (apply an aspect to other assembly class methods calls)

Postsharp 和 log4net 和 log4postsharp (Postsharp and log4net and log4postsharp)

Postsharp - 獲取調用程序集? (Postsharp - Get Calling Assembly?)

PostSharp 對象映射器 (PostSharp for an object mapper)

PostSharp - 自動化事件訂閱和集合添加 (PostSharp - automate event subscription and collection addition)

PostSharp:在目標構造函數之後初始化實例範圍的方面 (PostSharp: initialize instance-scoped aspect after target constructors)

使用 PostSharp 1.0 的 ClickOnce 應用程序似乎需要 GAC 中的 1.5 程序集 (ClickOnce application that uses PostSharp 1.0 seems to require 1.5 assemblies in GAC)

更改用戶控件 DependencyProperty 時屬性重置 (Property resets when changing Usercontrols DependencyProperty)







留言討論