Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

In my design, I have multiple models and each model has a controller. I need to use all the models to process some operation. Most examples I see are pretty simple with 1 view, 1 controller, and 1 model. How would you get all these models together?

Only ways I can think of are

1) Have a top-level controller which has a reference to every controller. Those controllers will have a getter/setter function for their model.

Does this violate MVC because every controller should have a model?

2) Have an Intermediate class to combine every model into a one model. Then you create a controller for that new super model.

Do you know of any better ideas? Thanks.

share|improve this question
add comment

3 Answers 3

up vote 1 down vote accepted

MVC is better viewed as a logical rather than physical construct.

You are free to create a model out of other models or take the repository (if you have one) in its entirety as the model if you need to. Some frameworks for example promote the use of viewmodels for such purposes when you wish to create a single model that is consumed by the view.

Likewise in the view, you can create multiple views based on the viewmodel. Whilst the code for this may sit in one physical view, logically they are different.

If you find yourself passing round the same model for everything, this might indicate that you have a god object which is obviously best avoided.

share|improve this answer
add comment

You started from the wrong idea that there is a one-to-one relation between controllers and models in MVC. There is no such requirement. In fact, an MVC controller should use as many classes from the MVC model as it needs to perform its operations.

share|improve this answer
add comment

In MVC, Model View and Controller are independent modules. The Controller have ultimate power to determine which Model has to be sent to View to serve the request.

share|improve this answer
1  
Downvoter, please explain –  Bharath Nov 11 '13 at 10:42
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.