I've just started using AutoMapper and so far found it very straight-forward and time-saving.
Just one thing I'm not sure about - how do I map all the properties of a given type in the same way?
Can this be done with AutoMapper in a single statement, using a lambda, as with regular mapping?
By default, AutoMapper only recognizes public members. It can map to private setters, but will skip internal/private methods and properties if the entire property is private/internal.
Inside this article, it discusses performance and it indicates that Automapper is 7 times slower than manual mapping. This test was done on 100,000 records and I must say I was shocked.
AutoMapper is used whenever there are many data properties for objects, and we need to map them between the object of source class to the object of destination class, Along with the knowledge of data structure and algorithms, a developer is required to have excellent development skills as well.
Collection. Adds ability to map collections to existing collections without re-creating the collection object.
What you are looking for is known as a CustomTypeConverter
. These are global in scope, and only need to be configured once.
The syntax is:
Mapper.CreateMap<TSourceProperty,TDestinationProperty>().ConvertUsing(argument);
where argument
can be
ITypeConverter<TSourceProperty,TDestinationProperty>
Func<TSourceProperty,TDestinationProperty>
Jimmy Bogard has an article on implementing CustomTypeConverter
s at http://www.lostechies.com/blogs/jimmy_bogard/archive/2009/05/05/automapper-feature-custom-type-converters.aspx .
More information is also provided in the CustomTypeConverter page of the AutoMapper documentation.
Oh, and by the way (since I want Omu's bounty) you can also do this by switching to valueinjecter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With