MVC Design Pattern – Combining Multiple Models for Use

designmvc

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.

Best Answer

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.

Related Topic