[Catalyst] Re: Sharing a database handle between components

Jonathan Rockway jon at jrock.us
Sun Feb 10 11:41:28 GMT 2008


* On Sun, Feb 10 2008, Matt S Trout wrote:
> On Sun, Feb 10, 2008 at 09:52:21AM +0100, Aristotle Pagaltzis wrote:
>> * Matt S Trout <dbix-class at trout.me.uk> [2008-02-10 08:50]:
>> > To reduce coupling I'd probably do (in MyAPP)
>> > 
>> > __PACKAGE__->config(
>> >   'View::TT' => { template_db_model => 'Foo' }
>> > );
>> > 
>> > and then have the view use thatas the arg to $c->model during new.
>> 
>> That still couples the view to a specific type of model though:
>> how to get the DBH out of a DBIC::Schema model is different from
>> how to do it with a DBI model, and with the approach you propose,
>> that logic lives in the view class.
>
> I'd probably have a mapping for how to do that.
>
> Putting it in the app class is no less wrong than putting it in the view
> class, really - "how do I get a dbh out of $thing" is a general concern so
> some sort of adaptor/driver approach is probably best if you're trying
> to keep it separated out.

The key thing to take away is that parts of your app shouldn't store
away pieces of the environment (like dbh).  They should ask the app for
it each time they need it.  Right now we usually use the DBIC model for
getting a dbh (since it does a good job managing it), but in the ideal
world the app would manage dbh for you and the parts that need it would
just ask.

At some point this pattern will be made easier (see Bread::Board on
CPAN), and perhaps we can write a dbh manager and coerce DBIC to use
it.  That would be nice-n-clean.  (Oh, and we can call it "aspect
oriented programming" and convert all the java folks to perl.)

In the mean time $c->model('DBIC')->... is an acceptable substitute.

Regards,
Jonathan Rockway



More information about the Catalyst mailing list