Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Catalyst: Users

Automatic passing of objects to DBIx::Class

 

 

Catalyst users RSS feed   Index | Next | Previous | View Threaded


ido at ido50

Feb 27, 2010, 10:58 PM

Post #1 of 5 (1114 views)
Permalink
Automatic passing of objects to DBIx::Class

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.


benvanstaveren at gmail

Feb 27, 2010, 11:21 PM

Post #2 of 5 (1062 views)
Permalink
Re: Automatic passing of objects to DBIx::Class [In reply to]

I've ran into the same issue, but solved it in a bit of a hackish
fashion. I went the route of the schema objects knowing nothing about
the outside world, just themselves, and their relations with other
schema objects.

On top of this sits a separate set of objects that contain all the logic
necessary to make things "work", using copious amounts of autoload
voodoo. These objects can basically be used from any other app, be it
Catalyst or some command line tool, and don't really have to know much
else. For Catalyst I've got a custom model object that will return the
appropriate "logic" object, whereas in offline tools they're used directly.

Sort of solves the problem in a cleaner way if you ask me.


Ido Perlmuter 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.
> ------------------------------------------------------------------------
>
> _______________________________________________
> List: Catalyst [at] lists
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
> Dev site: http://dev.catalyst.perl.org/
>

--
Ben van Staveren
phone: +62 81 70777529
email: benvanstaveren [at] gmail


garrison at zeta

Feb 28, 2010, 12:41 AM

Post #3 of 5 (1064 views)
Permalink
Re: Automatic passing of objects to DBIx::Class [In reply to]

Good evening,

On 28/02/10 at 8:58 AM +0200, Ido Perlmuter <ido [at] ido50> wrote:

>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.

I think you're looking for ACCEPT_CONTEXT or InstancePerContext.
That's how I solved passing values from Catalyst to DBIC.

<http://www.google.com/search?q=Catalyst%20ACCEPT_CONTEXT>
<http://search.cpan.org/perldoc?Catalyst::Component::InstancePerContext>

I seem to recall reading that you should decide what data/values
your DBIC model needs, and then only pass those in
ACCEPT_CONTEXT, rather than passing whole objects like $c->req.
That way when you use a different front-end, you don't have to
mock a $c->req object, you simply pass the values the DBIC
schema requires.

In your example you want the "user ID and the user IP" so grab
those from $c->req during ACCEPT_CONTEXT (or
build_per_context_instance) and pass them to your schema.


Charlie

--
Ꮚ Charlie Garrison ♊ <garrison [at] zeta>
〠 PO Box 141, Windsor, NSW 2756, Australia

O< ascii ribbon campaign - stop html mail - www.asciiribbon.org
http://www.ietf.org/rfc/rfc1855.txt

_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/


ido at ido50

Feb 28, 2010, 4:10 AM

Post #4 of 5 (1056 views)
Permalink
Re: Automatic passing of objects to DBIx::Class [In reply to]

Thanks, I suppose that would work.

Ido.

On Sun, Feb 28, 2010 at 10:41 AM, Charlie Garrison <garrison [at] zeta>wrote:

> Good evening,
>
>
> On 28/02/10 at 8:58 AM +0200, Ido Perlmuter <ido [at] ido50> wrote:
>
> 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.
>>
>
> I think you're looking for ACCEPT_CONTEXT or InstancePerContext. That's how
> I solved passing values from Catalyst to DBIC.
>
> <http://www.google.com/search?q=Catalyst%20ACCEPT_CONTEXT>
> <http://search.cpan.org/perldoc?Catalyst::Component::InstancePerContext>
>
> I seem to recall reading that you should decide what data/values your DBIC
> model needs, and then only pass those in ACCEPT_CONTEXT, rather than passing
> whole objects like $c->req. That way when you use a different front-end, you
> don't have to mock a $c->req object, you simply pass the values the DBIC
> schema requires.
>
> In your example you want the "user ID and the user IP" so grab those from
> $c->req during ACCEPT_CONTEXT (or build_per_context_instance) and pass them
> to your schema.
>
>
> Charlie
>
> --
> Ꮚ Charlie Garrison ♊ <garrison [at] zeta>
> 〠 PO Box 141, Windsor, NSW 2756, Australia
>
> O< ascii ribbon campaign - stop html mail - www.asciiribbon.org
> http://www.ietf.org/rfc/rfc1855.txt
>
>
> _______________________________________________
> List: Catalyst [at] lists
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive:
> http://www.mail-archive.com/catalyst [at] lists/
> Dev site: http://dev.catalyst.perl.org/
>


jshirley at gmail

Feb 28, 2010, 10:10 AM

Post #5 of 5 (1048 views)
Permalink
Re: Automatic passing of objects to DBIx::Class [In reply to]

On Sat, Feb 27, 2010 at 10:58 PM, Ido Perlmuter <ido [at] ido50> 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

_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/

Catalyst users RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.