[Catalyst] Catalyst MVC Best Pratices
Anthony Gardner
cyclewood_ltd at yahoo.co.uk
Thu May 3 18:22:12 GMT 2007
Greetings,
I've had a look at Catalyst and RoR from a basic point of view and like both. I'm also familiar with the concepts of MVC but again, from a basic point of view. I'm also busy learning design patterns etc.
So, my head is full and maybe I can't see the wood for the trees in what I'm about to ask.
I'm 100% happy with what should go in the View part of MVC, 90% sure of what should go in the Controller and more than confused about what should go in the Model (not exactly true)
I searched the Catalyst mail archives about 'best practice' and came across this (amongst others) and read it in full
http://www.gossamer-threads.com/lists/catalyst/users/1081?search_string=best%20practice;#1081
......... leaving me not confused but with lots of questions.
I've already started my new application and I've set it up like this ...
lib/P4C/Controller/SelectProduct.pm
/Model/Product.pm
/P4CSchema/Product.pm (amongst others)
/P4CSchema.pm
/P4C.pm
Now, when selectproduct action is called, I want to select all products that match a criteria. In the Catalyst examples, this would be done simply as ........
$c->stash()->{products} = [$c->model('P4CSchema::Product)->all()];
and all would be happy in the world.
But, I might want to do some work on those results before stashing them away so, I may decide to do this in the Controller .......
my $prod = Model::Product->new();
$c->stash()->{products} = [ $prod->get_all_products_that_like_me() ];
and in Model::Product I'd have something like this (maybe)...
sub get_all_products_that_like_me() {
my $self = shift;
## what's the use of schema in this command when it works okay without?
##$self->schema()->resultset('P4CSchema::ProductGroup')->all();
my $prods = $self->resultset('P4CSchema::ProductGroup')->all();
return $self->do_you_like_him( $prods );
}
But, is this the right place to put this logic as, I have read on the mailing list that people would use Model::* as a wrapper to the business logic code that is elsewhere.
So, would this be better (haven't thought this through because as I said, my head is too full) ? .............
lib/P4C/Controller/SelectProduct.pm
/Model/Product.pm
/P4CSchema/Product.pm (amongst others)
/P4CSchema.pm
/P4C.pm
/Logic/Product.pm
and have in Model::Product one of.....
use base qw|Logic::Product Catalyst::Model::DBIC::Schema|;
use base qw|Catalyst::Model::DBIC::Schema Logic::Product|;
and let Logic::Product do the .......
sub get_all_products_that_like_me() {
my $self = shift;
## what's the use of schema in this command when it works okay without?
##$self->schema()->resultset('P4CSchema::ProductGroup')->all();
my $prods = $self->resultset('P4CSchema::ProductGroup')->all();
return $self->do_you_like_him( $prods );
}
Is that's what meant by 'wrapper'?
Phew, okay, that's it for now. I have my flame pants on, so go ahead ;)
Many thanks
-Ants
---------------------------------
Yahoo! Mail is the world's favourite email. Don't settle for less, sign up for your freeaccount today.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/catalyst/attachments/20070503/78ca1107/attachment.htm
More information about the Catalyst
mailing list