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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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.


In reply to Re: Implementing Model-View-Controller by Joost
in thread Implementing Model-View-Controller by ghferrari

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-26 01:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found