Copied to clipboard

Flag this post as spam?

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


  • Siavash Mortazavi 24 posts 145 karma points
    Nov 17, 2016 @ 18:59
    Siavash Mortazavi
    0

    How to Make Document Type Inheritance by Composition?

    Hi, I'm using Umbraco 7.5.4, and I'm trying to use document type composition and ModelsBuilder to have strongly-typed models.

    I want to have a simple hierarchy:

    Page Base ==> Content Page Base ==> MyPage

    For "Content Page Base" I have clicked Compositions and ticked "Page Base", but for MyPage when I click Compositions, I don't see the "Content Page Base" option, which I think it is because it Content Page Base is already in use by another composition.

    Thinking about OOP, this doesn't make sense, because the possibility of chaining types should be endless.

    Also, since Umbraco creates interfaces for ModelsBuilder, multiple doc type inheritance should be very possible.

    So, after creating: public partial class PageBase: PublishedContentModel {..} and public partial class ContantPageBase: PublishedContentModel, IPageBase {..} why Umbraco cannot create a public partial class MyPage: PublishedContentModel, IPageBase, IContantPageBase {..}

    or something similar to that?

    Can someone please provide a solution to achieve this?

    Thank you.

  • Brian Lacy 28 posts 101 karma points
    Nov 23, 2016 @ 17:28
    Brian Lacy
    0

    In terms of what you're trying to do, bear in mind you're talking about Composition, not Inheritance.

    If I understand correctly, you intend for your MyPage doc type to have some properties which are inherited PageBase, and some properties which are inherited from ContentPageBase. To achieve this, you've got MyPage "inheriting" ContentPageBase which in turn "inherits" PageBase. This is classical inheritance.

    What you want to do instead is "compose" your doc type of independent parts. MyPage should probably derive from two different "base" doc types -- maybe call them "PageBase" and "ContentBase".

       MyPage inherits PageBase, ContentBase
    

    Unlike classical inheritance, composition is clean and flexible. Component types are like individual building blocks, which you can arrange in any order you choose to build whatever you want. Classical inheritance is more like building a steep pyramid, where at any given moment the whole thing could come crashing down...

    But I digress. My point is, thanks to the Composition features of Umbraco's Doc Type system you can reuse these "base" components as often you like, in as many different doc types as you like, without running into the "I can't use this because it inherits from that" and "I can't change that because it's inherited by this other" problems of multi-level inheritance.

    Personally, I have a whole folder in my Doc Type tree in Settings called "Base Types" (although in retrospect I probably should have called it "Component Types").

Please Sign in or register to post replies

Write your reply to:

Draft