NAME

Kaiten::Container - Simples dependency-injection (DI) container, distant relation of IoC.

VERSION

Version 0.21

SYNOPSIS

This module resolve dependency injection conception in easiest way ever. You are just create some code first and put it on kaiten in named container. Later you take it by name and got yours code result fresh and crispy.

No more humongous multi-level dependency configuration, service provider and etc.

You got what you put on, no more, no less.

Ok, little bit more - Kaiten::Container run |probe| sub every time when you want to take something to ensure all working properly.

And another one - KC try to re-use |handler| return if it requested.

use Kaiten::Container;

my $config = {
     ExampleP => {
         handler  => sub { return DBI->connect( "dbi:ExampleP:", "", "", { RaiseError => 1 } ) or die $DBI::errstr },
         probe    => sub { shift->ping() },
         settings => { reusable => 1 }
     }
};

my $container = Kaiten::Container->new( init => $config );
my $dbh = $container->get_by_name('ExampleP');

All done, now we are have container and may get DB handler on call. Simple!

SUBROUTINES/METHODS

new(%init_configuration?)

This method create container with entities as |init| configuration hash values, also may called without config. Its possible add all entities later, with add method.

my $config = {
     ExampleP => {
         handler  => sub { return DBI->connect( "dbi:ExampleP:", "", "", { RaiseError => 1 } ) or die $DBI::errstr },
         probe    => sub { shift->ping() },
         settings => { reusable => 1 }
     },
     test => {
         handler  => sub        { return 'Hello world!' },
         probe    => sub        { return 1 },
    },
};

my $container = Kaiten::Container->new( init => $config );  

Entity MUST have:

- unique name

- |handler| sub - its return something helpfully

- |probe| sub - its must return true, as first arguments this sub got |handler| sub result.

Entity MAY have settings hashref:

- 'reusable' if it setted to true - KC try to use cache. If cached handler DONT pass probe KC try to create new one instance.

NB. New instance always be tested by call |probe|. If you dont want test handler - just cheat with

probe => sub { 1 }

but its bad idea, I notice you.

get_by_name($what)

Use this method to execute |handler| sub and get it as result.

my $dbh = $container->get_by_name('ExampleP');
# now $dbh contain normal handler to ExampleP DB

add(%config)

Use this method to add some more entities to container.

my $configutarion_explodable = {
       explode => {
                    handler  => sub        { return 'ExplodeSQL there!' },
                    probe    => sub        { state $a= [ 1, 0, 0 ]; return shift @$a; },
                    settings => { reusable => 1 }
                  },
       explode_now => { 
                    handler => sub { return 'ExplodeNowSQL there!' },
                    probe    => sub        { 0 },
                    settings => { reusable => 1 }
                  },
};

$container->add(%$configutarion_explodable); # list, NOT hashref!!!

remove(@what)

This method remove some entities from container

$container->remove('explode_now','ExampleP'); # list, NOT arayref!!!

show_list

Use this method to view list of available handler in container

my @handler_list = $container->show_list;

# @handler_list == ( 'explode', 'test' )

NB. Entities sorted with perl sort function

AUTHOR

Meettya, <meettya at cpan.org>

BUGS

Please report any bugs or feature requests to bug-kaiten-container at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Kaiten-Container. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

DEVELOPMENT

Repository

https://github.com/Meettya/Kaiten-Container

SUPPORT

You can find documentation for this module with the perldoc command.

perldoc Kaiten::Container

You can also look for information at:

SEE ALSO

Bread::Broad - a Moose-based DI framework

IOC - the ancestor of Bread::Board

Peco::Container - another DI container

IOC::Slinky::Container - an alternative DI container

ACKNOWLEDGEMENTS

LICENSE AND COPYRIGHT

Copyright 2011 Meettya.

This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.