Fran Fabrizio wrote:
> Now, how do you represent in the model a
> complex query that joins across 5 of the nouns?

Others have already commented on this, but I want to point out that this 
is a general OO modelling question, not an MVC one, and there are many 
resources available to help you learn this stuff.  I'd suggest getting 
to your local computer book store and browsing through some OO 
programming titles.

> In your concert example, if I wanted to define a report that showed me 
> all of the seats that were purchased by people from New Jersey with a 
> Mastercard in the last week, how would I represent that?

That's a tricky one, because it doesn't make much sense to put all that 
logic about finding users' home states and payment types into a Concert 
class.  You could manipulate the objects to accomplish this:

my @wanted_seats;
my @seats = Model::Concert->findSeatsReservedAfter($date);
foreach my $seat (@seats) {
     if ($seat->person()->address()->state() = 'NJ') {
         push @wanted_seats, $seat;
     }
}

As you can see it gets messy fast, and I didn't even cover the 
Mastercard part.  It would probably have terruble performance too.  This 
is why people usually just write this kind of report as a big SQL query 
instead.  You can make a model object called ConcertSeatSearch:

@seats = Model::ConcertSeatSearch->findSeats(
                                              reserved_after => $date,
                                              payment_type   => $card,
                                              user_state     => $state,
                                             );

Just be careful that you don't end up making this into something that 
mirrors the SQL exactly.  There might be 4 tables involved in finding 
out what kind of credit card the user had, but that gets hidden behind 
this API.  If you find yourself writing classes that take options like 
"where => 'date > ' . $date" you are reinventing SQL, and you've lost 
your abstraction.

- Perrin

Reply via email to