tags:

views:

192

answers:

2

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  A: 

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.

Bob
+1  A: 

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.

Gael Fraiteur