by icebraining · 13y ago · 0 comments
No, that's just with the bastardized version of MVC popularized by Rails et all, which treat the views as little more than templates.
In the original MVC papers from Smalltalk, the views did actually send messages updating the model:
A view is attached to its model (or model part) and gets the data
necessary for the presentation from the model by asking questions.
It may also update the model by sending appropriate messages.
All these questions and messages have to be in the terminology
of the model, the view will therefore have to know the semantics
of the attributes of the model it represents.
http://heim.ifi.uio.no/~trygver/1979/mvc-2/1979-12-MVC.pdfAvdi Grimm has a great book about this called Objects on Rails[1] and Bryan Helmkamp from Code Climate has a good introductory blog post on it as well[2].
Rails implementation of MVC is perfectly fine. It's when people lean on MVC for everything without applying "classic" Object-Oriented design. That's when things become a mess.
1 - http://objectsonrails.com/
2 - http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decomp...
"MVC" as a meaningful term has been dead for at least 5 years.
The only times 'fat' and 'skinny' are mentioned in the title.
#app/models/user/query.rb
class User
module Scopes
#my scopes.
end
end"I don't understand MVC, this framework I'm using doesn't supply me with a one-size-fits-all architecture for my business logic and I'm incapable of designing my own. So I'll just stuff my spaghetti style code somewhere, and bitch about MVC and/or the framework being insufficient/dead/shit."
It's the ultimate expression of a bad craftsman blaming his tools, and it's endemic in web development.
Funny, because too me that seems like the whole point of MVC: To have a one-size-fits-all "architecture" to shoe-horn your business logic into.
But then again I never really understood the point of MVC. It seems like a somewhat nice fit for GUI applications, but that doesn't mean you should use it for everything. The real takeaway is to decouple and abstract to the point that makes sense. Not too much and not too little.
That said - the Controller is the only place that is left if the code isn't interface or datastorage specific, so naturally it's the first place to get bloated. But that's fine, you just have to take care of that when you can, by abstracting common functionality and/or move it out in a library.
Important word here: 'web'. If you move that word all ror puritanism falls apart. For example imagine multi-protocol MVC. In that sense javascript is really part of view and it can 'pass messages' to model using ajax.
While Rails did use this and make it popular, it's older than that: http://en.wikipedia.org/wiki/Model_2
I'm not sure if rails is the first offender here -- php (and asp, jsp etc) makes it so easy to drop a ton of complexity on your lower extremities that it becomes necessary to "ward off" the part that actually does templating from the rest of your code -- which, if it's all old school php -- isn't terribly easy to do -- or at least isn't trivial to make "obvious" in your code.
If you fail to maintain a part of your code as template-php, and mix it up with your logic, your nicely structured application quickly becomes a mess (this is what old php codebases I've looked at looks like -- and has left me a a little resentful against the entire language. Probably more than php deserves).
I write plenty of Rails apps that have thin controllers and fat models. Class methods / static methods on models can help encapsulate business logic in any language's MVC frameworks, as well as liberal use of the Tell Don't Ask pattern.
Trygve Reenskaug actually setup a web page to explain how MVC was originally conceived [1]. My main take-away was that models correspond with the user's mental model while using the application: "The essential purpose of MVC is to bridge the gap between the human user's mental model and the digital model that exists in the computer. The ideal MVC solution supports the user illusion of seeing and manipulating the domain information directly." The most important (IMO) implication of this is that your models aren't limited to data that exists in a database, which is a mistake that I see a lot of web developers make.
However, this is the first time I’ve seen any material from those early days that suggested having the view affect the model, as opposed to being a mere observer, passive or otherwise, and having controllers responsible for all changes of state.
It’s also the first time I’ve seen an early source suggest having one controller deal with multiple views, as opposed to possibly having multiple controller-view pairs associated with the same underlying data but with each view having its own associated controller. I’ve seen variations with views and controllers nested and using some form of delegation/bubbling if the presentation is complicated, but that’s not quite the same thing.
The only original papers I’d seen previously, even those that I thought included Reenskaug as an author, already had the characteristic triangle arrangement with one-way traffic from the model to the view and with a 1:1 view:controller relationship, so I was interested to know the origin of the paper that icebraining linked to. Your link answers that question, so thanks for that.
The idea of view directly modifying models comes from fact that in many cases there was class named FooView, that acted both as view and controller (which generally makes sense because the low level GUI logic is same for both). Also models represented not only the actual data but also various parts of GUI state itself (eg. position of scrollbar).
[1] https://en.wikipedia.org/wiki/Data,_Context,_and_Interaction
Rails and the other MVC frameworks don't just focus on the client-side, however. They are also deeply involved with the database and everything in between so a pure implementation of MVC is out of their reach. On the Web in the next 5-10 years maybe we will see more frameworks separating the concerns of the Web server/database and the client-side. Once we break those apart you can have a Smalltalk style MVC implementation on the client-side and organize your "back-end" code to your hearts desire or use a back-end specifc framework to guide you there.
There is little need to debate that point. Doing MVC-style architecture on the browser side works just fine. One of my long-term projects is a relatively large web application that uses something very close to the original MVC idea, with a central model that holds all the state and then a few dozen view-controller pairs that handle all the ways of interacting with it. This architecture has served me well for several years now, and the design has proven to be very easy to maintain and adapt to changing requirements. (In practice I add a fourth type of component in the web context, a “store” that is responsible for communicating with the server, and I define a small number of additional relationships to go with it because it doesn’t fit into any of the other three categories. Even then, the store acts somewhat like a view-controller pair for most purposes, and the cases where it doesn’t are mostly things I might do differently with the wisdom of hindsight.)
On the Web in the next 5-10 years maybe we will see more frameworks separating the concerns of the Web server/database and the client-side.
I’m not sure why we need any distinction between them. Ultimately, we’re just writing a distributed application, which happens to run using something like HTTP as a communications protocol in this case. All the usual concepts about maintaining state and actions that have side effects apply, and all the usual arguments about separating such concerns apply. What makes you think we can’t use these ideas already today?
They may as well have named it 123. At least that wouldn't make you guess at what the naming signifies.
For example, the Model of MVC consists of business logic and sometimes what people refer to as... models. The business logic controls how models are modified. The Controller doesn't. The Controller controls very little when the system is correctly architect-ed, especially when you have multiple UIs.
Really, the View is the only one without ambiguity. But even so, I still have to google from time to time to remember who is allowed to know about who and who notifies who of what...
A better name, and this is literally off the top of my head, would be "Business Layer, View and Event Translator".
Edit: Having thought about it more, I decided to change the word Presentation to View in my "better name" suggestion.
Model is a representation of the data of your domain and behavior imminently related to this data. I.e. what is "user" and what it means to "create a user"? What does it mean to "find most active users"? The name is because it "models" the domain by representing it as an abstract interface.
Controller is a representation of the rules of when and which models are invoked and which actions are performed. I.e. when we would "create a user"? When we would need to "find most active users"? The name is because it "controls" the models, telling them what to do.
View is how the information presented to the end consumer and how the consumer interacts with it. I.e. what it means to actually show "most active users" to the end consumer? The name is because end consumer "views" it.
Of course, this explanation without actual experience of how such systems are designed and behave does not make a newbie magically able to design complex systems flawlessly. No amount of name changing would. But explaining how this approach works doesn't seem that hard to me.
So I'll step out on a limb and say that it strongly influence(d/s) the design of Rails