Qiang has asked for the wisdom of the Perl Monks concerning the following question:

hi monks,

i am trying to design an API to expose some commonly used methods from our internal modules. this api will be used in our webapps.

in the following example. i would like to provide a single access point and chain methods through the wrapper class. i.e $self->api->do_this and $self->api->do_that..

here is something i am uncertain about.. every web app will have a database handler and i would like to reuse it in the company::api. hence i am using it when initializing company::api and passing it down to every modules that need it. i feel clumsy about it.

being a design pattern newbie, i find it difficult to achieve a clean design. i would like to hear other monks opinion on this design..

btw, read some articles on design pattern and this example seems to match the object composition.. I better get my pattern design book real soon.. :)

package company::api; use company::logging; use company::user; sub new { my $class = shift; my $dbh = shift; my $uname = shift; my $self = {}; $self->{__dbh} = $dbh; $self->{__log} = company::logging->new($self->{__dbh}); $self->{__user} = company::user->new($self->{__dbh}, $uname ); bless $self, $class; } sub log_event { my ($self, $log) = @_; $self->{__log}->log_event($log); } sub uid { my $self = shift; unless ( $self->{__uid} ) { $self->{__uid} = $self->{__user}->get_uuid(); } return $self->{__uid}; } ##### usage from webapp package MyWebApp::Base; sub api { unless ($self->{__api}) { $self->{__api} = company::api->new( MyWebApp::DB->dbh, $ENV{RE +MOTE_USER} ); } return $self->{__api}; } package MyWebApp::ControllerOne use base 'MyApp::Base'; sub testing { my $uid = $self->api->uid; $self->api->log_event("$uid is testing"); }

Replies are listed 'Best First'.
Re: question on OO design and object composition
by samtregar (Abbot) on Mar 20, 2008 at 19:33 UTC
    No need to pass your DBI handles around like that, just get a new one using connect_cached() and you'll automatically share a single handle.

    -sam

      i am aware of connect_cached. however, we use different user/pwd for every webapps so the $dbh i pass to company::api is that special dbh for a certain webapp. there is no way to get the dbh except passing it from the webapp.

      it would be much simpler if we are using a sigle dbh handler for every apps so i could do company::db->dbh to get the same dbh all the time. but that is not the case for me..

        Huh? So you want to share around a DBI handle, but you can't use connect_cached because each object has its own user/pass. If that's the case, how can you share a DBI handle in the first place?

        -sam

Re: question on OO design and object composition
by Herkum (Parson) on Mar 21, 2008 at 05:13 UTC

    What I did was all my Objects that talk to the database, inherit from one module. That module has an 'import' method( that gets called automatically), where it will attempt to create a database handle if one does not already exists. Then I share the database handle with a dbh() method from the base class.

    So this allows only my objects to have just one dbh(). without having to pass a $dbh around.