[Catalyst] best practices - model or controller ?

Yves Räber yraber at mailup.net
Sat Oct 4 07:16:41 BST 2008


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 ? 

Thanks.

Yves.




More information about the Catalyst mailing list