AutoMapper - Updating Instances
So far in this series we were dealing with mappings only with new instances of the source and destination classes. Automapper can also be used to do mapping for existing instances also, meaning instances that have data already in it before mapping. Typical example of this sceanario happens when we edit information, at that time our source will be the edited data and the destination data will be one stored in the data store.
We will be using an overloaded method for Map which takes the instance of the destination object as the second parameter. Unlike the other approach we used till now, we doesn't need to pass the type of the destination object to the Map object since AutoMapper can infer type from the instance of the destination object being passed to the method. Since we are dealing with an existing instance, nothing is returned by the Map method either. Once the mapping operation is completed our destination object will have the updated values mapped from the source.
Another major feature of Automapper is the native support of working with collection of objects or lists. The great thing about this feature is that there is no need to create a new mapping for the collection if there exists a map already created for the classes used in the collection. And this work on all types of collections and interfaces such as List<T>, ICollection<T>, IEnumerable<T>
In this example we have two lists with the second entry in the source list has empty values for some properties and after the mapping you can see that the entries in the destination object has got the values populated from the source. Please not that we have created the mapping for the types only and not for the collections.
Breaking Changes coming your way for ASP.NET Core 3.0
Resilient Connections in Entity Framework Core