Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Implementing Model-View-Controller

by Joost (Canon)
on Jan 04, 2006 at 14:53 UTC ( [id://520928]=note: print w/replies, xml ) Need Help??


in reply to Implementing Model-View-Controller

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:

  • Model::User - interface to the user info in the database - performs the SQL queries needed to get the right data and might perform additional calculations on that data. Can be implemented using a OO-Relational library, like Class::DBI or DBIx::Class (which can save you a lot of work, since those already implement most of the low-level SQL mappings).
  • View::User - displays a Model::User object as an HTML page. This is usally implemented using some kind of templating system, not as a "hand-written" class.
  • Controller::User - processes the HTTP requests for the user data, retrieves the right Model::User object(s) and performs any needed model changes, then passes the Model::User object(s) to the View.
In this design you do have 3 XXX::User objects, but they do not represent the same thing: the Model::User class represents and API to the (persistent) user data, Controller::User is responsible for translating requests into model actions and View::User translates models into HTML.

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://520928]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2024-03-28 23:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found