Any examples on how to use Ditto to map Nested Content with different documunettypes into a list of a commen interface?
Example:
public class MyViewModel{
public IEnumerable<IWidgit> Widigts {get;set;}
}
public interface IWidget {
public string CommenProperty {get;set;}
}
public class WidgetA : IWidgit {
public string PropertyA {get;set;}
}
public class WidgetB : IWidgit {
public string PropertyB {get;set;}
}
Apologies for the lack of documentation around this part of Ditto - it's on our todo list.
Depending on how you've named your doctypes for WidgetA and WidgetB ... you could do the following...
[DittoDocTypeFactory]
public IEnumerable<IWidget> Widgets { get; set; }
Your doctype names would need to match the class names.
If you need to do anything more custom, then you'll need to roll your own implementation of DittoFactoryAttribute, take a look an example of how it's done here - specifically in the ResolveTypeName method...
Thank you for explaining the "missing link". I also had to add [UmbracoProperty("widgets")] to get it working.
The following example now works:
public class WidgetPageController : RenderMvcController
{
public override ActionResult Index(RenderModel model)
{
var widgetPageVm = model.Content.As<WidgetPageVm>();
return CurrentTemplate(widgetPageVm);
}
}
public class WidgetPageVm
{
[UmbracoProperty("widgets")]
[DittoDocTypeFactory]
public IEnumerable<IWidgetBase> Widgets { get; set; }
}
public interface IWidgetBase
{
string Title { get; set; }
}
public class WidgetA : IWidgetBase
{
public string Title { get; set; }
public string WidgetAproperty { get; set; }
}
public class WidgetB : IWidgetBase
{
public string Title { get; set; }
public string WidgetBproperty { get; set; }
}
Could be really cool if it was possible to explicit map documenttypes to classname, so the names don't have to match.
Be mindful that you'll want to view the code snippets on gist.github.com, as the less than and greater than signs interfered with proper display of the code snippets on skrift.io.
Ditto, Nested Content and polymorphism
Any examples on how to use Ditto to map Nested Content with different documunettypes into a list of a commen interface?
Example:
Hi Morten,
Apologies for the lack of documentation around this part of Ditto - it's on our todo list.
Depending on how you've named your doctypes for
WidgetA
andWidgetB
... you could do the following...Your doctype names would need to match the class names.
If you need to do anything more custom, then you'll need to roll your own implementation of
DittoFactoryAttribute
, take a look an example of how it's done here - specifically in theResolveTypeName
method...https://github.com/leekelleher/umbraco-ditto/blob/develop/src/Our.Umbraco.Ditto/ComponentModel/Processors/DittoDocTypeFactoryAttribute.cs#L42
Cheers,
- Lee
Hi Lee
Thank you for explaining the "missing link". I also had to add [UmbracoProperty("widgets")] to get it working.
The following example now works:
Could be really cool if it was possible to explicit map documenttypes to classname, so the names don't have to match.
This article talks about how to do it with Archetype, which is very similar to Nested Content (pretty much what Lee said): http://skrift.io/articles/archive/building-umbraco-websites-with-archetype-widgets-and-ditto/
Be mindful that you'll want to view the code snippets on gist.github.com, as the less than and greater than signs interfered with proper display of the code snippets on skrift.io.
is working on a reply...