[Catalyst] The model -is- where your business logic lives.
Anthony Gardner
cyclewood_ltd at yahoo.co.uk
Mon Oct 1 18:23:13 GMT 2007
I've gone about it this way, and although the business logic is not within Catalyst, somehow, what I've done doesn't feel right.
In myapp_server.pl I've had to add a few paths to @INC
use lib "/home/anthony/devel/lib";
use lib "/home/anthony/devel/lib/Test"
For my business layout I have ...
~/devel/lib/Test/TestSchema.pm
~/devel/lib/Test/TestSchema/UserProfile.pm
~/devel/lib/Test/Model/UserProfile.pm (see snippet two)
~/devel/lib/Test/Model/Base.pm (see snippet three)
Then I have for Catalyst
~/dev/websites/MyApp/lib/MyApp/Model/UserProfile.pm (see snippet one)
########### (snippet one)
package MyApp::Model::UserProfile;
use strict;
use base 'Catalyst::Model::DBIC::Schema';
use Test::Model::UserProfile;
__PACKAGE__->config(
schema_class => 'Test::TroveSchema',
connect_info => [
'dbi:SQLite:db/test.db',
'',
'',
],
);
sub new {
my $class = shift;
my $c = shift;
my $args = shift;
my $merged_args = Catalyst::Utils::merge_hashes( $args, $class->config() );
return Test::Model::UserProfile->new( $merged_args );
}
######################## (snippet two)
package Test::Model::UserProfile;
use strict;
use base 'Test::Model::Base';
<methods>
################## (snippet three)
package Test::Model::Base;
use base 'Class::Accessor';
use Carp;
use Test::TroveSchema;
__PACKAGE__->mk_accessors(qw|schema|);
sub new {
my $class = shift;
my $self = $class->next::method(@_);
croak "->connect_info must be defined" unless( $self->{connect_info} );
$self->{schema} = Test::TroveSchema->connect( @{ $self->{connect_info} } );
$self
}
sub find {
my $self = shift;
my $id = shift;
my $calling_pkg = (caller(0))[0];
$calling_pkg =~ s|(.*)::(.*)$|$2|;
return $self->{'schema'}->resultset($calling_pkg)->find($id);
}
Now, what seems wrong to me is that I've had to write new() methods and also, for example, a find() method which is in Test::Model::Base. This doesn't seem right to me.I would've thought I would have all of DBIC's utils available to me without having to recall them.
Can anyone offer pointers?
Many thanks
-Ants
Matt S Trout <dbix-class at trout.me.uk> wrote: 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 () {
> $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/
_______________________________________________
List: Catalyst at lists.rawmode.org
Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@lists.rawmode.org/
Dev site: http://dev.catalyst.perl.org/
Disclaimer: Technically, I'm always wrong!!
---------------------------------
Yahoo! Answers - Get better answers from someone who knows. Tryit now.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/catalyst/attachments/20071001/c3ee2b6d/attachment.htm
More information about the Catalyst
mailing list