Model-View-Controller? Model-Controller-View? View-Model-Controller?

Status
Not open for further replies.

Bagheera

Ars Tribunus Angusticlavius
8,773
Subscriptor
I'm trying to get a clear grasp on MVC architecture, but there are two completely different views (no pun intended) of MVC.

1) An example of the MVC I learned is here in Apple's developer documentation. This should logically be called "Model-Controller-View," because the View and Model never touch. The Controller passes input and output between them.

-My first MVC app was written like that: The "Model" class had properties that matched columns in a database table. The "Controller" class instantiated arrays of "Models" from the results of SQL queries. The "Model" class had methods to CRUD database entries, but the "Controller" class called those methods. The "View" class built a table and displayed the Model objects the Controller class passed to it.

2) A different example is in the Wikipedia article on MVC. Here, the Model sits in between the View and the Controller. The Controller updates the Model, which in turn feeds data to the View. It's almost like the Controller is Input and the View is Output.

-I've built some PHP websites like this (I think). Data was built from custom classes instantiated in $_SESSION variables. One set of PHP files had the HTML needed to display the $_SESSION data to the user. They made AJAX calls to "Controller" PHP files that updated the $_SESSION variables but didn't produce HTML. In that respect, the Model ($_SESSION) sent data to the View (the PHP with HTML the user saw) and was updated by the Controller (the PHP that CRUD'd $_SESSION).

-------------------------------------

I probably made mistakes there. Please (constructively) correct me.

It seems like there's an ongoing flame war between devotees of one definition and the other. I've found dueling articles arguing back and forth about which element sits in the middle: Model or Controller. See this example from Coding Horror. The author uses HTML, CSS, and the browser as examples of Model, View and Controller. But commenters immediately take exception, and long argument about the "true" definition of MVC ensues.

What's the Ars opinion? In MVC, does the Controller pass data between the View and Model? Or does the Model take from Controller and give to View?
 

krimhorn

Ars Legatus Legionis
39,865
Rule the first: don't worry about the opinions of others as to which is best. Both have their place and both serve similar purposes: provide separation of concerns and prevent reliance on specific technologies to achieve your goals. Even then you're going to find that depending on what you want to do/how much effort you want to put into it you're not going to be able to have every application you design operate 100% in either paradigm.

The specific technologies you're using will also drive which version you end up using. Something like AngularJS has a Controller/Model-View style presentation, but then you realize that the directive concept of Angular has the View telling the Controller what it needs. So while the Angular Controller owns the Model and is responsible for managing the two-way data-binding the View is responsible for telling the controller what Model elements it needs the values of in order to determine how it's going to work.

Similarly ASP.NET MVC (when using the Razor View engine at least) has that kind of connection between the View and the Model. By strongly typing the View you have some guarantee that the data you're going to receive on it is data that you can use. You still have separation of concerns and testability even while tying your Model and View together.

End of the day? Neither are right. Both are right. Depending on your technology stack and what you need to do.
 

QuadDamaged

Ars Praefectus
3,983
Subscriptor++
I refer to this as the MWTF pattern.

There are so many dialects (MVC, MCV, MVVM, MVP, and so on) that depend on implementation details of the view framework, the core should really be : can you work with a Model that isn't dependent on your view? If so, good, if not, refactor.

If you like MVVM, check-out DurandalJS, you might like it. The author did some really good stuff in WPF and ported it to JS.
 

GohanIYIan

Ars Legatus Legionis
10,195
[url=http:/civis/viewtopic.php?p=26507735#p26507735:19ctax00 said:
Bagheera[/url]":19ctax00]
1) An example of the MVC I learned is here in Apple's developer documentation. This should logically be called "Model-Controller-View," because the View and Model never touch. The Controller passes input and output between them.

-My first MVC app was written like that: The "Model" class had properties that matched columns in a database table. The "Controller" class instantiated arrays of "Models" from the results of SQL queries. The "Model" class had methods to CRUD database entries, but the "Controller" class called those methods. The "View" class built a table and displayed the Model objects the Controller class passed to it.

I'd contend that the View and Model do touch in the example. The Controller instantiates Model objects and passes them to the View, at which point the View is interacting with Model objects.

IMO the Controller tends to be the most loosely defined part. The Model represents the data, the View displays the current data/state, and the Controller does everything else. The first two pieces are very similar regardless of what kind of application it is, but the "everything else" part is much more variable.
 

krimhorn

Ars Legatus Legionis
39,865
[url=http:/civis/viewtopic.php?p=26510811#p26510811:3k46fpnn said:
dragonlord[/url]":3k46fpnn]From what I've seen of it angular.js lends itself nicely to M-V-VM rather than MVC as you can use strong databinding to separate the view from the viewmodel (controller)
It definitely is closer to MVVM than MVC which was a (probably not well expressed) part of my point. You're not going to see a "pure" representation of many of these fundamental design patterns in actual practice.

At my company we use ASP.NET MVC 4 for routing, server side processing and general application layout and either AngularJS or Knockout on the client (depending on individual application design/needs) to keep data work close to the user (and allow for quick and responsive changes to the view state based on input). Once the View has been selected and served up the server-side controller's primary responsibility is being the interface between the back-end data-store and the View. So what we've (kind of) got is an MV(VM)C pattern in use.
 

krimhorn

Ars Legatus Legionis
39,865
[url=http:/civis/viewtopic.php?p=26511381#p26511381:2mq2gxnx said:
GohanIYIan[/url]":2mq2gxnx]
I'd contend that the View and Model do touch in the example. The Controller instantiates Model objects and passes them to the View, at which point the View is interacting with Model objects.
The Apple doc is missing one word that would allow them to say that the Model and View are, in fact, decoupled: interface. It doesn't matter what object the model actually is (which is the point of the pattern) if the Controller and View communicate using a well-defined interface. The Controller then transforms whatever model object it's using to conform to the Controller/View interface and you can swap out Model objects at will.
 

GohanIYIan

Ars Legatus Legionis
10,195
[url=http:/civis/viewtopic.php?p=26512229#p26512229:1gjagy8j said:
krimhorn[/url]":1gjagy8j]
[url=http:/civis/viewtopic.php?p=26511381#p26511381:1gjagy8j said:
GohanIYIan[/url]":1gjagy8j]
I'd contend that the View and Model do touch in the example. The Controller instantiates Model objects and passes them to the View, at which point the View is interacting with Model objects.
The Apple doc is missing one word that would allow them to say that the Model and View are, in fact, decoupled: interface. It doesn't matter what object the model actually is (which is the point of the pattern) if the Controller and View communicate using a well-defined interface. The Controller then transforms whatever model object it's using to conform to the Controller/View interface and you can swap out Model objects at will.

All three should be decoupled, ideally, if they provide well defined class interfaces. It doesn't matter how the model is implemented internally, but it can matter in practice what it is. If you pass a set of User model objects and a set of Group model objects to a View, it shouldn't care where that data is coming from or how it's stored, but it most likely does care which objects are Users and which are Groups.
 

CanSpice

Ars Tribunus Angusticlavius
7,147
[url=http:/civis/viewtopic.php?p=26510811#p26510811:ljvtn2a1 said:
dragonlord[/url]":ljvtn2a1]From what I've seen of it angular.js lends itself nicely to M-V-VM rather than MVC as you can use strong databinding to separate the view from the viewmodel (controller)

AngularJS prefers to call itself MVW -- Model-View-Whatever.
 

Bagheera

Ars Tribunus Angusticlavius
8,773
Subscriptor
Thanks for the feedback. I've learned a lot about the (many) interpretations of MVC.

Encapsulate all your classes by making as much private as possible. On the public methods, change the code as needed, but don't change the arguments they accept or their return types (overload as needed, but don't remove old signatures). That will keep your interfaces consistent and your classes loosely coupled.

My professors taught me the above is the most important rule in programming. It sounds like MVC is simply a variation on the above. Is that correct?
 

krimhorn

Ars Legatus Legionis
39,865
I'd disagree that your classes should be mostly private. You lose out on being able to test the individual components (which is fine in some cases, a problem in others) and a plethora of private methods is one sign that your class is probably trying to do too many things.

If you're not working in a language that has explicit, enforced interfaces then that is one technique to keep your classes "interfaced". A better method is documentation and minimizing what your classes are trying to do. In most cases you can refactor a class that has many private methods into multiple classes that are mostly public. Since what you'll often find is that those private methods are doing something that you may need to do elsewhere. Better have them public, separate and testable than private, combined and repeated elsewhere.

On the public methods, change the code as needed, but don't change the arguments they accept or their return types...
Never do this without first having unit tests against those methods that provide your baseline for correct operation. Just because you didn't change the signature doesn't mean you didn't change something inside the method that breaks relying methods.
 

Richard Berg

Ars Legatus Legionis
43,037
Subscriptor
I'd disagree that your classes should be mostly private. You lose out on being able to test the individual components (which is fine in some cases, a problem in others) and a plethora of private methods is one sign that your class is probably trying to do too many things.
I disagree with pretty much every part of this paragraph.
 

dlp

Ars Tribunus Militum
2,220
[url=http:/civis/viewtopic.php?p=26515087#p26515087:3ck7ofph said:
GohanIYIan[/url]":3ck7ofph]
[url=http:/civis/viewtopic.php?p=26512229#p26512229:3ck7ofph said:
krimhorn[/url]":3ck7ofph]
[url=http:/civis/viewtopic.php?p=26511381#p26511381:3ck7ofph said:
GohanIYIan[/url]":3ck7ofph]
I'd contend that the View and Model do touch in the example. The Controller instantiates Model objects and passes them to the View, at which point the View is interacting with Model objects.
The Apple doc is missing one word that would allow them to say that the Model and View are, in fact, decoupled: interface. It doesn't matter what object the model actually is (which is the point of the pattern) if the Controller and View communicate using a well-defined interface. The Controller then transforms whatever model object it's using to conform to the Controller/View interface and you can swap out Model objects at will.

All three should be decoupled, ideally, if they provide well defined class interfaces. It doesn't matter how the model is implemented internally, but it can matter in practice what it is. If you pass a set of User model objects and a set of Group model objects to a View, it shouldn't care where that data is coming from or how it's stored, but it most likely does care which objects are Users and which are Groups.
I've been playing with MVC frameworks but never deep dived into them. What's a good example of having them properly decoupled? I can't wrap my head around how you could have a view that displays things from the model but doesn't have some knowledge about each specific model to make it work. However in that case, if you change the model, your view may break which to me implies coupling. Am I overthinking it?
 

GohanIYIan

Ars Legatus Legionis
10,195
[url=http:/civis/viewtopic.php?p=26555911#p26555911:2m3jhjx7 said:
dlp[/url]":2m3jhjx7]
[url=http:/civis/viewtopic.php?p=26515087#p26515087:2m3jhjx7 said:
GohanIYIan[/url]":2m3jhjx7]
[url=http:/civis/viewtopic.php?p=26512229#p26512229:2m3jhjx7 said:
krimhorn[/url]":2m3jhjx7]
[url=http:/civis/viewtopic.php?p=26511381#p26511381:2m3jhjx7 said:
GohanIYIan[/url]":2m3jhjx7]
I'd contend that the View and Model do touch in the example. The Controller instantiates Model objects and passes them to the View, at which point the View is interacting with Model objects.
The Apple doc is missing one word that would allow them to say that the Model and View are, in fact, decoupled: interface. It doesn't matter what object the model actually is (which is the point of the pattern) if the Controller and View communicate using a well-defined interface. The Controller then transforms whatever model object it's using to conform to the Controller/View interface and you can swap out Model objects at will.

All three should be decoupled, ideally, if they provide well defined class interfaces. It doesn't matter how the model is implemented internally, but it can matter in practice what it is. If you pass a set of User model objects and a set of Group model objects to a View, it shouldn't care where that data is coming from or how it's stored, but it most likely does care which objects are Users and which are Groups.
I've been playing with MVC frameworks but never deep dived into them. What's a good example of having them properly decoupled? I can't wrap my head around how you could have a view that displays things from the model but doesn't have some knowledge about each specific model to make it work. However in that case, if you change the model, your view may break which to me implies coupling. Am I overthinking it?

It's the "interface" part krimhorn was talking about. I would think of coupled/decoupled as a matter of degree instead of a clear dividing line. Any code that interacts has some degree of coupling - i.e., if you change a public method then any code that class the method may break. But if the classes expose a consistent public interface, then the internal details of each class can change without impacting the others. If you ported all your data from MySQL to Postgres, the View and Controller classes shouldn't care as long as the Model's public interface hasn't changed.
 

Paul Hill

Ars Legatus Legionis
19,890
However in that case, if you change the model, your view may break which to me implies coupling. Am I overthinking it?

I've always thought of it as rings where the inner ring doesn't care about the outer ring - so in MVVM the ViewModel doesn't rely on the View and the Model doesn't rely on the ViewModel.

I don't think it's possible or desirable to isolate the other way - I mean the ViewModel has to know about the Model and the View has to know about the ViewModel right?

If you:
a) have some way of testing the ViewModel outside the model (so you can fake interactions)
b) have some way of injecting fake model data (so you can fake data)

You're doing great, this is the fundamental reason why you'd do this.

Also try to have as little view coded as possible, something like KnockoutJS or Angular or WPF or any decent templating system would be great.

Hope this helps!
 
I interpret MVC as a model of user interaction, whether it's a text editor, spreadsheet, photo editing program, or what have you.

Mouse and keyboard input gets parsed into commands and directs the controller to update the internal data model.

The change in model state causes a view update.

The updated view gives feedback to the user, who inputs subsequent commands, and the cycle continues.

user => controller => model => view => user => controller...

The arrow represents causality, or functional dependency. The sequence of commands is a function of keyboard and mouse input. Model state is a function of the sequence of commands generated by the controller. The view is a function of the model, just like a scatter plot is a function of some collection of (x, y) pairs (and style options).

The view may be polling the model for updates, or maybe subscribe to various events, or use some other interaction scheme, but it doesn't change the functional dependency above, and I consider that the essential concept.
 
Status
Not open for further replies.