Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^2: RFC: Object::Proxy (or somesuch)

by stvn (Monsignor)
on Nov 19, 2004 at 19:52 UTC ( [id://409140]=note: print w/replies, xml ) Need Help??


in reply to Re: RFC: Object::Proxy (or somesuch)
in thread RFC: Object::Proxy (or somesuch)

Also, with this module you can lazy load something on an instance basis as well, most all the other options only allow class level deferment. You load instances like this:

my $instance = Class::Plugin::LazyLoad->proxy_instance('My::Class', @c +onstructor_args);
The $instance variable is now a lazy-loader object which will only inflate when you touch it. One of my big goals for this was to be as non-invasive as possible.

-stvn

Replies are listed 'Best First'.
Re^3: RFC: Object::Proxy (or somesuch)
by perrin (Chancellor) on Nov 19, 2004 at 20:31 UTC
    Do you have a real-world example of when this would be useful? I understand that it's not exactly like these other modules, but it's close enough that it's hard for me to see the point.
      I'm loading up a Tk window for a customer. There are some 2-3 dozen invoices for that customer. I go ahead and pull the info from the database for the invoices, and instantiate them. Now, part of the initialization of the invoice object is to instantiate all the lineitems associated with that invoice. Each invoice makes its own database call(s) to load up its lineitems.

      Now, the average user of this screen will look at 2-3 invoices. Why should I pre-load 25-35 invoices, with some 50-70 DB queries, when I can defer loading until the user actually tells me which one they care about?

      Now, so far, you say "Just use another screen." But, I didn't write the Invoice object code. I am told I have to reuse code because my PHB needs to look good, but I am not allowed to modify it because my PHB has no clout.

      Enter Class::LazyLoad. The Invoice object doesn't even know it's being deferred, nor does it care. The Customer screen now loads really fast and all the information that used to be there still is.

      Being right, does not endow the right to be rude; politeness costs nothing.
      Being unknowing, is not the same as being stupid.
      Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
      Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

        This is the kind of thing that Class::DBI does for me automatically, but that doesn't help you since this is someone else's code. After taking a brief glance at Class::LazyObject though, I think that it would do what you want without additional effort. There is also Class::Proxy and Class::Proxy::Lite, but the docs for those are a bit harder to follow.
        Let me just say:

        kewl!

        /J

      The point of being able to lazyload just a single instance is basically to be as unintrusive as possible, while still allowing this to be possible. Let me explain in more detail.

      Say you want to lazy load ever instance of the class.

      use Class::LazyLoader 'My::Class'; push @lazy_classes => My::Class->new() for 1 .. 100;
      Every single instance of My::Class is now lazily loaded. This is obvious, and how most of the class proxy/lazy loading module out there work.

      Now take this particular example (based on the IOC issues mentioned above in response to dio).

      My A constructor requires an instance of B, and my B constructor requires an instance of A. The best solution is to give A a real B instance, and give B a lazy-loaded A instance.

      Now say my C constructor requires an instance of A. I previously gave B a lazy-loaded A, however, there is no need to give C a lazy-loaded A, that is an unessecary level of indirection. But if lazy-loading was on a class level only, I would not be able to make this choice. With lazy-loading being available on a per-instance basis I can give C a real A instance.

      Of course the need (or desire) to fiddle at this low a level is a rare instance and one not likely to be an issue for anyone but me. However, this is exactly the issue that caused dragonchild and I to start talking about and building this module.

      -stvn

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://409140]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2024-04-19 18:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found