[Catalyst] Creating a thin Model

John Napiorkowski jjn1056 at yahoo.com
Fri May 18 18:59:30 GMT 2007


--- "Christopher H. Laco" <claco at chrislaco.com> wrote:

> Christopher H. Laco wrote:
> > Jamie Neil wrote:
> >> Can anyone tell me the best way of wrapping a non
> catalyst perl module
> >> in a Catalyst Model?
> >>
> >> I'm trying to setup the following stack:
> >>
> >> MyApp::Controller::Widget
> >> MyApp::Model::Widget
> >> MyApp::Logic::Widget
> >> MyApp::Schema::DB
> >>
> >> where MyApp::Model::Widget is based on
> Catalyst::Model, contains the
> >> configuration information for
> MyApp::Logic::Widget, and makes the
> >> methods in MyApp::Logic::Widget available in
> MyApp::Controller::Widget
> >> as $c->Model('Widget')->mymethod(...);
> >>
> >> MyApp::Schema::DB is a DBIC Schema which I want
> MyApp::Logic::Widget to
> >> use for storage, but not just passed through to
> the Model in the same
> >> way that Catalyst::Model::DBIC::Schema does.
> >>
> >> There is obviously more than one way to do this
> from looking at all the
> >> other models on CPAN, but is there some kind of
> best practice? Should I
> >> use AUTOLOAD, ACCEPT_CONTEXT, COMPONENT, override
> new or something else?
> >>
> >> I did see a very promising thread on this issue a
> few months ago, but
> >> this question
> >>
>
(http://lists.scsys.co.uk/pipermail/dbix-class/2007-January/003289.html)
> >> was never really answered. I've been going round
> and round in circles on
> >> this for a couple of weeks now, so some pointers
> (or even better example
> >> code) would be most welcome.
> >>
> > 
> > You're right. It depends on a few things.
> > Does the module you're wrapping have a new? Is it
> statefull? stateless?
> > Is it self creating, or is it a factory that
> creates other things.
> > 
> > Those are all factors.
> > 
> > Personally, I almost always do:
> > 
> > sub COMPONENT {
> >    my $self = NEXT::new
> > 
> >  diddle config...return $self
> > }
> > 
> 
> Sorry...that pseudo code was too vague:
> 
> 
> sub COMPONENT {
>   my $self = shift->NEW::new(@_);
>   $self->{'noncatclass'} = NonCatClass->new
>   return $self
> }
> 
> Then, delegate via autoload, or use real methods to
> forward request to
> the instance of the real class...
> 
> -=Chris

Any comments on why it's better to override
'COMPONENT' as opposed to 'new'?  I could never really
figure out the advantages or disadvantages for either.
 Of course if you are going to subclass a model that
might use 'ACCEPT_CONTEXT' then new is the only way to
go.

--john


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 



More information about the Catalyst mailing list