Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
Good so far. Kinda. So long as MyApp is simple, this works. But then we add MyApp::DB::User. It needs the database handle to run some queries.package MyApp::DBConnection use Moose; has dbh => { is => 'ro' }; sub BUILD { #load the config from a config file, etc. #create the connection, set the handle. Etc, etc. } 1; package MyApp; use Moose; has dbConnection (is => 'ro', isa => 'MyApp::DBConnection', default => + sub { MyApp::DBConnection->new() }); 1;
package MyApp::DB::User; use Moose; has dbh => (is => 'ro', required => 1); sub runQuery { return $shift->dbh->selectrow_array("sp_msforeachtable 'SELECT * F +ROM \?'"); }
MyApp::User(s) are created at runtime. So, there's several things I can do:#someplace in MyApp sub doSomethingWithUser { my $self = shift; my $user = MyApp::DB::User->new({dbh => $self->DBConnection->dbh}) +; 1; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: passing objects to dependancies
by doug (Pilgrim) on Feb 11, 2010 at 21:33 UTC |