Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

comment on

( #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

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

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

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

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (3)
    As of 2021-04-10 12:33 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found