Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Morten Rasch 2 posts 83 karma points
    Jul 27, 2017 @ 11:10
    Morten Rasch
    0

    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:

    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;}
    }
    
  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    Jul 27, 2017 @ 15:12
    Lee Kelleher
    100

    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 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...

    https://github.com/leekelleher/umbraco-ditto/blob/develop/src/Our.Umbraco.Ditto/ComponentModel/Processors/DittoDocTypeFactoryAttribute.cs#L42

    Cheers,
    - Lee

  • Morten Rasch 2 posts 83 karma points
    Jul 28, 2017 @ 09:39
    Morten Rasch
    1

    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:

     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.

  • Nicholas Westby 2054 posts 7100 karma points c-trib
    Jul 27, 2017 @ 15:21
    Nicholas Westby
    0

    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.

Please Sign in or register to post replies

Write your reply to:

Draft