[Catalyst] ACCEPT_CONTEXT not invoked if COMPONENT if present

Alejandro Imass alejandro.imass at gmail.com
Sat May 28 03:04:28 GMT 2011


On Fri, May 27, 2011 at 7:22 AM, Peter Shangov <pshangov at yahoo.com> wrote:
> Hi all,
>
> I need to create a Model class that returns an object that is mostly build during component initialization time, but also has access to the current request. The API will look like this:
>

So I'm not the only to have fallen into this trap ;-)

Since you are deriving from catalyst model or component means you are
creating an 'instance' type which in turn means that your model will
be instanced once and left loaded as long as the app lives.

COMPONENT will be called only once when Catalyst starts so you can
merge the config and initialize your model internals.

ACCEPT_CONTEXT will be called when you use $c->model([instance])->method();

You should (must?) return a different model object that lives only for
that particular request. In other words, your model instance is
actually an object factory.

The idea as I understand it, is for you to code your Business Models
independent from Catalyst and call them per request with a thin
wrapper (the per-instance object created by the instance/factory),
that is, if you model actually needs the context, but will surely need
some info from the request so the thin object is there precisely to
translate that.  Also remember Catalyst runs with multiple engines so
you have to remember this when designing your models (beware of
attributes in the instance class being changed by multiple requests
(e.g. multi-threaded Perls).

Best, and good luck finding any useful info on this topic "for the
rest of us". I am trying to put together some for-dummies examples for
this particular topic to publish on the wiki and/or POD, but sadly
haven't had any free time for this.

Also take a look at: Catalyst::Component::InstancePerContext
It's supposed to ease the creating of per instance models, but I
wonder. Hope you understand it ;-)

--
Alejandro Imass



More information about the Catalyst mailing list