3

Is there any way to use Automapper 5.1.1 to update an existing object as opposed to creating a new one.

For example we have a Customer entity and a CustomerViewModel. We would like to update an existing Customer with the CustomerViewModel field values.

Would greatly appreciate your assistance.

jrbedard
  • 3,662
  • 5
  • 30
  • 34
user2981411
  • 859
  • 12
  • 34

1 Answers1

2

It is not adviced to use Automapper to map a model to your Entity. Dependencies or Informations can be overwritten if it isn't used wisely. But to use it as you want, you only need to create a map from your Model to your Entity and then call

Mapper.Map(myModel, myEntity);

The mapping to entity Problem

I guess you use a ORM like NHibernate or EF, then your Entites are Proxies, where references are proxies too and so on. Now lets imagine you have an ASP.NET MVC Project and you map your Entity to your ViewModel. You show your Model in your View as a form, but you only show the properties that you need in your view, not all that are set in your ViewModel. Then the user sends the Form back to you and your Controller gets the ViewModel back, but this time not all Properties are set, because your View only knew the ones that were shown. If you map your ViewModel back to your entity, all unitialized properties are in there default state and will overwrite the valid data f rom your entity.

Another Problem is, that AutoMapper uses Reflections to set the Properties. Normally the right to exist for an ORM is the possibility to easy implement an DomainLayer. The DomainLayer has some Validations, Calculation... on the Entity itself. If now the Properties set with Reflection it would ignore the Business logic and no Validation, Calculations.... would be executed.

So my advice is, Don't map to Entities ;)

Rabban
  • 2,451
  • 1
  • 19
  • 21
  • Thanks for your response. You mention be careful about dependencies/information. What could be lost ? All we want to do is to copy field values from one to the other. Thanks again. – user2981411 Oct 17 '16 at 15:48
  • Glad i could help ;) I edited my Answer to bring a little bit more light in it. – Rabban Oct 17 '16 at 16:28
  • Thanks for the insight. The first issue is exactly why I want to use a ViewModel, so that both initialised and uninitialised - i.e. Whatever the ViewModel is should be transferred to the Entity - but nothing else. The second issue, is that if you apply your validation logic and data annotations etc to the ViewModel, you get the same behaviour as if it was applied to the Entity. Infact we have applied the IValidatableObject interface and data annotations to the View Model which works. – user2981411 Oct 17 '16 at 18:07
  • Mapping from entities to viewmodels is the lions share of the mapping most apps need to do. If that's done by hand, doing what else needs to be done by hand is a small problem. Better advice would be to explain what is necessary to make AutoMapper work, when dealing with entities. – Jeff Dege Feb 09 '17 at 15:39
  • @JeffDege i only stated that mapping **to** entities can not be recommend. This has nothing to do with mapping **from** entities. I know that projections are a very important part in using automapper, but you have to be very carefully if you want to map your DTOs/ViewModels/etc... back to the entities. You need to know what you do. And in larger projects with many developers unfortunately not all developers know what they do. I don't know a best practice for this kind of mapping. I can only advice awareness ;) – Rabban Feb 10 '17 at 11:04
  • Is the problem entirely with navigation properties? – Jeff Dege Feb 10 '17 at 15:13
  • @JeffDege No, unfortunately this applies to all properties. And the problem gets bigger if you let AutoMapper map your properties automatically by names. The mapping will be hidden and can't be easily read within the code. – Rabban Feb 10 '17 at 15:31