views:

70

answers:

3
+3  Q: 

Mapping Objects

what is the best Solution for mapping class object to lightweight class object by example:

Customer to CustomerDTO both have the same properties names, i was thinking of the best optimized solution for mapping between them , i know reflection slow me down badly , and making methods for every mapping is time consuming so any idea ?

thanks in advance.

+7  A: 

AutoMapper. There's also ValueInjecter and Emit Mapper.

Darin Dimitrov
Damn, beat me to it. +1
Winston Smith
automapper is so slow i tried it and tested it is slower than doing the mapping manual with 20 times !
Stacker
@Zeus. Indeed - but does that have a perceptible impact on your system?
Winston Smith
Did you use the latest version? I know that Jimmy Bogard made some performance improvements recently.
Darin Dimitrov
yeah i used the latest version, and yeah i cant use something that is slower with 20 times, i was wondering if there is any other techniques people are using .
Stacker
Emit Mapper is pretty fast.
Darin Dimitrov
im gonna test emit mapper and give feedback
Stacker
also i need to try ValueInjecter
Stacker
+1  A: 

I've been playing about with this, and have the following observations. Should Customer inherit from CustomerDTO or read/write to a CustomerDTO? I've found that some DTO generators only generate dumb fixed size array collections for vectors of data items within the DTO others will allow you to specify a LIST<> or some such collection. The high-level collection does not need to appear in the serialised DTO but effects which approach you take. If your solution adds high-level collections then you can inherit if it doesn't then you probably want to read/write to a intermediate DTO.

I've used Protocol Buffers and XSDObjectGenerator for my DTO Generation (at different times!).

Tony Lambert
+1  A: 

If reflection is slowing you down too much, try Fasterflect: http://www.codeproject.com/KB/library/fasterflect_.aspx

If you use the caching mechanism, it is not much slower than hand-written code.

Gabe
im going to try that out and i will give feed back
Stacker