The open source project AutoMapper written by Jimmy Bogard has been actively developed for about a year and recently got to the 1.0 mark. AutoMapper is a convention based object-to-object mapper often used to (but not restricted to) flatten complex object models to DTOs, commonly used in ViewModels and crossing service boundaries.
AutoMapper provide the user with a fluent configuration API as well as using conventions to do the auto part of the mapping. Some of AutoMapper’s features include:
- Flattening
- Projection
- Configuration Validation
- Lists and Arrays
- Nested Mappings
- Custom Type Converters
- Custom Value Converters
- Custom Value Formatters
- Null Substitution
AutoMapper is a one-way mapper. Meaning no built in support for mapped objects to write back to its original source, unless the user specifically create a reverse mapping after the mapped object is updated. This is by design, since having a DTO write back to e.g. a domain model would (among other things) take away its immutability, and is often considered an anti-pattern. In such a scenario the Command Messages is often used as a better solution over bidirectional mapping. However, there are a few circumstances where one could defend bidirectional mapping, like for very simple CRUD applications. One framework that does support bidirectional mapping is Glue.
On the roadmap for AutoMapper, support for Silverlight and Mono is on top of the list. Also “First-class reverse mapping support” was listed, so InfoQ asked Jimmy if he’s planning to add bidirectional mapping support:
Personally, I don't really want to support two-way mapping, as those asking for it are really asking for first-class ActiveRecord support in ASP.NET MVC (something that Rails still has the MS solution beat by a large margin). If you look at how Rails processes form posts, it's a straightforward affair. I'm still hoping not to do the feature, as I think it works against design concepts like POJO/POCO, the domain model pattern, etc. More people are asking for a Silverlight release in any case :)
Do you think AutoMapper should add bidirectional mapping?
Community comments
Comparison with Dozer for Java?
by Martin Klinke,
Comparison with Dozer for Java?
by Martin Klinke,
Your message is awaiting moderation. Thank you for participating in the discussion.
I have used Dozer (dozer.sourceforge.net/) for "domain object" to "DTO" mapping in Java and was very satisfied with this approach. So far I haven't found a similar solution for .NET/C#. Has anyone experience with both "worlds" and can recommend a tool that does with C#-objects what Dozer does with Java-objects? Is it AutoMapper?
Though Dozer has two-way mapping capabilities and I have incoming and outgoing objects and thus mapping requirements, I didn't use Dozer's built in approach. Rather I have defined a separate mapping for each direction, because I may want to transfer some properties on the way out but not automatically write back the values that are submitted by a service consumer (e.g. timestamps for optimistic locking - I do need to use them, but I don't want them to be mapped back as this would work against the concept of locking ;).
So I don't really need bidirectional mapping. I think AutoMapper would be a solution for the above mentionned requirements and would be glad to hear some real-world experience.