2/26/2006

Books are in a Library or Library has books or is it both?

Is it silly? but for a developer doing object relational mapping this could be interesting.
Suppose we model this relationship as bidirectional, i.e. Book *<---->1 Library, then, when we load/send a book from database, we load book’s library (since book references library), and all the books in that library (since library refers books).
If we model this relation as a unidirectional--Library has Books (Library 1 -->* Book), then, since book doesn’t refer to library, we don’t have the previous problem of loading and serializing all the books. However, our book does not know which library it came from. If we need to load library, we still end up loading all the books in the library.
Suppose we model this relation as Books are in library, i.e. Book *-->1 Library, then, when we load book we get book’s library, but since library does not have books we don’t end up reading all the books; When we load library we load only library. I choose option Books are in library (Book *-->1 Library), it’s me.
Object relationships and OR Mapping are very interesting, if we don’t model them properly, reading/sending a small object may result in reading a whole load of things that we are not intended to read/send. Technique is, model relationships to the right granularity to match our object reading and writing patterns. In many places, I find turning a container into a marker, as we did above, to be very helpful in getting to right object granularity. Then, what about finding books in a library? If need be, we can always find books in a library by query: get all books whose library is x.
How should we model relation between Book and book pages, Pages are in book or Book has pages or both?

0 Comments:

Post a Comment

<< Home