[Catalyst] The model -is- where your business logic lives.

Matt S Trout dbix-class at trout.me.uk
Thu Sep 27 21:43:44 GMT 2007


On Thu, Sep 27, 2007 at 11:51:26AM +0100, Ian Docherty wrote:
> In a previous thread, Matt S Trout said.
> 
> >The model -is- where your business logic lives.
> >
> >The real question is whether your ORM should be directly in the model or
> >not, but that's a whole different thread.
> 
> Based on this I have the following simple code.
> 
> ####
> package MyApp::Model::DBIC;
> 
> use strict;
> use base qw(Catalyst::Model::DBIC::Schema);
> 
> ####
> package MyApp::Schema::Demo;
> 
> use base qw(DBIx::Class);
> 
> __PACKAGE__->load_components(qw(PK::Auto Core));
> __PACKAGE__->table('demo');
> __PACKAGE__->add_columns(qw(id name state));
> __PACKAGE__->set_primary_key('id');
> 
> 
> ####
> package MyApp::Model::DBIC::Demo;

Put this code in MyApp::Schema::Demo.

I often call it e.g. MyApp::DataStore or MyApp::Domain to remind me that
it's the business layer and the DBIC-ness is merely incidental.
 
> sub do_some_business_logic_stuff {
>    my ($self) = @_;
> 
>    if (<some complicated business logic>) {
>        $self->state('pending');
>        $self->update;
>    }
> }   
> 
> 
> #### somewhere in my application
> 
> $demo = $c->model('DBIC::Demo')->find($index);
> $demo->do_some_business_logic_stuff;
> 
> -------------
> 
> Is this a standard/typical/best-practice way to do this sort of thing? 
> 
> It seems to me that if I want to use the business logic in an external 
> application (cronjob) then I am having to use the MyApp::Model::DBIC::Demo 
> namespace as decreed by Catalyst (not that this is a big issue).

Having moved the logic out of MyApp::Model:: this ceases to be an issue.

Don't confuse class -names- with the nature of classes. MyApp::Model:: is
*adapters* that make a model available to MyApp, not where your domain model
logic itself should live.

I usually these days have MyApp::Web for the catalyst app instead of MyApp so
I can deploy things like MyApp::DataStore from a separate dir tree.

-- 
      Matt S Trout       Need help with your Catalyst or DBIx::Class project?
   Technical Director                    http://www.shadowcat.co.uk/catalyst/
 Shadowcat Systems Ltd.  Want a managed development or deployment platform?
http://chainsawblues.vox.com/            http://www.shadowcat.co.uk/servers/



More information about the Catalyst mailing list