Effective reference to parent elements in the object model

I have the following object model:

- Book 
- Chapter 1
--- Page 1
---- Image 1 
---- Image 2
---- Text 1
--- Page 2 
...

Resources go down at page level. But I need to know the full path to resources, in terms of resources.

One way is to inform the resources of their parents.

Thus, the my Image object can have the parentPage property, which in turn can have the parentChapter property. That way, I could access the full path through currentImage.parentPage.parentChapter. Is there a better way?

A few words about why I need to know the full path in terms of resources. I have an object model that passes and is displayed on the screen. The visualization tool descends from the level of the chapter down to the level of the element / resource (this happens during rendering). However, to display resources, I need to know where they live (i.e. the actual path on the disk), and this information is usually indicated at the chapter level.

Thank!

- Edit - To clarify if parent.parent is the best approach? This makes children's objects aware of parents, which makes me uncomfortable. Clutch?

+3
source share
4 answers

Regardless of whether you use the tree structure of Zachary or you do it more specifically, the question is how to live.

, , , , ().

, , .

+2

, node.

#:

class TreeNode {
 public TreeNode Parent { get; set; }
 public List<TreeNode> Children { get; set; }
}

class Book : TreeNode {
 ... book attributes ...
}

... other classes ...

, , , , ? , , . , . , , :

class TreeNode<TParent, TChild>  
{
    public TParent Parent { get; set; }
    public List<TChild> Children { get; set; }
}

class Book : TreeNode<object, Chapter> { }
class Chapter : TreeNode<Book, Page> { }
class Page : TreeNode<Chapter, object> { }

, !

+6

, .

0

, xml linq xml .

, , ( wikipedia)

0

All Articles