| layout | title | category | since_version |
|---|---|---|---|
datatype |
uMapper |
root |
5.1.0 |
uMapper lets .NET developers map & query nodes as strongly typed objects with minimal configuration. It does its best to map the properties and relationships of a node to an object by name, but features a fluent interface for manual configuration in the much the same way AutoMapper does.
I've put together an example project with Umbraco 4.9.0 which shows example usage of uMapper. A SQLCE database is included as a file, and the username and password for the Umbraco back office are both "admin".
-
Add a reference to
uComponents.Mapping.dllin your web project (this should already be added if you used NuGet to install uComponents). -
Create your document types in Umbraco:
Make sure you use CSV as the data format for any multi-node pickers you use. -
Create your corresponding models as .NET classes in your project, naming the classes the same as the document type aliases: {% highlight c# %} public class Artist { public string Name { get; set; } public IEnumerable Genres { get; set; } // You can also use List } {% endhighlight %}
-
Derive from
ApplicationStartupHandlerand create your maps (these must be created before you start mapping nodes, or uMapper doesn't know which classes to map to). Now is also a good time to enable caching (which uses theHttpContext.Current.Cache): {% highlight c# %} using uComponents.Mapping; using umbraco.businesslogic;
public class ExampleStartupHandler : ApplicationStartupHandler { public ExampleStartupHandler() { uMapper.CreateMap(); uMapper.CreateMap(); uMapper.CreateMap();
uMapper.CachingEnabled = true;
}
} {% endhighlight %}
-
Get some nice data! {% highlight c# %} public partial class Site : System.Web.UI.MasterPage { protected Homepage Model { get; set; } protected IEnumerable Artists { get; set; } protected IEnumerable Genres { get; set; }
protected void Page_Load(object sender, EventArgs e) { this.Model = uMapper.GetCurrent();
this.Artists = uMapper.GetAll<Artist> .OrderBy(artist => artist.Name); this.Genres = uMapper.GetAll<Genre>(); var featuredGenre = uMapper.Find<Genre>(1234); // node ID is 1234} } {% endhighlight %}