Welcome to Umbraco Mapper

The following is one of a series of posts introducing various open-source projects Zone has developed and made available for others to use. Some come from personal side-projects and others where we’ve been able to identify general solutions to specific problems we’ve worked on for our clients — who we thank for allowing their contributions to these projects to be released in this manner. This article by Andy Butland introduces Umbraco Mapper, an Umbraco package for a content-to-object mapping module that supports developing Umbraco solutions in a pure MVC manner, using controllers and strongly typed view models…

Out-of-the-box Umbraco supports a number of ways of transforming your content into HTML. Even putting aside older techniques such as using XSLT and Web Forms, within MVC there are still a number of ways of working — something I’ve blogged about elsewhere before.

However, our preferred approach is to use a more “purist” MVC approach, where we utilise route hijacking via our own controllers in order to construct a strongly typed view model to pass to our views.

It’s not to everyone’s taste but for us, working in a large team of developers, it provides a very nice separation of concerns between the page rendering logic and the view itself. The view is particularly clean, containing no content retrieval logic or queries; in fact, in most cases, it has no dependency on Umbraco at all. This means a front-end developer can focus on the mark-up without having to worry about retrieving content using the Umbraco APIs.

There’s an extra hurdle to get across with this method, though, which is the mapping of the content held in Umbraco IPublishedContent instances to our strongly typed model. And this is what Umbraco Mapper was built to support. Influenced by AutoMapper which is commonly used by .Net developers for mapping instances of one class to another, this package attempts to do the same thing for Umbraco content.

It uses conventions to support the mapping operations, mapping values for content with the same property alias as the field in the view model. It can also be used to map collections of content as well as that from other sources in formats such as XML and JSON.

Attributes can be used to customise the mapping process, allowing mapping from fields of differently named types, from parent nodes in the tree and from dictionary items.

Lastly, for non-primitive types on your view model you can set up custom mappers, to handle more complex content such as that produced by Archetype and the Umbraco grid.

For more information, to review the code or potentially contribute, please see the project’s GitHub page. To use you can download from our.umbraco.org or install from NuGet.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.