The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

IOC - A lightweight IOC (Inversion of Control) framework

SYNOPSIS

  use IOC;
  
  my $container = IOC::Container->new();
  $container->register(IOC::Service->new('log_file' => sub { "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();   

DESCRIPTION

NOTE: This is very alpha software, and a very early release of this module. There are many plans in the works, and should be coming soon as this project is currently under active development. That said, submission, suggestions and general critisism is welcomed and encouraged.

This module provide a lightweight IOC or Inversion of Control framework. Inversion of Control, sometimes called Dependency Injection, is a component management style which aims to clean up component configuration and provide a cleaner, more flexible means of configuring a large application.

A similar style of component management is the Service Locator, in which a global Service Locator object holds instances of components which can be retrieved by key. The common style is to create and configure each component instance and add it into the Service Locator. The main drawback to this approach is the aligning of the dependencies of each component prior to inserting the component into the Service Locator. If your dependency requirements change, then your initialization code must change to accomidate. This can get quite complex when you need to re-arrange initialization ordering and such. The Inversion of Control style alleviates this problem by taking a different approach.

With Inversion of Control, you configure a set of individual Service objects, which know how to initialize their particular components. If these components have dependencies, the will resolve them through the IOC framework itself. This results in a loosly coupled configuration which places no expectation upon initialization order. If your dependency requirements change, you need only adjust your Service's initialization routine, the ordering will adapt on it's own.

Now, if you are confused by my description above, then I suggest going to this page and reading the description there. This author does a much better job than I could ever hope to in explaining Inversion of Control (or as he calls it Dependency Injection). It should also be noted that the code and IoC techniques in this module were taken largely from this article. For more links and information on this topic, see the "SEE ALSO" section.

TO DO

Create a tree-like Container manager

I can use a path syntax to climb the tree (ex: 'database/connection', 'logging/logger', 'logging/log_file', etc). This would allow a finer grainer organization of services and containers, however it would also only really be useful for larger projects.

BUGS

None that I am aware of. Of course, if you find a bug, let me know, and I will be sure to fix it.

CODE COVERAGE

I use Devel::Cover to test the code coverage of my tests, below is the Devel::Cover report on this module test suite.

 ------------------------ ------ ------ ------ ------ ------ ------ ------
 File                       stmt branch   cond    sub    pod   time  total
 ------------------------ ------ ------ ------ ------ ------ ------ ------
 IOC.pm                    100.0    n/a    n/a  100.0    n/a   28.4  100.0
 IOC/Container.pm          100.0  100.0   72.7  100.0  100.0   19.8   94.9
 IOC/Exceptions.pm         100.0    n/a    n/a  100.0    n/a   17.0  100.0
 IOC/Service.pm            100.0   91.7   83.3  100.0  100.0   34.8   95.7
 ------------------------ ------ ------ ------ ------ ------ ------ ------
 Total                     100.0   95.0   78.3  100.0  100.0  100.0   96.2
 ------------------------ ------ ------ ------ ------ ------ ------ ------

SEE ALSO

Apparently at some time there was an IOC::Lite on CPAN, but the author seems to have removed it and withdrawn support for it. I have emailed the author for more information.

Some IoC Article links

The code here is based upon the code found in this article.

http://onestepback.org/index.cgi/Tech/Ruby/DependencyInjectionInRuby.rdoc

This is a decent article on IoC with Java.

http://today.java.net/pub/a//today/2004/02/10/ioc.html

An article by Martin Fowler about IoC

http://martinfowler.com/articles/injection.html

Here is a list of some Java frameworks which use the IoC technique.

Avalon

http://avalon.apache.org/products/runtime/index.html

Spring Framework

http://www.springframework.org/

PicoContainer

http://www.picocontainer.org

AUTHOR

stevan little, <stevan@iinteractive.com>

COPYRIGHT AND LICENSE

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