I am currently using ViewModels to separate my Views from the actual Model structure.
For example I have a User persistence entity and a MyProfile ViewModel containing all the information, that a user can change on his own.
For the conversion from User to MyProfile I am using Automapper.
Now after a user posts back his (changed) information, I need to save these. But the information in the ViewModel is not complete, and when AutoMapper creates a User persistence entity from the ViewModel, important information gets lost.
I do not want to expose this information to the View Layer, especially not with hidden form elements.
So i need a way to merge a ViewModel into a persistence entity. Can I do that with AutoMapper, or do I have to do it manually?
Example:
My User class contains ID, Firstname, Lastname, Username and Password. The user should only edit his First and Lastname in his profile. Therefore my ProfileViewModel contains ID, Firstname and Lastname. After posting back the information from the form, Automapper creates a User object from the transferred ProfileViewModel, and in this object only ID, Firstname and Lastname are set. When feeding this Entity to my Repository, I have lost username and password information.
Yes, you can do that with AutoMapper. For example:
prints:
And to translate this into a typical ASP.NET MVC pattern: