user2470057
user2470057

Reputation: 537

MVC variables in model or controller?

I am trying to understand the MVC Pattern, and I finally understand a lot of it. There is one concept that I don't quite understand yet. I have looked through all the posts on here that try to explain MVC, but this one question isn't answered clearly yet.

Do you create variables in the model or the controller or both?

I can see someone passing variables from the controller to the model to change the data held within the variables, but would it be better to create them in the model then just call their values from the controller? Or would it be better to create variables in the model, and copy their values to the same variables in the controller?

If you know, please explain why one is better than the other, please. I am asking to understand, not just to know the right answer. Thank you.

Upvotes: 1

Views: 1520

Answers (2)

user2470057
user2470057

Reputation: 537

Persistent data needed throughout the lifetime of the application should be held within the Model. Model method calls to set, get and manipulate the data within the Model should be done by the Controller.

Temporary data needed by the application or the view (for any reason) can be held in the controller... but no persistent data should ever be held within the controller, as it's considered to be a bad MVC design pattern implementation to do so.

Upvotes: 0

Supun Wijerathne
Supun Wijerathne

Reputation: 12958

If I give a straight forward answer for

Do you create variables in the model or the controller or both?

It doesn't really matter.

The main idea behind Model and Controller is

  • Controller resides only Presentation Logic.
  • Model resides only Business Logic.
  • So that, if you want to present your model with a different presentation logic, you can get your existing Model out and plug it with a new Controller without any problem because your business logic & presentation logic is decoupled(not mixed with each other).

This is the best diagram I found for MVC architecture. Hope you can upgrade your understanding with this.

enter image description here

So in terms of variables, in Model you should make variables only for business logic purpose. In Controller, it's only for presentation purpose. :))

Upvotes: 1

Related Questions