[Catalyst] More new shortcuts

Sebastian Riedel sri at oook.de
Sun Nov 6 21:49:34 CET 2005


Am 06.11.2005 um 21:18 schrieb Jules Bean:

> Sebastian Riedel wrote:
>> It works for all components that inherit from a  
>> Catalyst::Component subclass, which are Catalyst::Base and the new  
>> Catalyst::Controller, Catalyst::Model, Catalyst::View.
>
> Some people recommend not to inherit from any of these for your  
> model class, so that the model can be used outside Catalyst in  
> batch scripts etc.

What makes a Catalyst::Model subclass unusable outside of Catalyst?

>
>>> I think there's a real potential source of confusion here. For  
>>> example, a classic catalyst view or controller is typically a  
>>> singleton object, and one can get away with confusing class and  
>>> unique instance. However, a CDBI or DBIC model doesn't work like  
>>> this: an instance represents a tuple (row if you prefer) in the  
>>> database. So what 'instance' would C->model() return?
>>
>> $c->components stores a instance of each loaded component (see  
>> Loaded Components table on startup).
>> Even DBIC/CDBI models have such a instance, it's really not  
>> complicated.
>
> I've read the code, I understand what happens. It may not be  
> complicated but I find it confusing. What is the purpose of these  
> instances? (I'm not trying to be difficult, I am genuinely trying  
> to understand) What is the advantage of $unique_instance->foo()  
> over Class::Name->foo()?

Do you really know what happens?

A basic Catalyst component could look like this.

     package MyApp::Model::Foo;
     use base 'Catalyst::Model';
     __PACKAGE__->config( { dbh => DBI->connect(...) } );
     1;

What really happens is that the config hash gets blessed into the  
class, to become our persistent component instance.
So this would work too.

     package MyApp::Controller::Bar;
     use base 'Catalyst::Controller';

     __PACKAGE__->config( { message => 'welcome to catalyst' } );

     sub foo : Local {
         my ( $self, $c ) = @_;
         $c->res->output( $self->{message} );
     }

     1;

More advanced components like DBIC overload the new() method  
inherited from Catalyst::Component to do more complicated init stuff.
Class data based models like DBIC/CDBI are really just the minority,  
Xapian, Plucene, SPOPS and Tangram are all instance based!


--
sebastian




More information about the Catalyst mailing list