Search code examples
c#model-view-controllermodelcontroller

Explanation for MVC pattern needed


I have a question about the MVC pattern.

For example: I have a simple application that shows some objects (e.g. user datasets) in a listbox and maybe some buttons to interact with the stored data.

In my understanding, the model should provide the User class with its properties and the business logic behind the user interactions.

Where I have to store the objects? I've seen some example MVC programs and there was sometimes a collection stored in the controller class. But why?

Many explanations in the web shows that the model class handle the data tier. For me, a ListObject is a concrete instance from the model class.

Can someone explain to me where I should store the data that is viewed in the GUI?

Thank you!

I've read many blogs and communities, but didn't find any helpful explanation.


Solution

  • > Where I have to store the objects?

    It depends on the programming language you use, its ability to implement MVC pattern and of course your imagination of "how it could be" or "how it should be". With other words it could be Controller and it could be Model as well.

    > I've saw some example MVC programs and there was sometimes a collection stored in the Controller class. But why?

    Some frameworks propose great support for separating view, model, and the layer between them (controller or view model) like Java Spring (MVC - model/view/controller) or WPF (MVVM - model/view/view model). For example, WPF hat strong support of organizing your code such way, that the data which should be loaded into a view, could be stored in view model (read - in the controller analog) as observable objects or observable collections. In that case, changing some properties of observable objects dynamically would not require any additional programming from your side to update the view with new values.

    > Can someone explain to me where I should store the data that is viewed in the GUI?

    As Controller in MVC is kind of a layer between View and Model, the data which should be loaded directly into the View, could be stored in the Controller, to make the way to the View shorter. If the data should be preprocessed – for example validated, filtered and so on, it would make sense to store them first in the Model for preprocessing.

    In general, MVC can have very different implementations whose details depend on the language/framework you use and your own imagination.