Menu

Archetype Mapper for Umbraco Mapper

Updated: November 2, 2015

Ali Taheri

Archetype Mapper is an extension to Umbraco Mapper that helps you to map the content directly to your C# model, therefore you don’t need to de-serialise or parse JSON to retrieve data.

Archetype Mapper like Umbraco Mapper uses conventions to map from IPublishedContent fields to view model fields; Archetype mapper extends it to allow you to map Archetype fields by convention too.

Archetype mapper also supports nested Archetype properties. Here is an example:

Imagine you have a page with a property called "Modules" with Archetype - Homepage datatype.


The Archetype Home Page datatype has 2 field sets as follow.

  1. Video Banner
  2. Carousel


Archetype Mapper Home Page Archetype


The Video Banner field sets has the following properties:

Archetype Mapper Video Archetype


The Carousel fieldsets has only one property called Carousel Items with the data type of another Archetype (Archetype – Carousel Item) 

Archetype Mapper Carousel Archetype


Carousel Item Archetype has the following properties. 

Archetype Mapper Carousel Item Archetype


Now we want to map the above Archetype structure into the following classes:

public class HomePageViewModel : BasePageViewModel
{
    public IEnumerable<HomePageModules> Modules { get; set; }
}

public class HomePageModules
{
    public Carousel Carousel { get; set; }

    public Video VideoBanner { get; set; }
}

public class Carousel
{
    public IEnumerable<CarouselItem> CarouselItems { get; set; }
}

public class CarouselItem
{
    public string Title { get; set; }

    public IHtmlString Text { get; set; }

    public MediaFile Image { get; set; }
}

public class Video
{
    public string VideoLink { get; set; }

    public string VideoHeaderText { get; set; }

    public string VideoBodyText { get; set; }
}

In order to map the content with the above class you can use just use the Umbraco Mapper and Archetype Mapper together to achieve that.

Here is how the new class will look:

public class HomePageViewModel : BasePageViewModel
{
    [MapFromArchetype]
    public IEnumerable<HomePageModules> Modules { get; set; }
}

public class HomePageModules
{
    [MapFromArchetype]
    public Carousel Carousel { get; set; }

    [MapFromArchetype]
    public Video VideoBanner { get; set; }
}

public class Carousel
{
    [MapFromArchetype]
    public IEnumerable<CarouselItem> CarouselItems { get; set; }
}

public class CarouselItem
{
    public string Title { get; set; }

    public IHtmlString Text { get; set; }

    public MediaFile Image { get; set; }
}

public class Video
{
    public string VideoLink { get; set; }

    public string VideoHeaderText { get; set; }

    public string VideoBodyText { get; set; }
}

It's as easy as adding MapFromArchetype attribute to the properties that are Archetype.

That's it for today!

Tags

Umbraco Archetype
comments powered by Disqus

© 2017 - Ali Sheikh Taheri