Configuring / Using
Setting up a basic mapper is as simple as:
The call to CreateMap only needs to be made once for the duration of the AppDomain, calls to Map are then made as needed.
As was as direct property name mapping several other more complex mappings are provided “out of the box”. This includes flattening properties of child objects into named properties in the target object. AutoMapper also provides several different methods to provide custom mappings either inline in the form:
Or using custom formatting classes in the form:
- Mapper.CreateMap<string, DateTime>().ConvertUsing(new DateTimeTypeConverter());
- public class DateTimeTypeConverter : ITypeConverter<string, DateTime>
- public DateTime Convert(string source)
- return System.Convert.ToDateTime(source);
Initial tests have thrown up no surprises; the flexibility that Automapper provides does come with some overhead – but in most cases I’m sure that the performance is going to be fine for the project in question. Being able to quickly map messages to domain objects and move on is going to improve the speed in which we can develop the project. If problems are encountered once the code base is live, there is nothing to stop the areas in question being re-investigated and/or rolled back to manual mappings.
del.icio.us Tags: Automapper