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

后端 未结 4 424
[愿得一人]
[愿得一人] 2021-02-01 11:46

UPDATE: this stuff has evolved into a nice project, see it at http://valueinjecter.codeplex.com


check this out, I just wrote a simple automapper, it takes
相关标签:
4条回答
  • 2021-02-01 11:56

    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 ?

    0 讨论(0)
  • 2021-02-01 11:56

    Just a thought:

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

    0 讨论(0)
  • 2021-02-01 11:57

    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 ;-)

    0 讨论(0)
  • 2021-02-01 12:07

    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.

    0 讨论(0)
提交回复
热议问题