1

I'm trying to implement MVC in a small school project, but I'm having some small doubts, I hope somebody can help me:

1.- if I'm having 5 views, I think I need 5 models, one for each view, but do I need 5 controllers?, or just one controller that will interact with all of them.

2.-I need to add data from a DB, where do I make the select query, in the model, or in the controller?

1
  • You got the clarification about where to use 1 controller and where to use multiple controllers? Commented Jul 7, 2013 at 18:30

4 Answers 4

2

1.- if I'm having 5 views, I think I need 5 models, one for each view, but do I need 5 controllers?, or just one controller that will interact with all of them.

No, that's the beauty of MVC. One model can be used by many different views. The model should be written so that it has no knowledge of the views and no need for this knowledge. You may need 5 controllers, but that's not a given. It's possible (though unlikely) that you will only need one control.

Also note that you may in fact need several models if you require several different data sources and "business rules" for the program.

2.-I need to add data from a DB, where do I make the select query, in the model, or in the controller?

That sounds like a control issue.

Per comments, note MVC was not created as an answer to limited resources, but as a way to allow for the creation of modular program code to allow for the reduction of coupling and an increase in cohesion. This should make it much easier to upgrade, enhance and debug code.

Sign up to request clarification or add additional context in comments.

9 Comments

+1 beautiful explanantion..I have Just a little doubt though..What is the soul purpose of controller?
@Mac: the controller ties everything together.
Thanks for clarification sir. But if controller ties everything then isn't deploying 5 controllers a wastage of resource?
@Mac: MVC was not created as an answer to limited resources, but as a way to modularize a program code to reduce coupling and increase cohesion. This should make it much easier to upgrade, enhance and debug code.
@user2558554: it all depends on context. Most of the time your code will fail, since control is usually specific to the view.
|
2
  1. It depends on what domain concept these 5 views represent. If they are representing different views about same domain concept such as List of Students, Details of student, etc. then, you will have 1 model and 5 views. It is also a good idea to have one controller per domain entity.

  2. You will use specialized classes called DAO (Data access objects) to interact with Database, and the methods of DAO will be typically CRUD operations (Create, Read, Update & Delete). You will invoke DAO from controller.

3 Comments

1+ for adding useful information not covered in other answers.
well, each view shows different things, would be something like, students, proffesors, departments, etc. something like that
Yes, in that case you will have 5 models and 5 controllers. Controller should logically be tied to single entity for clarity. So, you will have StudentController, ProfessorController, etc. Also, I believe you will need multiple views per entity - a page to display list of students, a page to add a student, and so on. So, StudentController should handle the various aspects related to student entity, and so on.
1
  1. Models, views, and controllers need not map to one another 1:1.
  2. I would normally consider this to be controller code, but it may somewhat depend on your particular situation.

Comments

0

1) depends, on what you are trying to do. Ideally, there should be one to one mapping between views and controllers. However, you may create only one controller for multiple models and try to manipulate the views using the view ids.

2) Select query should normally be in controller

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.