• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

DAO Pattern and Many-To-Many relationships

 
Frederico Costa
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, folks, I am not sure if this is the best forum to ask this very simple question, but as there must be a lot of great architects here I thought it would be ok.

I am making a simple aplication, for academic purposes, to implement the DAO Pattern. So there is a TO (Transfer Object) used to transfer data between the layers, a BO (Business Object) that validates the business logic and function as a facade between the Presentation Layer and the Data Access layer.

There is an entity called Test and another entity called Question. One Test can have one or more Questions and one Question can be in zero or more Tests.

Implementing this relation on a Relational DataBase we need a "link table" between the Test and Question tables.

So far, everything is ok.

Until when I need to separate the responsabilities between the DAOs.
What is the best approach for this problem?

is the TestDAO who is responsible for inserting the Test and making the associations with the Question table? Or I should create another DAO to encapsulate this logic?

Thanks in advance!

Frederico Costa
[ June 17, 2008: Message edited by: Frederico Costa ]
 
giuseppe fanuzzi
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello Federico
it depends on your functional requirements.
For example:
if inserting a test you have to insert also his questions, then this function is of TestDAO
if you can also insert, indipendently, a question (and, also, join it in a test) then you need a QuestionDAO.
 
Fred Muhlenberg
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Frederico,

Your description of the Test and Question entities describes constraints, not the implementation. Yes, there is a link table in the database but on the object level, the Test containing zero or more Questions.

The next question to decide is: does a Question know about a Test. Probably not. Why? A Question is likely an atomic entity that can be reused in multiple Tests.

Data/Information needs to flow one direction. A Test entity know about the Questions -- it contains them. The Question entity know about things below it (like Answers) but not above (like Tests).

So, in implementing your DAO's, you need a DAO for the Question entity. You need a DAO for the Test entity.

Included in the TestDAO is the responsibility to update the link table and the responsibility to use the QuestionDAO to save any Questions.

-Fred
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic