[Catalyst] Automatic passing of objects to DBIx::Class

J. Shirley jshirley at gmail.com
Sun Feb 28 18:10:15 GMT 2010


On Sat, Feb 27, 2010 at 10:58 PM, Ido Perlmuter <ido at ido50.net> wrote:
> Hi everyone.
>
> I've been racking my brain trying to find a way to automatically pass an
> object, for the matter the Catalyst::Request object, to DBIx::Class methods.
> I cannot and will not pass it myself every time I call a method in my
> schema. While I do believe the model and the app should be completely
> separated, further information must flow between the two.
>
> For example, I need to setup an administrative usage log for my app.
> Whenever a user with proper privileges invokes a schema method that creates,
> edits or deletes information from the database, I want that action to be
> automatically recorded in the usage log, with details such as the user ID
> and the user IP. I'm facing two difficulties: on one hand I believe this
> should be automatically done by the schema (so recording into the usage log
> will not be made from the app, but from the schema), but that means I need
> to pass the
> context object (or the Request object) to every schema method I invoke; on
> the other hand, if I let the app handle recording into the usage log, my
> schema loses power. What if I want to write an API to my program in several
> programming languages, should they do the same too?
>
> So for a while now I've been trying to find a way to automatically pass
> objects to the schema. Maybe if DBIx::Class was Moose-based I would have
> already found one using method modifiers.
>
> Any thoughts will be appreciated.
>
> Thanks,
> Ido Perlmuter.
>


I would recommend writing a simple trait that gets applied.  You can
look at this for inspiration:
http://cpansearch.perl.org/src/FAYLAND/Catalyst-TraitFor-Model-DBIC-Schema-QueryLog-0.03/lib/Catalyst/TraitFor/Model/DBIC/Schema/QueryLog.pm

It's really quite simple, and the usage is great -- you just add a
configuration line and everything works.  I use custom traits to
decorate my schema model with application-specific objects, and have
yet to have any significant complaints.

-Jay



More information about the Catalyst mailing list