views:

30

answers:

3

Hello,

This is theory question but any advice is more than welcome..

When designing OO applications according to many books you should have a class for every table of the database with the fields as class variables..

But when you want to get data for multiple tables and store it in an collection of objects you find out that you need an object that will have variables from more than one table..

Example: Need to get a list of Books with ISBN/Author's First Name/Author's Last Name/Category The actual data are in 3 tables: Books/Authors/Categories and the IDs of the Author/Category are as foreign keys to Books table..

What is the best design approach? Add to the Book class more variables (Author's First Name/Author's Last Name/Category) or not??

Hope it's clear..

Thanks for any response.

A: 

It seems reasonable that the category and the author's name is a property of the Book-class. As for implementation, you could create a service, that fetch all information from the different tables to read a object of the class Book.

crunchdog
Thanks very much for replying..There is no other approach right??Adding properties from other "entities" is the only way?
Nikos
A: 

You don't have to have a class for every database table there are other approaches. Domain Driven Design (DDD) is a set of guidelines which is a top down approach to designing your applications. In a DDD world you write code to solve business problems not to model your data layer. There are many instances where your database schema greatly differs from your domain design.

Eric Evans and Jimmy Nilsson have both written excellent books which I'd strongly recommend you have a look at.

Kane
Thank you very much,I need to implement this using OO design, but i'll have your advice in mind.
Nikos
A: 

"One class per table" is a rule of thumb, good to start with. But it's not carved into stone. You can (and should) assess the needs of your app, the forecasted usage patterns of your data etc. It is not uncommon to map several tables to a single class, neither the opposite: to represent a single table to multiple classes.

It is recommended to design your classes based on the OO paradigm (as opposed to just trying to map them to your table structure). If needed, you then fine-tune your class design to match the requirements from your data layer.

In your concrete case, it depends on whether you foresee the need to fetch book data alone (without author & category), or is it more typical that you need all 3 at once. It is also a question how many properties the author and category have.

If you often fetch only the book data and/or the author & category have many properties, it makes more sense to keep them in separate classes. If you keep them in one class, you will have lots of duplicated data (because many books will have the same category or author data), and you will always need to unnecessary load 3 rows with a join from DB, so you pay a performance penalty for nothing in most of the cases.

OTOH if you usually fetch data from all 3 tables at once, it might make sense to keep them in a single class. Even then, I would opt for separate classes though, to avoid duplication of category data. Moreover, you may even be able to cache your categories & authors better if these are in separate entities rather than part of the Book class.

A lot depends on your development language and tools as well; e.g. in Java with an ORM tool like Hibernate, it is much easier to set these mappings, use lazy fetching and fine-tune the fetch plan as you learn more about your application, then e.g. using JDBC.

Péter Török
Thanks very much for the reply!Indeed, i decided to have separate classes for Author and Category and withing the Book class has a book's properties a Writer and Category object.
Nikos