tags:

views:

54

answers:

3

Note: WinForms C# Early Learning Level!

I would like some assistance with the best way to implement a situation in a C# object environment. I cannot post code because it doesn't exist yet, however I can give you an alternative real-life comparison.

In simple terms (because thats how I work) I have a Book class. I have a collection (<List>) of these Book classes. Within each Book class I have a collection (<List>) of Chapter classes. The book Class and the Chapter class are based on completely different abstract classes. These are defined in a separate business layer and data is loaded from a data source layer. Bear in mind that there are many books and many chapters.

From my GUI layer I can retrieve a book and it’s underlying attributes based on the collection index/key. I can also get all the chapters within the book via the Book class.

What I need to do is to be able to get any Chapter class from any Book without having to go via the Book class. What’s the best way to make this happen?

Thanks guys and gals

+2  A: 

What do you mean by "get". Would you expect to have all Chapters in memory? Or are we searching a database?

What information identifies a Chapter?

Let's suppose that all the Chapters (or perhaps if not the full text for a Chapter then a ChapterDescriptor) are in memory. You have a Collection of Chapters keyed by ChapterIdentity (perhaps BookId + ChapterNumber).

So then it's just

ChapterCollection.getById(aChapterId);

When you add a Book you need to add all its Chapters to the Collection.

More likely this is coming from a datbase. So you want the ability to retrieve Chapters by id. That all depends upon how you are doing persistence, but in concept can look just like the in-memory example from the point of view of the retriever.

djna
'get' would mean search for and retrieve a reference to a chapter from memory which can then be used to query a db for all details. A chapter would have a unique id along with it's title. Are you suggesting I keep a separate collection of books and chapters and then link the them rather than having the hierarchical relationship of chapters in a book?
You can have both a hierarchical relationship and an "all chapters" collection. It's quite reasonable to have several access paths through your object model. However, I'm not actually sure that its reasonable to keep the entire set of chapters in memory - wouldn't work for the Libraray of Congress I guess? So a service api offering getById() could hide whetehr or not the whole thing is in memory. Do a database retrieval if necessary.
djna
+1  A: 

Why is it that you don't want to go through the book class? Really you only have two options - searching through the book class objects' lists or maintaining a secondary collection object that contains all the chapters from all books. I'm assuming there will be a certain key you will be searching with that is an attribute of the chapter class? If so you can use a Dictionary(of key, Chapter) which will be fast at searching, or else keep a List(of Chapter) and use LINQ to search through it. It depends on what you are searching on and your reasons for avoiding the book class.

link664
The second collection option was the only thing I could come up with and it just seemed that there would be a better way to do it.
A: 

If you are not going throught the book classes, you need to have a "library" collection that contains references to all the chapter objects.

awe