1

In MVC, does the view component deliver a new view to the user directly or indirectly via the controller component? says

The first thing to realize is that Server-side Web MVC (e.g. ASP.NET MVC & similar where controllers handle requests and views render to HTML) is not the same as client-side/desktop MVC/MVP UI pattern. In the UI pattern, generally the View component is the view (it doesn't create one). Also modern view widgets have the capability to detect user input, back when MVC was first created, widgets had no such capability (they were just pictures on the screen), so every widget had it's own MVC, where C handled the input - in modern MVC, C implements the nontrivial behavior of a larger view.

I was wondering why and how "Server-side Web MVC (e.g. ASP.NET MVC & similar where controllers handle requests and views render to HTML) is not the same as client-side/desktop MVC/MVP UI pattern"?

Are they different variants of MVC pattern?

Thanks.

2 Answers 2

4

MVC is a pattern, a model, a principle, a way of thinking. Assigning roles and responsibilities. If you need to test an implementation x to see if it is MVC, you need to prepare a series of questions to test against. Do we have a separation of data, presentation and control flow? That's it. The pattern does not concern itself with details of it's implementation.

4

That comment was made by me as an offhand remark; it was probably too handwavy, so I better elaborate on it a bit. The problem is that both use the same terms and similar high-level concepts, but there are certain non-trivial differences.

From a historical perspective, (classic) MVC came about in a language called Smalltalk, in the late 70's and the 80's. Today, various UI libraries/frameworks provide you with ready-made controls (a.k.a widgets1) - buttons, check boxes, radio buttons, sliders, scroll bars, text boxes, etc. These can detect events like clicks, button presses, and have various other capabilities like data binding and whatnot. Back then, that wasn't the case - all those were just pictures on the screen. So people came up with a pattern: each widget would be made out of a View - an object that would render it (combine visual elements and display it), and a Controller - an object that would detect and interpret user input, turn it into a command of some kind, and invoke some domain function within the business logic code, and instruct the view how to adjust itself afterwards. Generally, each view would have some piece of the business logic associated with it. This piece of the business logic - some class or a group of interacting objects - is the Model associated with that view. So the Model is not a data structure, it's a business object. It's also not a model of the view (as in "view model"), that's something else. It's just an object that implements a part of core application functionality (what the application actually does). So in the original version of MVC, each button, text box, check box, etc. was made from its own MVC triad.

Now, over time, widgets became smarter, applications got bigger, and the pattern evolved into what we know as the MVC/MVP pattern today (the P stands for "Presenter"). The View is now more complicated - it's some kind of a specialized panel, or a form, or a screen, usually composed out of a bunch of more primitive widgets (buttons, labels, text boxes). The View is still really about rendering, but it now has more advanced capabilities - you can do things like subscribe to events of a particular button, or adjust a layout by altering a property, etc. The Controller is still about interpreting user input, interacting with the model, and governing presentation logic, except some of the more generic view-related behavior is now left to the view.

The whole idea is to separate presentation logic from the business logic. The presentation layer contains a bunch of view-controller pairs, and the business logic layer is unaware of their existence - models don't know about (don't depend on) views and controllers. There are several variants of the pattern; in some the View contains a bigger chunk of presentation logic, in others nothing but the very basic stuff - it just knows how to draw itself, and all the manipulation (what's enabled when, what should change color under what circumstances) is placed in the Controller or the Presenter. These variations are driven by different concerns, testability being one of them.

Now, one variation of particular interest (in the context of this answer) is called Presentation Model. There, the idea is to represent a view in an abstract way, independently of the GUI framework used, by basically putting data that represents various aspects of the View into the Presenter (or a closely related class - the Presentation Model), and manipulating that data, rather then the View itself. The view is then updated by some synchronization mechanism, like data-binding. GUIs developed in .Net with WPF are organized around this pattern, but in that context it is called Model-View-ViewModel (MVVM). Here, the Model is still the what it used to be (the code that does the actual work of the application), and a ViewModel is the Presentation Model - an abstract representation of the view that resides in the presentation layer.

Finally, we arrive at Web MVC. There's a bunch of web MVC frameworks that are designed around an MVC-like structure. This is all happening server-side. Here, views are "rendered" to HTML and sent back as the response to a web request. So a View here is less an actual view, and more a thing that knows how to construct a page; this is usually done via some sort of HTML templating engine. There are often "view models" involved, e.g., they are commonly found in ASP.NET MVC. But these are just plain data structures that are passed to the view, and not ViewModels in the sense discussed above (and they are most certainly not Models (the M in MVC), although some people seem to think so). BTW, I'm not saying that's bad, just that it's different. The Model, as before, resides in the business logic code of the web application. Also, in Web MVC, you can't really ignore the fact that it is happening server-side and that the view will be transferred back over the web, and this puts certain constraints on the way you implement things. The cadence of the interaction is also different, and is dictated by the request-response cycle. So, as I've said before, it's a bit confusing because the high-level concepts are the same, but there are aspects that differ, and the terminology is slightly different.

The core characteristic of MVC is the separation of the presentation logic. Keeping that in mind, another thing worth pointing out is that using these web MVC frameworks doesn't make that automatically happen. So choosing a framework does not in itself amount to choosing an architecture; rather, MVC is a pattern that's applied by the developer team in order to achieve said separation. If you are not deliberate about this separation, you'll end up with a tangled mess of dependencies, with some of the business logic in the controllers, and some of the view-related code in the business objects. Unfortunately, a lot of the examples on the web lean towards that kind of code.

There are also client side (JavaScript) MVC frameworks, but the roles various MVC components play there are more in line with the MVC/MVP UI pattern, it's just that the execution environment is the browser.


1 For this particular discussion, I'll prefer the term "widgets" rather then "controls" in order to avoid potential confusion with "controller".

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.