Click here to Skip to main content
15,902,737 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi everybody,

I have this structure for my program (it's a simple book management).
MyLibraries
-Library1
--Shelf1
---Book1
----Cover (img)
----Metadata (XML, contains things such as publisher's name and so forth)
----BookFile (
---Book2
----...
---Book3
--Shelf2
---...
-Library2
--...


And so forth.
So these are the classes I created:

C#
public class Book:INotifyPropertyChanged{
   //...other properties
   public string Path
}

public class Shelf{
   public string Name;
   ObservableCollection<Book> BooksOnShelf;
}

public class Library {
   ObservableCollection<Shelf> Shelves;
   public string Name;
}


The libraries are stored on disk in a particular fixed location and the folder tree follows the Library-Shelf-Book tree (so if I have "Basic C++" under "C++" shelf which is under "Computer Science" Library, the folder the book is stored in is "<mylibrariesfolder>/Computer Science/C++/Basic C++").

The problem is to calculate the path from the Book class based on which collection contains it.

I came up with two solution (but I do not particularly like them):

1. Add a Shelf object to the book and a Library object to the Shelf so that I can get the parent Shelf of a book and the parent Library of a Shelf

2. Create method inside Shelf that gets the path of the book and a method inside Library that gets the path of a shelf. Then I combine them together.

I do not like both, so my question is: is there another solution, cleaner and more "error-safe" than mines?

Thanks a lot for your help and time.

Gianmarco L.
Posted
Updated 22-Oct-15 10:13am
v3
Comments
xszaboj 22-Oct-15 16:00pm    
Why exactly don't you like the first option? have a parent in your node? (shelf in a book and library in shelf?)
LLLLGGGG 22-Oct-15 16:08pm    
Because if I want to change the shelf the book is in I'd just want to change the collection it's stored in and not both... just in order to be more error-safe.
phil.o 22-Oct-15 16:13pm    
The best way, then, is to do the change from one method only, and this method takes care of updating both parent and child properties, on both side. ;)
Why the collection in the class Shelf is not simply called Books? (I'm a consistency-freak)
xszaboj 22-Oct-15 16:35pm    
ok, get your point. But if you make object Shelf with method Update in it. You can easily implement the logic. Even better would be have parent property readonly and set it only through constructor. So you would have Shelf(string LibraryPath) or Shlef(int LibraryId) what ever you are using :) and Book(ShelfPath/ShelfId).That is another thing that will add you some consistency :) just my thoughts how to improve your code. Criticism is welcome
LLLLGGGG 22-Oct-15 16:58pm    
I think I'll go with this solution and unify the two changes in a single method.
Thanks a lot for your help. :)

1 solution

Technically, having a Children property in the parent class, and a Parent property in the child class, is much easier than having only a tree-relationship between your objects. Of course, you could try to implement your own version of Tree (Data structure)[^], but by adding just one property you can easily hide all this complexity.

And I would not give the Book class a Path property. I would give it a Title, for sure, but retrieving a path is not the responsibility of the Book class; it would better be a method string GetPath(Book book) in a LibraryCollection class containing an ObservableCollection of Library objects, as per the requirement you defined.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900