Greetings, monks. I ask your assistance.

I've been looking into updating some fairly old modules I have written, and, given some time to do so, look at redesigning them. And deciding to learn a better way to do things in general, decided to approach a easily solved problem, wondering what the better way is.

(Oh, I'm going to do a lot of things wrong. I'm pretty new to Moose, for one, and for another, this is design-related, so I'm taking shortcuts to demonstrate my thought processes.)

I have an application. It has a database connection.
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;
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::DB::User; use Moose; has dbh => (is => 'ro', required => 1); sub runQuery { return $shift->dbh->selectrow_array("sp_msforeachtable 'SELECT * F +ROM \?'"); }
#someplace in MyApp sub doSomethingWithUser { my $self = shift; my $user = MyApp::DB::User->new({dbh => $self->DBConnection->dbh}) +; 1; }
MyApp::User(s) are created at runtime. So, there's several things I can do:
* pass the connection around when I create the dependent objects.
      That's what I'm doing above. It works. It gets uglier when MyApp::DB::User needs to create MyApp::DB::User::SomethingElse, which also needs the database handle.
* create a DBConnection singleton.
     Again, it works. I could easily make the handle in MyApp::DBConnection a method that returns the same $dbh, for example, and call MyApp::DBConnection->dbh when I need it. I see a lot of .NET code that does this.
* ...Something else?
I was looking into dependency injection (with Bread::Board), but that doesn't seem to be right when I'm creating the objects at runtime.

Can anyone shed any light into modern (or post-modern) ways of handling this problem?

In reply to passing objects to dependancies by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.