[Catalyst] Creating a thin Model

Matt S Trout dbix-class at trout.me.uk
Mon May 21 12:06:05 GMT 2007


On Mon, May 21, 2007 at 11:06:44AM +0100, Jamie Neil wrote:
> Christopher H. Laco wrote:
> >>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...
> 
> I assume you mean:
> 
> my $self = shift->NEXT::new(@_);
> 
> in which case it fails with:

That should be in 'sub new', and next::method would be better.

Or you could just return the intance of your external class - there's no
requirement for the return of MyApp::Model::Foo->new to be a MyApp::Model::Foo
object and $c->model('Foo') will happily return your OtherClass::Blah object.

> I noticed that a number of newer models on CPAN were using this 
> construction:
> 
> use base qw/ Catalyst::Model /;
> 
> sub new {
>     my $self  = shift->next::method(@_);
>     my $class = ref($self);
>     my ( $c, $args ) = @_;
>     $self->{'.mymodel'} = ExternalModule->new(
>         Catalyst::Utils::merge_hashes( $args, $self->config )
>     );
>     return $self;
> }
> 
> sub ACCEPT_CONTEXT {
>     return shift->{'.mymodel'};
> }
> 
> or is this only recommended if you need to make the context available in 
> the external module?

No, that's just plain silly. Could I have a list of the models that do that
so I can track down the authors and slap them, please?

sub new {
  my ($class, $c, $args) = @_;
  return ExternalModule->new(
    Catalyst::Utils::merge_hashes( $args, $class->config )
  );
}

would be quite sufficient.

-- 
      Matt S Trout       Need help with your Catalyst or DBIx::Class project?
   Technical Director    Want a managed development or deployment platform?
 Shadowcat Systems Ltd.  Contact mst (at) shadowcatsystems.co.uk for a quote
http://chainsawblues.vox.com/             http://www.shadowcatsystems.co.uk/ 



More information about the Catalyst mailing list