[Catalyst] Catalyst::Component/Model Instances and Attributes per Request

Alejandro Imass alejandro.imass at gmail.com
Sun Jul 18 20:42:07 GMT 2010


On Sun, Jul 18, 2010 at 3:36 PM, Tomas Doran <bobtfish at bobtfish.net> wrote:
>
> On 18 Jul 2010, at 19:58, Alejandro Imass wrote:
>
>> So in conclusion, it seems reasonable to say that I should not worry
>> about the global vars (the Moose object attributes) in my Model
>> Instance to get overridden by the ACCEPT_CONTEXT call, as this call
>> will only be called once in sequence with a single request. Anyone
>> disagree?
>
> Yes.
>
> They're _still_ not global variables, they are instance variables, of an
> instance which is scoped to the application lifetime.
>
> And changing the values of them per-request is still entirely wrong, yucky,
> bad design etc.
>

Ok. already! ;-)  I have a big bump in my forehead from all this
headbanging. With the headache and all I think I finally
understand...I hope... In my case I need a model that creates an
object to handle every request individually so:

1) Create a very thin layer that will be the instance. Having: BEGIN
{extends 'Catalyst::Model'; }
2) Override ACCEPT_CONTEXT
3) In this sub, create an object of type foo and return _that_ instead of $self

So my Model instance is in fact a factory. This way I don't have to
worry about the attributes because each controller will have it's own
copy of foo. Is this close?

The Definitive Guide to Catalyst has an extension to the Catalyst
Cookbook, in page 255 talks about Catalyst::Model::Factory which might
be another way of doing what I want, but perhaps overkill for my
simple problem.

TIA,
Alejandro

> If you need an ACCEPT_CONTEXT method, than return a _different_ instance
> which has the mutable state, rather than smashing things in your per-app
> instance and returning that.
>
> (But yes, even if you do the thing I'm recommending against, it will work
> without multiple threads stamping on each other).
>
> Cheers
> t0m
>
>
> _______________________________________________
> List: Catalyst at lists.scsys.co.uk
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
> Dev site: http://dev.catalyst.perl.org/
>



More information about the Catalyst mailing list