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

Of Mason, OO and DBHs

by jimbus (Friar)
on Oct 17, 2006 at 16:07 UTC ( #578826=perlquestion: print w/replies, xml ) Need Help??
jimbus has asked for the wisdom of the Perl Monks concerning the following question:


I'm using apche2/mod_perl/mysql/mason to build web apps and have been follow the patterns, or some bastardization of the stuff found in the mason book. I've also been trying to follow a sort of MVC/OO format with my pages. I'm running into a mental snag as to what to do with the DBHs...

The mason book has you set up the dbh in the autohandler and then pass it as a parameter to the sub that will use it. But to my mind, this is definitely not a separation of display and business logic. My thought is I should be able to replace the code/db/whatever behind the module call without changing the stuff on the web side... but his way you have to change the web side to alter the DB.

Am I missing something, over thinking it...?

--Jimbus aka Jim Babcock
Wireless Data Engineer and Geek Wannabe

Replies are listed 'Best First'.
Re: Of Mason, OO and DBHs
by perrin (Chancellor) on Oct 17, 2006 at 18:20 UTC
    Although many Mason developers make database calls from the templates, there is no reason you have to do it that way. You can have database components that follow an MVC-style separation of concerns and just use them from your templates (or maybe from your autohandler, or from MasonX::WebApp.
Re: Of Mason, OO and DBHs
by EvanCarroll (Chaplain) on Oct 17, 2006 at 19:11 UTC

    There is nothign wrong with having the database handle in a global whatsoever. This is my suggestion. In fact in the Mason book they even weigh the alternative, which is even worse. Personally, I maintain many mason sites, and I have at least two globals in every app, one for the dbh - $dbh - and one for the session - $s.

    If you follow this design your root /autohandler should have this

    <%shared> ## Create database handle $dbh = DBI->connect( q{dbi:Pg:dbname=contacts}, q{ecarroll}, "", {AutoCommit=>0, RaiseError=>1, PrintError=>1} ) || die "Could not Connect to DB".$dbi::errstr; $dbh->commit; </%shared> <%cleanup> undef $S; $dbh->commit; </%cleanup>
    Than in your /auth/autohandler or whatever, you should have something like
    <%init> ## Verfiy we have a cookie with a _session_id my $j = Apache2::Cookie::Jar->new($r); my $c = $j->cookies('Dealermade'); unless ( defined $c ) { Dealermade::Error::nice_error($m, 'E201'); } ## Verify we have session that matches cookies ID my %APACHE_SESSION; eval { tie %APACHE_SESSION, 'Apache::Session::Postgres', $c-> +value, { Handle => $dbh, Commit => 1, }; }; if ( $@ ) { ## No tuple with matching ID (form cookie), bogus data +. $dbh->rollback; Dealermade::Error::nice_error($m, 'E301'); } $S = \%APACHE_SESSION; ## Verify that the sessioned user still has an entry in the us +ers table ## Save user information into $U by ref $U = $dbh->selectrow_hashref( q{ SELECT * FROM users WHERE pkid = ? }, {}, $APACHE_S +ESSION{'pkid'} ); if ( defined $U ) { delete $U->{'password'}; } else { $dbh->rollback; Dealermade::Error::nice_error($m, 'E501'); } </%init>
    And finally this in your Apache2 conf
    PerlSetVar MasonAllowGlobals $dbh PerlAddVar MasonAllowGlobals $U PerlAddVar MasonAllowGlobals $S PerlModule Apache::DBI PerlRequire /etc/apache2/ PerlModule Dealermade::Error PerlModule CGI PerlModule Dealermade::Event PerlModule Apache2::Request PerlModule Apache2::Cookie PerlModule Data::HTMLDumper PerlModule DBIx::Abstract PerlModule Apache::Session::Postgres PerlModule Digest::SHA1 PerlModule Template PerlModule DBI PerlModule DBD::Pg <LocationMatch "(\.html|\.mas)$"> SetHandler perl-script PerlHandler HTML::Mason::ApacheHandler </LocationMatch> <Location "/auth_required/edit/"> SetHandler perl-script PerlHandler HTML::Mason::ApacheHandler </Location> <Location "/auth_required/add/"> SetHandler perl-script PerlHandler HTML::Mason::ApacheHandler </Location>

    Evan Carroll

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://578826]
Approved by Corion
LanX frowning!
[Corion]: Hi choroba, LanX!
[Corion]: LanX: Turn that frown upside down! ;)
[Eily]: hello people!
[choroba]: a bit early for harvest mite, isn't it?

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (8)
As of 2018-05-23 07:39 GMT
Find Nodes?
    Voting Booth?