AutoMapper, .NET’s most popular object-to-object mapping library, has been updated with a large set of new features including support for open generics and callbacks. AutoMapper 3.3 is expected to the final version in the 3.x series.
Custom destination type constructors
This feature allows developers to pass in a lambda that represents a custom constructor call. Normally this isn’t necessary, as AutoMapper is capable of matching up source properties to the constructor’s parameters. According to the documentation, “only use this method if AutoMapper can't match up the destination constructor properly, or if you need extra customization during construction.”
Open Generics
Open generics are generic types such as List<T> where the type parameter T hasn’t been provided yet. In previous version versions of AutoMapper, you could only provide closed generics such as List<int> or List<string>. Now you can write code such as…
Mapper.CreateMap(typeof(List<>), typeof(ObservableCollection<>));
Before and after map actions
Callbacks can now be added for mapping operations. These callbacks occur just before or after an object is mapped. In the example offered by the documentation, they populate a name property on an object using HttpContext.Current.Identity.Name instead of a property on the source object.
Replacing Member Names
In addition to whole member names, you can now map individual characters such as the accented é to an unadorned e. This mapping would apply no matter where the character is found in the member name.
Other Features
Other features mentioned in the announcement and release notes include
- Support string conversion in LINQ projections
- Added Functionality to Support Web Api expands on Projections
- Missing feature of handling enum to string mappings in queryable projections
- Allow parameterizing LINQ queries
- Support converting expression trees
- Configuration setting: allow public vs. internal properties to map.