Skip to content

Instantly share code, notes, and snippets.

@leekelleher
Last active August 29, 2015 14:06
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leekelleher/513c48c634fc44729900 to your computer and use it in GitHub Desktop.
Save leekelleher/513c48c634fc44729900 to your computer and use it in GitHub Desktop.
Ditto - Example of mapping an Archetype property to custom POCO model/type

Here is an example of using Ditto to map an Archetype property to custom POCO model/type.


Let's say that we have an Archetype to represent some SEO meta-data, and we'll call the DocType property "metaData".

We'd have 3 properties in the Archetype:

  • metaTitle
  • metaDescription
  • metaKeywords

Then for our MyPoco POCO that we'd like to map to our content-node (DocumentType), we would have a property called MetaData (of a custom class/type also called MetaData).

Special note: On the custom class/type MetaData, we used the TypeConverter attribute to declare the custom converter. This means that whenever Umbraco's .GetPropertyValue<T> is called, the associated TypeConverter will be used.

So when Ditto attempts to map the "metaData" DocType property with the POCO's MetaData property - the MetaDataTypeConverter will be called.

Important note: Because Archetype comes with its own custom ValueConverter, the value type that is passed into our MetaDataTypeConverter will be of type ArchetypeModel (as opposed to a JSON string).

From there, our custom converter can use the ArchetypeModel to access its fieldsets and nested property values - see the ConvertFromArchetype method.

namespace Our.Umbraco.Ditto.ArchetypeExamine
{
public class MyPoco
{
//
// other properties go here
//
public MetaData MetaData { get; set; }
}
}
using System.ComponentModel;
namespace Our.Umbraco.Ditto.ArchetypeExamine
{
[TypeConverter(typeof(MetaDataTypeConverter))]
public class MetaData
{
public string Description { get; set; }
public string Keywords { get; set; }
public string Title { get; set; }
}
}
using System;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using Archetype.Models;
using Umbraco.Core;
namespace Our.Umbraco.Ditto.ArchetypeExamine
{
public class MetaDataTypeConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(ArchetypeModel))
return true;
return base.CanConvertFrom(context, sourceType);
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is ArchetypeModel)
return ConvertFromArchetype((ArchetypeModel)value);
return base.ConvertFrom(context, culture, value);
}
private MetaData ConvertFromArchetype(ArchetypeModel value)
{
var fieldset = value.FirstOrDefault();
if (fieldset == null)
return null;
return new MetaData()
{
Title = fieldset.GetValue<string>("metaTitle"),
Description = fieldset.GetValue<string>("metaDescription"),
Keywords = fieldset.GetValue<string>("metaKeywords")
};
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment