Terrance,

If you are going to include classes which are not just specifically lazy loaders, then you can also include IOC::Service which has the deferred() method which will create an IOC::Service::Deferred object for you. IOC::Service::Deferred objects cannot be instantiated directly, only through the IOC::Service object (it checks the caller and throws an exception if not called from within an IOC::Service object). Basic usage of it would be:

my $container = IOC::Container->new('DBI');
# create the dsn, username and password services here
# and put them in the container
my $service = IOC::Service->new('connection' => sub {
        my $c = shift;
        DBI->connect($c->get('dsn'), $c->get('username'), $c->get('password'));
});
$container->register($service);

my $dbh = $service->deferred();

Now the moment anyone uses $dbh, it will resolve the service. You can also use IOC::Service::ConstructorInjection, IOC::Service::SetterInjection, IOC::Service::Prototype, IOC::Service::Prototype::ConstructorInjection and IOC::Service::Prototype::SetterInjection in this way as well.

However it should be noted that this feature was never really intended to be used directly (hence it's ackward usage), and was only meant as a means of allowing for circular dependencies in the IOC framework, so you cannot directly instantiate an IOC::Service::Deferred object through an IOC::Container (containers hold services), you must retain the reference to the IOC::Service itself to get the deferred instance.

Also the code is based heavily on Class::LazyLoad which Rob Kinyon and I worked on, which is a much better general purpose lazy loader.

Steve

On Jan 25, 2005, at 6:36 PM, Terrence Brannon wrote:


In reading the docs to Class::Container, I noticed that object creation could be delayed. Hence is belongs in the list of module/object lazy loaders. Here's the entry:

=item * Class::Container by Ken Williams

L<http://search.cpan.org/dist/Class-Container>

This module's primary purpose is to provide something similar to
Inversion of Control: the container establishes and provides the
dependencies of a class instead of the class doing so.

Inversion of Control is discussed here:

L<http://www.picocontainer.org/Inversion+of+Control>



--
        Carter's Compass: I know I'm on the right track when,
           by deleting something, I'm adding functionality.


_______________________________________________ sw-design mailing list [email protected] http://metaperl.com/cgi-bin/mailman/listinfo/sw-design



_______________________________________________
sw-design mailing list
[email protected]
http://metaperl.com/cgi-bin/mailman/listinfo/sw-design

Reply via email to