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"); }

In reply to question on OO design and object composition by Qiang

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.