Bread-Board
view release on metacpan or search on metacpan
lib/Bread/Board/Manual/Concepts.pod view on Meta::CPAN
# PODNAME: Bread::Board::Manual::Concepts
# ABSTRACT: An overview of the concepts in Bread::Board
__END__
=pod
=encoding UTF-8
=head1 NAME
Bread::Board::Manual::Concepts - An overview of the concepts in Bread::Board
=head1 VERSION
version 0.37
=head1 INTRODUCTION
This document attempts to convey the central concepts of Bread::Board
and show how they work together to manage both object lifecycles and
object dependencies.
In this document we use the raw OO syntax of Bread::Board, this is
so that the concepts being illustrated are not clouded by syntactic
sugar. We only introduce the I<sugar> layer at the end, at which point
we hope that it will become clear what is going on "under the hood"
when you use it.
=head1 CONCEPTS
=head2 What is Inversion of Control?
Inversion of Control (or IoC) is the very simple idea of releasing
control of some part of your application over to some other part
of your application, be it your code or an outside framework.
IoC is a common paradigm in GUI frameworks, whereby you give up
control of your application flow to the framework and install your
code at callbacks hooks within the framework. For example, take a
very simple command line interface; the application asks a
question, the user responds, the application processes the answer
and asks another question, and so on until it is done. Now consider
the GUI approach for the same application; the application displays
a screen and goes into an event loop, users actions are processed
with event handlers and callback functions. The GUI framework has
inverted the control of the application flow and relieved your
code from having to deal with it.
IoC is also sometimes referred to as 'Dependency Injection' or the
'Dependency Injection Principle', and many people confused the two.
However IoC and dependency injection are not the same, in fact the
concepts behind dependency injection are actually just an
I<example of> IoC principles in action, in particular about your
applications dependency relationships. IoC is also sometimes
referred to as the Hollywood Principle because of the I<don't call
us we'll call you> approach of things like callback functions and
event handlers.
Howard Lewis Ship, the creator of the HiveMind IoC Framework, once
referred to dependency injection as being the inverse of garbage
collection. With garbage collection you hand over the details of
the destruction of your objects to the garbage collector. With
dependency injection you are handing over control of object
creation, which also includes the satisfaction of your dependency
relationships.
The following sections will explain the basis concepts around the
Bread::Board and how it relates to the concept of IoC.
=head2 Containers
The central part of just about any IoC framework is the container.
A container's responsibilities are roughly to dispense services and
to handle the resolution of said service's dependency relationships.
First we can start with a simple container for our services to
live in. We give the container a name so that we can address it
later on, think of this like a package namespace.
my $c = Bread::Board::Container->new( name => 'Application' );
Next we need to add a service to that container (we will explain
services a little later on).
$c->add_service(
Bread::Board::BlockInjection->new(
name => 'logger',
block => sub { Logger->new() }
)
);
Now if we want an instance of our 'logger' service, we simply ask the
container for it.
my $logger_service = $c->fetch('logger');
And we then can ask the service to give us an instance of our Logger
object.
( run in 0.918 second using v1.01-cache-2.11-cpan-39bf76dae61 )