Simple Convention Automapper for two-way Mapping (Entities to/from ViewModels)?

One thing you might want to add is to cache the reflection bits. If you map an object twice, you probably don't want to look up all the reflection stuff again. Also, things like GetValue and SetValue are quite slow, I switched to late-bound delegates + Reflection.

Emit to speed things up.

Well, I don't map a specific type to another one, it's more like per property mapping for all Types – Chuck Norris Nov 23 '09 at 13:41 I'm going to investigate how to speed the things up though, thanx for suggesting Reflection. Emit and late-bound delegates, not sure about caching – Chuck Norris Nov 23 '09 at 13:45 2 Caching definitely helped AutoMapper out - the GetFields and GetProperties calls can be a bottleneck - run this in something like dotTrace and you can see where the time is spent. This type of coding is pretty cool, have fun with it!

– Jimmy Bogard Nov 25 '09 at 13:59 thnx, good idea with caching the GetProperties, I can store the Properties for each type in cache so I won't need to get them each time – Chuck Norris Nov 26 '09 at 8:55 it is fun :), check it out valueinjecter.codeplex. Com/documentation – Chuck Norris May 5 '10 at 18:49.

Just use AutoMapper. This is fine, but it'll grow into a mini project. Just some things AM (the real one) does is: reporting if you have properties that can't be mapped to flattening objects providing hooks for you to customise some aspects, not in a big switch statement using Expression.

Compile for perf reasons rather than reflection directly But it's certainly an interesting space to mess about in, and the idea of auto mapping is certainly useful. A bit like DI in 15 or 33 lines vs NInject or its friends - cool, but why?. I take it you've read the article and comments regarding 2 way mapping on Jimmy's blog?

I intend to use it for editing entities (to map from my models into my viewmodels and viceversa), I wanted to use automapper for that, but Jimmy told me not to :) – Chuck Norris Nov 23 '09 at 8:23 1 @Omu, fair enough. The point for me though is that your superficially simple 'solution' is hiding an iceberg of special cases, cool and all as it is, and in completing it, you'll end up with a non-trivial amount of work. Cottsak's point asking what are you doing this mapping for is worth considering.

But you didn't give us much to go on regarding what your real scenario is. For all we know you might the ideal . NET RIA Services user :P Did Jimmy say not to add 2-way mapping to AM or that he wasnt going to because its not a use case he sees making sense?

Forking could be better than reinventing – Ruben Bartelink Nov 23 '09 at 8:37 I have updated the question text, you can see now how I use it – Chuck Norris Nov 23 '09 at 13:20 Thanks for the link, it (lack of 2-way mapping) makes sense now. – JTew Nov 23 '09 at 0:02.

Just a thought: One might wonder what the point of an abstraction is if the abstraction is so easily mapped to that being abstracted.

You can add exceptions (ifs, switch) for each type you may need You're not going to do this. Seriously. Case statements acting on object types are bad OOP style.

I mean, really bad. Like driving with a drink of Vodka inside your stomach. It may work for some time, but eventually you get into trouble.

OK Jimmy told you not to use AutoMapper... but I bet he meant something else. Now, have you invented something different - something that make Jimmy happy? ;-) No, you just made your own half-rolled AutoMapper.

And Jimmy told you not to use it! ;-) So here's my suggestion: ignore what Jimmy says, just think yourself.. and use AutoMapper ;-).

Well yes, I could, but with this stuff I can do my own conventions very easily; I use it at the moment in 2 methods of a "Builder" class: BuildEntity BuildInput, so i'm mapping automatically all my entities to all my Inputs (ViewModels), without the need to specify a mapping (CreateMap) for each pair of types – Chuck Norris Nov 23 '09 at 10:40 You can have your BuildEntity methods to invoke AutoMapper CreateMap/Map internally. Moreover, you can have it to check if CreateMap is already registered, so you have possibility to override default mapping - instead of hard-coding into switches which is the BAD BAD style. Still I don't see a reason to re-invent AutoMapper.

– queen3 Nov 23 '09 at 12:55 I have updated the question text, you can see now how I use it – Chuck Norris Nov 23 '09 at 13:20.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions