While there are plenty of web articles on the theory of the model-view-controller design pattern, there is precious little advice available on how to implement it, in terms of structuring one's classes and objects.
So let me ask a simple question. Suppose I'm writing an application that looks up user information in a database, performs some calculations, and then outputs that information. Suppose also that for the sake of code encapsulation, I want to use object-orientation, and wrap up my code in a "user" object.
So, the practical question is this. Does it follow from the MVC design pattern that I ought to have both a Model::User object and a View::User object, and perhaps a Controller::User object, too? In short, won't I need a separate "User" object for the model, the view, and maybe the controller?
If that's so, then true, it means I can update any member of the MVC triad separately. But it also means that I must update each member separately, even if I want to make the same update to each. And mightn't that be more hassle than it's worth?
Perhaps I just lack a good idea of how to implement MVC. If anyone has any links to articles on the implementation (as opposed to merely the theory) of MVC I'd be really grateful for them.
GHFIn short, won't I need a separate "User" object for the model, the view, and maybe the controller?You might need 3 separate references to the object, but not 3 copies.
You may view the original node and the consideration vote tally.
You'd want your calculation class (the model), and UI class (the viewer). The controller receives user input and calls the model or the viewer as appropriate.
For instance: you could use a module like HTML::Template to provide your viewer functionality, your model would be custom written, and your controller might be implemented with CGI::Application.
You don't say if you're doing web based apps or console based apps?
If web based, I'm a big fan of CGI::Application - where it's the controller, your model is a package that defines your user object and the view is whatever template system you choose.
You really don't want to wrap the three parts of MVC into a single OO hierarchy but you could wrap the three individually into seperate OO hierarchies.
In practice, my controllers have always been simple derived classes of CGI::Application (I prefer to have logging embedded into the controller). The model is where I tend to get a bit crazy but it all depends on what your modelling. If you have only one user type than your model can be simply one class. The view has always been pretty simple too but then again I eschew most of the power of my template system (HTML::Embperl) and prefer a USES relationship for my template parts (headers, footers, sidebars, etc) to an ISA one.
If that's so ... it also means that I must update each member separately, even if I want to make the same update to each ... more hassle than it's worth?
Not necessarily, the first thing is, MVC is one of those buzzwords (just like OOP) where multiple definitions apply. Asking "how do I do MVC" is like asking "how do I do arithmetic?"
Since you are looking for practical "nuts and bolts" responses, the best approach would probably be to just understand the 'axioms' of MVC (which it seems you do) then just start coding or looking at code. Do a search for "Template::Toolkit", "Catalyst", "Smarty Templates", "Velocity", "Ruby on Rails", (etc) ... and *avoid* searching on just "MVC". The former will tend to give you 'nuts and bolts', the latter will tend to give you 'ivory tower'.
As far as your question about the "hassle", one way to avoid it is to simply define the properties of your User object in one place, and then use reflection in your View components to automatically spit out a view. This still preserves the vaunted MVC separation while minimizing the number of breaking changes for things like adding and deleting members (or fields).
You could use something like Class::DBI (or DBIx::Class) as your modeler. Then you would make a module inheriting from it that describes a table in your database. That's a model. It maps rows in the database to objects in memory.
Then you need a controller to take data from the user, validate it, calculate additional fields, etc. It interacts closely with the model, by creating new objects (rows) or updating/deleting existing ones.
Finally, you need a way for the user to see the process. That's the view. If you design well with a templating system, you could have a set of standard viewing templates that work for many different controllers even crossing apps. For example, in our system we have a single viewing template for virtually all of our add/edit forms. The controller populates it based on business rules (should the field be visible or calculated later, etc.) and on what is in the model (does the row exist, if so, show the user the data from it).
So, in summary, we use a model class for each table, a controller for each table (usually), and a set of stock viewing templates which are shared by many controllers (with most controllers using at least three templates: add/edit, delete confirmation, and listing of rows).
Phil
So, the practical question is this. Does it follow from the MVC design pattern that I ought to have both a Model::User object and a View::User object, and perhaps a Controller::User object, too? In short, won't I need a separate "User" object for the model, the view, and maybe the controller?
Yes and no. For your example, you'll probably want 3 objects:
update: Note that in this design, there is no User object outside the 3 XXX::User objects.
One thing to note is that although in typical CRUD applications, like those used to demonstrate ruby on rails, there are is a single model, a single controller and multiple views for each database table, this is not the best way to design all applications. The seperation between model, view and controller is a good one, but you might want to encapsulate more than one database table into a single model class and/or use views and controllers that span multiple model classes.
I suggest you try building a few small apllications using one or more of the available MVC frameworks to get a good feel for what works well. Most frameworks suggest a basic structure for your application on which you can build, and building everything from scratch will probably get you distracted by low-level details and it'll take a lot longer too.
Update2: you might want to check out my article, the MVC pattern in web applications - it specifically explains the roles that the M, V and C parts play and how they interact.
Maybe I don't understand MVC, but isn't the point to keep the M V and C separate and interchangeable? So that if you write this thing for a web page, and then later realize you'd like to write a console app, or you decide you want to use a different database, it's really easy? The User object doesn't know or care about HTML or text or whatever. There's view gunk that handles that.
So, maybe you'd have a User object, and a User::MySQL object and a User::HTML object.
And then later you write the User::Console object and the User::PostgreSQL objects.
Or am I thinking of some other acronym?
-Pileofrogs
Maybe I don't understand MVC, but isn't the point to keep the M V and C separate and interchangeable?Basically, yes.
The User object doesn't know or care about HTML or text or whatever. There's view gunk that handles that.I think you and possibly the OP are confused by the naming of the classes. What you call User object is probably what is called the Model::User class in the OP. The Model class (whatever name it has) should be independent of the programs UI. The modelSo, maybe you'd have a User object, and a User::MySQL object and a User::HTML object.
Database independence of the model (if you're using a database for persistence) is mostly done independent of the model classes directly. For instance, if you're using Class::DBI to implement your model classes, you're probably using a shared base class for all your model classes which determines the type of database (excepting maybe some handwritten non-portable SQL for specific model actions).
Okay, then if I get this right, I might create an object, called User or Method::User which contains all the internal data for a user as well as methods for normal actions on that data, like get_age() or whatever. It also contains any persistence (db) or other features.
That's the Model.
I'm still fuzzy on what makes the View and Control.
My best guess is that View is a bunch of formatting functions, and Control uses both the Method object and the View functions to give you web pages. Am I right?
For example, I want a list of everyone who has blue hair. The control might be bluehair.pl, a user callable CGI. It would instantiate a bunch of User (or Method::User) objects and use User->has_blue_hair() to pick the ones that had blue hair. Then maybe it would use CGI.pm to present the View.
Warmer? Still missing it?
Thanks
-Pileofrogs
My best guess is that View is a bunch of formatting functions, and Control uses both the Method object and the View functions to give you web pages. Am I right?You're quite right.
If I'm not mistaken, in the original MVC design, the controller is responsible for setting up the view and connecting it to the model, and after that the View and the Model work mostly by themselves. But since web based applications behave differently from typical GUI applications, what tends to happen is subtly different:
Browser View Controller Model
. . . .
. HTTP Request . . .
+---------------------------------->+ .
| . | update model .
| . +-------------->+
| . | |
| . | return status |
| . +<--------------+
| . select view | .
| +<----------------+ .
| | . .
| | query state . .
| +-------------------------------->+
| | . |
| | . return state |
| +<--------------------------------+
| HTTP Response | . .
+<----------------+ . .
. . . .
(see also [http://zeekat.nl/joost/mvc-web/index.html])
Basically, the controller interprets the (http) requests as model actions/queries and then passes (parts of) the model* to the appropriate view (which is selected by the controller and does nothing more than display part of the model).
* By model here I mean all of the objects and classes that make up the API of whatever is being manipulated by the program - i.e. a database, sensors, a tv-card, whatever.
For example, I want a list of everyone who has blue hair. The control might be bluehair.pl, a user callable CGI. It would instantiate a bunch of User (or Method::User) objects and use User->has_blue_hair() to pick the ones that had blue hair. Then maybe it would use CGI.pm to present the View.Yes. You've got the basic seperations right. But in most MVC frameworks in use today you would probably put most of the specifics of bluehair.pl (which might be VERY little, if you're using a good framework) in a subclass of the generic controller class and you'd use a templating system to generate the views.
Also, if you're using a relational database, it's a lot more efficient to use an SQL query (probably implemented as a Model::User class method, and likely generated by an OO-relational library) to select a list of blue haired people instead of iterating through all users in the database one by one.
updated: minor corrections.
my @users = User->search(hair_color => $hair_color);Then it would pass the @users data to a template for display.
The older I get, the less I can hold in mind at a time. Separation of concerns lowers the volume I must keep in the front of my mind at an instant of debugging. If I can reuse the code because of separation, that's a bonus.
Phil
#!/usr/bin/perl -w
use strict;
package Model;
my $data = { fruits=>'apples',vegies=>'carrots'};
sub get_food { $data->{$_[0]} }
package View;
my $template = "I like %s, especially %s!\n";
sub show_food { printf $template, @_ }
package main; # Controller;
for my $foodtype(qw(fruits vegies)) {
my $food = Model::get_food($foodtype);
View::show_food($foodtype,$food);
}
__END__
You can change how the Model stores and retrieves the data without impacting either of the others. You can change how the View gets and displays its templates without impacting the others. You could use a different controller, e.g. an interactive script that prompted for foodtype. ...
... and to extend [jZed]s excellent bare-bones example, here is a slightly modified version that renders correct output even if you modify the data module. The controller and view modules still work, obviating the anticipated "hassle" posed in the original question.
Note that the aforementioned 'hassle' really has more to do with 'loose coupling' rather than the specifics of MVC. Note also that OOP purists will not like the modification introduced here, but this is good enough to get the point across.
#!/usr/bin/perl -w
use strict;
package Model;
our $data = {
fruits=>'apples',veggies=>'carrots'
,meat=>'bacon',grains=>'pita bread'
};
sub get_food { $data->{$_[0]} }
package View;
my $template = "I like %s, especially %s!\n";
sub show_food { printf $template, @_ }
package main; # Controller;
for my $foodtype(sort keys %{$Model::data}) {
my $food = Model::get_food($foodtype);
View::show_food($foodtype,$food);
}
__END__
Another benefit of MVC not mentioned elsewhere in this thread, is that you can put your HTML designer to work on the view (and even edit it in Dreamweaver or whatever) without them having to know how to program. This also takes another kind 'loose coupling' to pull it off correctly, as well as the politics necessary to let each team member do what they do best.
package main; # Controller;
for my $foodtype(sort keys %{$Model::data}) {
my $food = Model::get_food($foodtype);
View::show_food($foodtype,$food);
}
You're right that my example that used qw(fruits vegies) gave the Controller knowledge of the Model. But so does yours. Yours breaks the MVC separation because the Controller now needs to know that the Model stores its data in a hash. If we add sub get_keys { sort keys %$data } to the Model, the Controller can then do this: for my $foodtype( Model::get_keys ) ... and thus be freed from knowing anything about how the Model stores things. If the Model changes to using a database, you could still have a Model::get_keys() method that would do a DISTINCT query and the Controller wouldn't need to change at all.
No sweat. I just followed up to address the issue to the OP (specifically, the OP's concern that inter-component changes in MVC must somehow be a hassle).
I must admit, however, I was a little surprised that you did not define Model to look something more like:
my $data = [
{type=>'fruit',food='apple'},
{type=>'veggies',food='carrot'},
{type=>'meat',food='bacon'},
];
or
my $data = [ [qw(fruit apple)], [qw(veggies carrot)], ];
This is the approach I tend to use most often (with obvious anticipation that "M" will be eventually migrated to a database). I just figured you were trying to save typing ... (which is why I didn't even bother to give a code example at all in my first response ;-).
One first question you should do to yourself is: How big is this system?
This question is important because this can make you choice from two different approuches...
1) If the system is small and you just need to show the contents of a database, you can stick to the MVC used by struts, or others... wich mean that the Model object is the database encapsulation.
2) OTOH, if the system is supposed to be a little bigger, and you're not just showing the contents of the table, but you have to deal with business rules you probably want to create a more sofisticated design...
In this design, you would split your system in two parts, one as a Service Provider that would provide the needed services to retrieve and save the information, so you can implement multiple interfaces, or interface with other systems (see Web Services Architecture in w3c website)...
The not-so-obvious implication is that the Model object will not necessarly represent the plain table, but could also include some calculated information, some object's relationships.
Now to the implementation:
The main key to MVC is a magic keyword named: "event". The key is... When the model is changed, it notifies the changes, so if the model is changed by the Controller (I mean, loading the data) the UI is updated, and if the UI changes the Model, the controller can be notified and do some extra calculation (I mean, sum the values and set the total value in the model and the UI will be notified).
The big difference is that when you want to fetch the data to send to your service, you will not traverse all the components getting the values, the model will be already in the format you need to pass to the service, thus, reducing the complexity of the user interface.
1) If the system is small and you just need to show the contents of a database, you can stick to the MVC used by struts, or others... wich mean that the Model object is the database encapsulation.True, most MVC-for-the-web frameworks assume most of the model is implemented using an object-relational system, but struts does not mandate that. In fact, struts (like most other MVC systems I've seen) doesn't even have a built-in oo-relational library, although it does make some assumptions about your model's API. In short, you are typically free to design your model classes in any way you want (but see below).
The main key to MVC is a magic keyword named: "event". The key is... When the model is changed, it notifies the changes, so if the model is changed by the Controller (I mean, loading the data) the UI is updated, and if the UI changes the Model, the controller can be notified and do some extra calculation (I mean, sum the values and set the total value in the model and the UI will be notified).Yes, and this is exactly where most web-MVC systems do things very differently compared to traditional GUI MVC systems, which tend to use Observer pattern or callbacks to link Model and View. First off, since the web mostly works with HTML pages that can only be updated via request/response actions (I'm ignoring flash), you can't really have a model update the view's output directly. Also, there are so many possible views that updating them all would be very inefficient.
What tends to happen is that for each request, after the model has been updated, the view just queries the relevant model objects and generates new HTML/XML/whatever output.
The exception to this is caching; cache-invalidation can sometimes handled by observing the model (however, in this case the observer responsible for clearing the cache is usually not the view or controller).
First off, since the web mostly works with HTML pages that can only be updated via request/response actions (I'm ignoring flash), you can't really have a model update the view's output directly. Also, there are so many possible views that updating them all would be very inefficient.
Unless you get serious about AJAX (many people bet on this). In theory, you can implement the entire MVC in JavaScript (client-side) and have your server acting just as a Web Services provider. In fact, XForms is just that, the only problem is that it still isn't supported by the web browsers..
Unless you get serious about AJAX (many people bet on this).While you can certainly craft stuff on top of AJAX that behaves like a callback system, it's still running on top of HTTP, i.e. request/response pairs originating from the browser.
That means you're still just polling for data from the client as soon as you need any information from the server side. You're not pushing data to the display as you do in traditional GUI applications, where the view also knows a lot about the state of the display. Actually, my guess is that the more AJAX technology you're using, the more you'd have to focus on plain output & http caching if you want to get any kind of performance (instead of generating one HTML page, you're generating lots of XML fragments).
That means you're still just polling for data from the client as soon as you need any information from the server side. You're not pushing data to the display as you do in traditional GUI applications.
Well... I was talking about getting even deeper into AJAX... I mean, the Model would be implemented as Javascript objects, the controller as javascript objects and the view as javascript code glued to the form input elements... This way you would have the same type of communication of traditional GUI applications... where the server doesn't push data also...
Yup. In fact there is work underway now on complete templating, database persistence and state management using only the browser and Javascript.
Here are some links relating to what ruoso said:
Javascript-only Templating and MVC framework.
Ajax Massive Storage System (supposedly simulate a database without RDBMS or web server).The point is that "offline AJAX" is a buzzword to be looking out for. Another buzzword "serverless AJAX" might become popular. In fact, with JSON, you can have "offline AJAX without XML" (which is ironic, since XMLHTTP is what started this whole bandwagon in the first place).
Well, but if we go to offline AJAX, then we're getting back to that small system that a simple WebMVC already solves... The question here *is* having a complex system that you *do* want to have a server, or yet, multiple servers..
perlmonks.org content © perlmonks.org and Anonymous Monk, astroboy, derby, dimar, ghferrari, Joost, jZed, NodeReaper, perrin, phaylon, philcrow, pileofrogs, ruoso
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03