[Catalyst] Making a hash available across the application

Dermot paikkos at googlemail.com
Tue Mar 30 12:09:27 GMT 2010


On 17 March 2010 23:08, Tomas Doran <bobtfish at bobtfish.net> wrote:
>
> On 17 Mar 2010, at 18:48, Ram Dobson wrote:
>>
>> I'm kinda new to Catalyst too, but i believe this is what one might call a
>> "helper" function. Anybody who knows more have an opinion on the relative
>> merit of my change?
>
>
> Crapping stuff into the top level context class like that works, however as
> you do this you're throwing away reusability (as that stuff can now only be
> used inside Catalyst, not in anything else reusing the same database
> schema), and you're going towards making a 'god object'.
>
> I'm not saying that doing something like this isn't _sometimes_ fine, but in
> this case, my mental model would consider the caching of some data values
> (which is basically what you're doing here) a function of the domain model
> layer, not of the web application per-se.
>
> In fact, generally - you don't want to be calling the generic ->search
> method in your controllers _at all_.
>
> The specific search functionality should instead be pushed down into your
> ResultSet classes, so that you say:
>
> $c->model('DB::Books')->find_all_books_by_author($author_name);
>
> Rather than $c->model('DB::Books')->search({ %stuff_including_author_name },
> { %ordering_stuff });
>


I had to take a rather difficult learning curve through DBIx::Class
and move from load_classes to load_namespaces(). I'm fortunate enough
to have had Aperion to hold my hand through the process. This has
enabled me to make a first step towards what I think t0m was driving
at. So what I have at the moment is

package MyApp::Local::Schema::Resultset::Country;
use strict;
use warnings;
use parent qw/DBIx::Class::ResultSet/;

my @confirmed_countries;
sub get_confirmed_countries {
    my $self = shift;
    return \@confirmed_countries if ($confirmed_countries[1])  || do {
             # return if the array has members
        @confirmed_countries = $self->search({ foo=> 1});
    };
}

1;

Then in my Root controller it is pushed into the stash. The DBIC_trace
shows only one call to the DB which is smashing, marvellous and
fantastic. I think in this case I fall into that __sometimes__ area
because this structure is required in 90% of all pages. I have also
managed to keep the method available to non-cat applications. Ido is
correct though, sometimes I simply forget that Catalyst is perl and
loose my way.

Thanks for all the help.
Dp.



More information about the Catalyst mailing list