in reply to passing objects to dependancies
I'm not sure that I'm understanding you. You need your database connection, but don't want to pass is as a parameter all over the place. That is usually done with a factory object. That factory object is a singleton (aka - global) and you ask it for a DB connection. It returns the same MyApp::DBConnection object to everyone. Your code snippet becomes
my $self = shift; my $connection = MyApp::ConnectionFactory->connection(); my $user = MyApp::DB::User->new({dbh => $connection->dbh()});
Is this even close to what you want?
Also, that { dbh => $value } thing is ugly. Use BUILDARGS so that the constructor (aka 'new') takes a single arg.
I think that is much easier on the eyes. And instead of creating all the connections at startup time, I'd make 'em lazy and only have them done when necessary.sub BUILDARGS { my ($class, $dbh) = @_; { dbh => $dbh }; }
Good luck.
- doug
|
|---|