Class BlogPostPart - How can it be composed with properties of content parts

Topics: Core, Customizing Orchard
Nov 2, 2012 at 5:48 PM
Edited Nov 2, 2012 at 5:52 PM

The following is the example code of the BlogPostPart class:

1. Does this mean a content part (BlogPostPart) can be composed of content parts (TitlePart, BodyPart, etc.)? I can't find any code in the data migration file, which define the BlogPostPart to contain other content parts.

2. But in the data migration file, there is a content type definition for BlogPost to contain other content parts (TitlePart, BodyPart, etc.). Dose this mean a content type could be used as content part?

Actually, I want to know how I can creat a content part like this, with reusable content parts.

public class BlogPostPart : ContentPart {
    public string Title {
        get { return this.As<TitlePart>().Title; }
        set { this.As<TitlePart>().Title = value; }
    }

    public string Text {
        get { return this.As<BodyPart>().Text; }
        set { this.As<BodyPart>().Text = value; }
    }

   // ...
}

 

Many thanks, I almost got headache on this.

Developer
Nov 2, 2012 at 8:16 PM

A content part cannot be composed of other content parts. Content parts are only ever attached to content types.

what you see in the above code is the BlogPostPart "casting" itself to another part that is part of the type of which the BlogPostPart is also part of.

So a BlogPost type consists of:

  • BlogPostPart
  • TitlePart
  • BodyPart

What the .As<> method does is this: it goes to the ContentItem property of the ContentPart (from which BlogPostPart, like all content parts, derive). Then it selects the part of the content item of the requested type (the T of .As<T>()).

Have a look at it's implementation to see exactly how it's done, it's pretty neat.

Nov 3, 2012 at 2:41 AM

Thank you skmskywalker! You explained it very clear, I can continue on my project now.