M.Amin
M.Amin

Reputation: 402

How strict is the mvc pattern with model and view interactions?

I am confused about how model and view can interact

I was making a simple to do app with mvc pattern and I saw an article which said you shouldn't pass the model values directly to the view, which made the project more complex than I thought (I am relatively new to programming and this is the first time I am trying out a design pattern).

But then later on I talked to someone who said that that is not true and you can send the model data directly to view, he didn't even use classes or some kind of grouping to separate the function he just put them in separate files.

I was wondering if there is a guideline that I couldn't find or we can do whatever we want as long as they are kind of separated. I would love an article or a guide to read up on as well.

Upvotes: 1

Views: 128

Answers (1)

Clayton Long
Clayton Long

Reputation: 638

Since, I am not 100% sure the context in which you are trying to apply the MVC pattern, a good generic explanation of MVC can be found in GoF's 1995 book, Design Patterns: Elements of Reusable Object Oriented Software.

In the book, they state the following.

The Model is the application object, the View is its screen presentation, and the Controller defines the way the user interface reacts to user input.

A more robust explanation can be found from Martin Fowler where he also makes the case for a variation of Model View Controller that uses a Presentation Model.

If you are referring to Spring MVC then there is some magic that blurs the lines a bit. But in general, you have a controller that represents some screen or an encapsulated piece of functionality that the user (web requests) interact with. The controller serves up responses that are derived from the domain, usually via a Spring Service (i.e. @Service). The domain (Model) doesn't know anything about the View and the View may or may not know anything about the domain.

Given that, the View should be derived from the Model. But that's not always the case since sometimes how we present things to a screen is not the best logical way to model things in our domain - not to mention, the domain should be presentation agnostic. This leads into Fowler's argument for a Presentation Model, which is a model that belongs to the Presentation.

I call this a Presentation Model because it's a model that is really designed for and thus part of the presentation layer.

Microsoft took that idea and ran with it in a variant of MVC called MVVM (Model View ViewModel).

You can read more about that in Microsoft's documentation on ASP.Net Core.

So, back to your original question of "Should you pass the model directly to the view?" If you are using MVC then the controller is what provides the interaction. But if you're really asking, "Can you bind your view directly to the model?" If your model has all the stuff you need organized how your view needs it, then sure. And if it's simple enough, maybe that's the way to go. Otherwise, you could go with something like a Presentation Model or MVVM.

Upvotes: 1

Related Questions