|
NAMEIOC::Container - An IOC Container object SYNOPSIS use IOC::Container;
my $container = IOC::Container->new();
$container->register(IOC::Service::Literal->new('log_file' => "logfile.log"));
$container->register(IOC::Service->new('logger' => sub {
my $c = shift;
return FileLogger->new($c->get('log_file'));
}));
$container->register(IOC::Service->new('application' => sub {
my $c = shift;
my $app = Application->new();
$app->logger($c->get('logger'));
return $app;
}));
$container->get('application')->run();
# or a more complex example
# utilizing a tree-like structure
# of services
my $logging = IOC::Container->new('logging');
$logging->register(IOC::Service->new('logger' => sub {
my $c = shift;
return My::FileLogger->new($c->find('/filesystem/filemanager')->openFile($c->get('log_file')));
}));
$logging->register(IOC::Service::Literal->new('log_file' => '/var/my_app.log'));
my $database = IOC::Container->new('database');
$database->register(IOC::Service->new('connection' => sub {
my $c = shift;
return My::DB->connect($c->get('dsn'), $c->get('username'), $c->get('password'));
}));
$database->register(IOC::Service::Literal->new('dsn' => 'dbi:mysql:my_app'));
$database->register(IOC::Service::Literal->new('username' => 'test'));
$database->register(IOC::Service::Literal->new('password' => 'secret_test'));
my $file_system = IOC::Container->new('filesystem');
$file_system->register(IOC::Service->new('filemanager' => sub { return My::FileManager->new() }));
my $container = IOC::Container->new();
$container->addSubContainers($file_system, $database, $logging);
$container->register(IOC::Service->new('application' => sub {
my $c = shift;
my $app = My::Application->new();
$app->logger($c->find('/logging/logger'));
$app->db_connection($c->find('/database/connection'));
return $app;
}));
$container->get('application')->run();
DESCRIPTIONIn this IOC framework, the IOC::Container object holds instances of IOC::Service objects keyed by strings. It can also have sub-containers, which are instances of IOC::Container objects also keyed by string. +------------------+
| IOC::Container |
+---------+--------+
|
+------------------+-----------------+
| | |
(*sub-containers) (*proxies) (*services)
| | |
V V V
+------------------+ +--------------+ +--------------+
| IOC::Container | | IOC::Proxy | | IOC::Service |
+------------------+ +--------------+ +--------------+
|
(instance)
|
V
+-------------------------+
| <Your Component/Object> |
+-------------------------+
METHODS
Service Methods
Parent Container Methods
Sub-Container Methods
TO DOBUGSNone that I am aware of. Of course, if you find a bug, let me know, and I will be sure to fix it. CODE COVERAGEI use Devel::Cover to test the code coverage of my tests, see the CODE COVERAGE section of IOC for more information. SEE ALSOAUTHORstevan little, <stevan@iinteractive.com> COPYRIGHT AND LICENSECopyright 2004-2007 by Infinity Interactive, Inc. <http://www.iinteractive.com> This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
|