the role of the controller in the mvc

Started by
6 comments, last by DiegoSLTS 10 years ago

Hello, i'm not understand the role of the controller in the mvc , for example:
if i have only a model with all my functions that query the db ,do other logic and return to the view the data to show(that calls the model directly) and the model is a singleton why i need a controller?
I read that decouples the model from the view , but this what means?
Can you show me an example of the benefits of the controller?
thanks.

Advertisement

I dont know the definitions of the MVC setup.

To me it appears like a simplification of some good ways to structure software.

You have some components which each do their own thing:

Model(s) (no I/O involved)

View(s) (graphical, network, whatever output you have)

And these are glued together by the controller:

Controller

The controller also handles applying input to the components such that everything is notified when something changes, as well as keeping them in sync (eg. updating view with data manually extracted from model, which is glue basically)

Which practically is just a class that has member variables which may be other classes or references to other classes.

I believe the reasoning behind this pattern is to allow changing the view without disturbing the model or needing to make changes to the model, since the model should be independent of the view and work with and without a view. All the code changes needed are moved from the model (where they would have to be made if you were to make model and view highly interdependent) to the controller. Now neither model or view know of each other, and the controller is the only thing you need to touch to reconfigure your view and model setup.

o3o

View and controller commonly become mixed up in the real world. In a GUI for example, a tree view widget is both a view and a controller.

Strict MVC would have the view do nothing but display data in response to data changes and controller would provide input from the user but it is a stretch to make this strict approach apply in real world. Qt for example has models and views but no controllers - the views take care of those roles themselves.

thanks.
1)Is a good thing to make the model a singleton?
So i have always one instance and i don't fall in possible errors
2)Is a good thing create in the model all the logic with functions and call these functions from the view? or is better to create many models and share some logics with the controller
3)there are some already made mvc framework for winform .net?

Thanks.

View and controller commonly become mixed up in the real world. In a GUI for example, a tree view widget is both a view and a controller.

Strict MVC would have the view do nothing but display data in response to data changes and controller would provide input from the user but it is a stretch to make this strict approach apply in real world. Qt for example has models and views but no controllers - the views take care of those roles themselves.

What is your tree view widget doing that it is bleeding over to being a controller? Any modification of data should just be the view calling the controller. (ie someone goes and hits the delete key on a treeview node, the widget shouldn't be performing any direct operations on the data itself, but something like controller.Delete(treeviewNode.Data)

Basically, one should be able to do all the data operations without the view being involved in anyway, it makes unit testing and debugging easier, and you can re-use the controller logic in multiple views. You can have a tree view, a gantt chart, etc, all using the same controller, doing all the same logic without any duplication.

View and controller commonly become mixed up in the real world. In a GUI for example, a tree view widget is both a view and a controller.

Strict MVC would have the view do nothing but display data in response to data changes and controller would provide input from the user but it is a stretch to make this strict approach apply in real world. Qt for example has models and views but no controllers - the views take care of those roles themselves.

What is your tree view widget doing that it is bleeding over to being a controller? Any modification of data should just be the view calling the controller. (ie someone goes and hits the delete key on a treeview node, the widget shouldn't be performing any direct operations on the data itself, but something like controller.Delete(treeviewNode.Data)

Basically, one should be able to do all the data operations without the view being involved in anyway, it makes unit testing and debugging easier, and you can re-use the controller logic in multiple views. You can have a tree view, a gantt chart, etc, all using the same controller, doing all the same logic without any duplication.

and if i move the controller.Delete(treeviewNode.Data) in the model and becomes model::instance.Delete(treviewNode.Data) is not the same?

View and controller commonly become mixed up in the real world. In a GUI for example, a tree view widget is both a view and a controller.

Strict MVC would have the view do nothing but display data in response to data changes and controller would provide input from the user but it is a stretch to make this strict approach apply in real world. Qt for example has models and views but no controllers - the views take care of those roles themselves.

What is your tree view widget doing that it is bleeding over to being a controller? Any modification of data should just be the view calling the controller. (ie someone goes and hits the delete key on a treeview node, the widget shouldn't be performing any direct operations on the data itself, but something like controller.Delete(treeviewNode.Data)

Basically, one should be able to do all the data operations without the view being involved in anyway, it makes unit testing and debugging easier, and you can re-use the controller logic in multiple views. You can have a tree view, a gantt chart, etc, all using the same controller, doing all the same logic without any duplication.

and if i move the controller.Delete(treeviewNode.Data) in the model and becomes model::instance.Delete(treviewNode.Data) is not the same?

Okay, but what if you want to re-use a view with a different model? the controller gives you that abstraction (which you may not need, depending on scope of your project)

Hello, i'm not understand the role of the controller in the mvc , for example:
if i have only a model with all my functions that query the db ,do other logic and return to the view the data to show(that calls the model directly) and the model is a singleton why i need a controller?
I read that decouples the model from the view , but this what means?
Can you show me an example of the benefits of the controller?
thanks.

First of all, MVC is a design pattern, and it makes sense to apply it only in some problems. It's hard to tell if you need a controller in your example because it's not a real problem and it's also too abstract. What does your "model" represent? When you apply the MVC pattern you're not supposed to have 3 classes named Model, View and Controller, you'll have a lot of classes that represent the state of the whole system (all of them are "the model"), some classes that output the state of some parts of the model (all of those are "the view") and some more classes that handles the input and notify the objects that need to be notified (the controller).

"decoupling" two things means that changing the code of one of them doesn't require to do some changes in the other (they "don't know" each other). Your example is again too simplified to see how decoupling the model and the view would be a good thing, but it's a concept that applies to other patters as well and it's a good thing to have (so making changes is easier). Let's say you have a view that has a button that affects a lot of objects of your model, from different classes that are not related in any way. If you want to call a function on all those objects, your view code must know about all the implementations of your model classes, it must know how to get access to those objects (maybe some arrays in some other class) and it must have a way to tell if an objects needs to be notified about it or not. Now, if you change any of those classes, or add more, or change some rules, you must also change your view. If you have a controller with a function modifyAll that does all this work, your view will always say controllerObject.modifyAll, and it doesn't care about anything else. Of couse, now the controllers and the model are coupled together, but now you can make any number of views ignoring the actual model. If you change something on a model you must change one controller instead of multiple views.

thanks.
1)Is a good thing to make the model a singleton?
So i have always one instance and i don't fall in possible errors
2)Is a good thing create in the model all the logic with functions and call these functions from the view? or is better to create many models and share some logics with the controller
3)there are some already made mvc framework for winform .net?

Thanks.

1 - As I said before, you're not supposed to have one model, the Model in MVC pattern is a group of classes. You should have classes that represent the domain, and those classes would be "the model" in the pattern. If you're making a game with classes like Character, Obstacle and Vehicle, all of those would make the model, and you'll have objects instances of them. Some of those classes will make more sense as singletons, but probably not all.

2 - If you want to apply the MVC pattern it's bad to call model functions directly from the view, if you don't care about the pattern do it the way you want it.

This topic is closed to new replies.

Advertisement