[Catalyst] Accessing DB from external model

Mike Raynham catalyst at mikeraynham.co.uk
Fri Nov 5 09:37:35 GMT 2010


I am fairly new to all things Perl and Catalyst, and would like to know 
if what I am attempting to do is correct.

I've recently finished reading 'The Definitive Guide to Catalyst', which 
has been very helpful, but I am still a little confused about separating 
my business logic from my Catalyst application, and accessing the 
database connection from my non-Catalyst application.

At the moment, I have my external class in the lib/MyApp directory.  It 
uses the connection info found in MyApp::Model::DB, like so:


# lib/MyApp/MyExternalClass.pm

use MyApp::Model::DB;
use MyApp::Schema;

my $connect_info = MyApp::Model::DB->config->{connect_info};
my $schema = MyApp::Schema->connect( $connect_info );

# Do stuff with $schema...


That works, and I can connect to the database.  However, I have searched 
this mailing list, and found this:


Here, the connection information is moved from MyApp::Model::DB to 
MyApp::DB, and then Catalyst::Model::Adaptor is used to glue 
MyApp::Model::DB to MyApp::DB.  Is it a good idea?  MyApp::Model::DB 
appears to be part of the Catalyst application, so if I want to access 
the database from an external model, it makes sense to me to move the 
connection code outside of the Catalyst application.



More information about the Catalyst mailing list