0

Should AutoMapper be used to take data from ViewModel and save back into a database model?

I know the opposite practice is good software practice: to have Automapper to extract database models, and place them in Viewmodels for front end web applications.

I was reading this general article here: The article was not specifically about Automapper, but want to validate if the opinion is true. best way to project ViewModel back into Model

"I believe best way to map from ViewModel to Entity is not to use AutoMapper for this. AutoMapper is a great tool to use for mapping objects without using any other classes other than static. Otherwise, code gets messier and messier with each added service, and at some point you won't be able to track what caused your field update, collection update, etc."

Relocating here: https://softwareengineering.stackexchange.com/questions/387385/should-automapper-be-used-to-map-from-viewmodel-back-into-model

  • 3
    If you need to map from one type to another, you can use AutoMapper. Whether it is a viewmodel or what or which direction is irrelevant. It's simple: need mapping? Use it. – CodingYoshi Feb 18 '19 at 01:51
  • If your models and view models all have the same auto-mappable properties then it sort of begs the question... Why not just use your models directly in your views? – David Feb 18 '19 at 02:03
  • 1
    @david Because there is an army of gangsters who says not to do that though I ask the same question myself all the time. But yes, I do it all the time. – CodingYoshi Feb 18 '19 at 02:05
  • The linked answer lists a number of issues they've come across using automapper to map DTOs to VMs. If you have those issues then you may find similar problems using automapper. Unless I had specific issues that were causing me problems I'd use the nifty tool that makes life easy. – Ben Feb 18 '19 at 02:07
  • sometimes, for me viewmodel may have different properties (less columns) from model, anyways interested to know if automapper can/should be used in reverse into model –  Feb 18 '19 at 02:09
  • If this is a best practice question you are sort-of in the wrong place, i mean, there are reasons for and against and they are entirely subjective. If it works for you and your colleagues and boss don't mind automapping to an entity, then by all means. However, if you dont want to abstract the mapping code, and you like seeing every modification, then don't – TheGeneral Feb 18 '19 at 02:21
  • ok, well I always thought Stack is good place to learn about best practice, I started programming 3 months ago, so trying to learn about this, thanks –  Feb 18 '19 at 02:24
  • This is more of a software engineering and design question, although they do get answered here from time to time they tend to be close more often than not. , there is a stack exchange sire with this very name, I think you want to give it a try – TheGeneral Feb 18 '19 at 02:42
  • relocating here, https://softwareengineering.stackexchange.com/questions/387385/should-automapper-be-used-to-map-from-viewmodel-back-into-model –  Feb 18 '19 at 18:52

0 Answers0