views:

7

answers:

1

Hi,

i need to set up a IS-A relationship with coredata.

I have a Page class which has the following structure

PROPERTY title PROPERTY layoutType RELATIONSHIP layout

Now, i have three classes: ImageLayout, TextLayout, and SlideshowLayout. I want the Page.layout relationship to refer to one of these three classes depending on the layoutType property.

How can i do with coredata? Or there is another way to do this? Keep in mind that the number of layouts can grow in the future, so i can't simply put all the properties in page and leave empty the ones that are not related to the layout of the page.

Thank you in advance!

A: 

You use entity inheritance. You create a parent entity that can be abstract or concrete. Then you assign the parent to the Page.layout relationship. When you need different finds of layouts, you create subentities (like a subclass) of the parent. The object graph will accept any subentities in the relationship.

E.g. Create an abstract entity called layout. It may have no attributes save the relationship like so (pseudocode):

Page{
    //...various attributes
    layout<-->Layout.page
}

Layout{
    page<-->Page.Layout
}

ImageLayout:Layout{
    imageName:string
}

TextLayout:Layout{
    text:string
}

SlideshowLayout:Layout{
    numberOfSlides:int
}

You can assign any single instance of ImageLayout, TextLayout or SlideshowLayout to the Page.layout relationship of any Page instance.

TechZen
Thank you TechZen! Thank you a lot! :)
Paolo Sangregorio