0

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.

3 Answers 3

1

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.

3

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.

0

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.

0

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