Copied to clipboard

Flag this post as spam?

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


  • Ismail Mayat 4511 posts 10092 karma points MVP 2x admin c-trib
    Nov 04, 2015 @ 12:30
    Ismail Mayat
    0

    Ditto with archtype

    Lee,

    I followed your gist example for using ditto with archetype however I am having some issues. My situation is this:

    My poco:

        public class GlobalContent
    {
        public RelatedLinks PrimaryHeaderLinks { get; set; }
        public RelatedLinks GlobalSecondaryHeaderLinks { get; set; }        
        public IEnumerable<MultiBlockContent> GlobalFooterColumns { get; set; }
    }
    

    The first 2 properties all map nicely. The second one is MultiblockContent which looks like

    [TypeConverter(typeof(MultiBlockContentTypeConvertor))]
    public class MultiBlockContent 
    {
        public string BlockLabel { get; set; }
    
        public RelatedLinks BlockContent { get; set; }
    }
    

    And the type convertor looks like:

        public class MultiBlockContentTypeConvertor : 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 MultiBlockContent ConvertFromArchetype(ArchetypeModel value)
        {
    
            var fieldset = value.FirstOrDefault();
            if (fieldset == null)
                return null;
    
            return new MultiBlockContent()
            {
                BlockLabel = fieldset.GetValue<string>("blockLabel"),
                BlockContent = fieldset.GetValue<RelatedLinks>("blockContent")
            };
        }
    }
    

    I know what the issue is which is that the archtype can have multi values. So do I handle this, I guess I need to update code in MultiBlockContentTypeConvertor so it then sends back IEnumerable

    Regards

    Ismail

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Nov 04, 2015 @ 12:42
    Lee Kelleher
    100

    Hi Ismail,

    If you move the TypeConverter attribute to the property...

    [TypeConverter(typeof(MultiBlockContentTypeConvertor))]
    public IEnumerable<MultiBlockContent> GlobalFooterColumns { get; set; }
    

    ... and remove it from the MultiBlockContent class.

    Then in MultiBlockContentTypeConvertor.ConvertFromArchetype you can return a collection/list of items.

    Hope this helps (so far)?

    Cheers,
    - Lee

  • Ismail Mayat 4511 posts 10092 karma points MVP 2x admin c-trib
    Nov 04, 2015 @ 13:55
    Ismail Mayat
    1

    Lee,

    Works a treat many thanks, you have a new ditto fan. Anthony was saying we need to start using on all new projects so this is the first one and no doubt the first of many.

    Regards

    Ismail

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Nov 04, 2015 @ 13:57
    Lee Kelleher
    0

    Cool, thanks - that's great to hear!!!

    Cheers,
    - Lee

Please Sign in or register to post replies

Write your reply to:

Draft