[Catalyst] best practices - model or controller ?
Peter Karman
peter at peknet.com
Sat Oct 4 13:31:27 BST 2008
Yves Räber wrote on 10/4/08 1:16 AM:
> Hello,
>
> I want to implement something really simple : log some events into a
> database. And I already can think of three way to do it, but because
> this will be used very frequently I'd like to know what's the best
> solution. For me the best solution would be to have little overhead, and
> a really short command (like $c->logdb()).
>
> 1/ In the DBIC Model
> --------------------
> package MyApp::Model::AppDB
>
> sub add() {
> my $self = shift;
> my $message = shift;
>
> my $log = $self->resultset('Log');
> $log->create( {
> message => $message
> });
> }
>
> And then call $c->model('AppDB')->add('Hello World');
>
> This seems ok, but $c->model('AppDB')->add('Hello World') ... too much
> characters.
>
> 2/ In the controller
> --------------------
> (in Root.pm)
> sub log : Private {
> my ($self, $c, $message) = @_;
> $c->model('AppDB::Log')->create({
> message => $message;
> });
> }
>
> And then call $c->forward('/log', [ 'Hello World' ]);
>
> This doesn't seem really elegant to me.
>
> 3/ As a plugin
>
> This seems really overkill, but I like the idea of having a really short
> command like $c->logdb(...);
>
> So could someone tell be what is best practice is ?
>
You don't need a real plugin unless you need to override the dispatch process.
But I often put convenience methods in my MyApp.pm base class. So implement your
idea #1 and then add:
package MyApp;
sub logdb {
my $c = shift;
my $msg = shift or croak "msg required";
$c->model('AppDB')->add($msg);
}
--
Peter Karman . http://peknet.com/ . peter at peknet.com
More information about the Catalyst
mailing list