Codebase list libbread-board-perl / HEAD
HEAD

Tree @HEAD (Download .tar.gz)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
# NAME

Bread::Board - A solderless way to wire up your application components

# VERSION

version 0.37

# SYNOPSIS

```perl
use Bread::Board;

my $c = container 'MyApp' => as {

    service 'log_file_name' => "logfile.log";

    service 'logger' => (
        class        => 'FileLogger',
        lifecycle    => 'Singleton',
        dependencies => [ 'log_file_name' ],
    );

    container 'Database' => as {
        service 'dsn'      => "dbi:SQLite:dbname=my-app.db";
        service 'username' => "user234";
        service 'password' => "****";

        service 'dbh' => (
            block => sub {
                my $s = shift;
                require DBI;
                DBI->connect(
                    $s->param('dsn'),
                    $s->param('username'),
                    $s->param('password'),
                ) || die "Could not connect";
            },
            dependencies => [ 'dsn', 'username', 'password' ]
        );
    };

    service 'application' => (
        class        => 'MyApplication',
        dependencies => {
            logger => 'logger',
            dbh    => 'Database/dbh',
        }
    );

};

no Bread::Board; # removes keywords

# get an instance of MyApplication
# from the container
my $app = $c->resolve( service => 'application' );

# now user your MyApplication
# as you normally would ...
$app->run;
```

# DESCRIPTION

Bread::Board is an inversion of control framework with a focus on
dependency injection and lifecycle management. It's goal is to
help you write more decoupled objects and components by removing
the need for you to manually wire those objects/components together.

Want to know more? See the [Bread::Board::Manual](https://metacpan.org/pod/Bread::Board::Manual).

```
+-----------------------------------------+
|          A B C D E   F G H I J          |
|-----------------------------------------|
| o o |  1 o-o-o-o-o v o-o-o-o-o 1  | o o |
| o o |  2 o-o-o-o-o   o-o-o-o-o 2  | o o |
| o o |  3 o-o-o-o-o   o-o-o-o-o 3  | o o |
| o o |  4 o-o-o-o-o   o-o-o-o-o 4  | o o |
| o o |  5 o-o-o-o-o   o-o-o-o-o 5  | o o |
|     |  6 o-o-o-o-o   o-o-o-o-o 6  |     |
| o o |  7 o-o-o-o-o   o-o-o-o-o 7  | o o |
| o o |  8 o-o-o-o-o   o-o-o-o-o 8  | o o |
| o o |  9 o-o-o-o-o   o-o-o-o-o 9  | o o |
| o o | 10 o-o-o-o-o   o-o-o-o-o 10 | o o |
| o o | 11 o-o-o-o-o   o-o-o-o-o 11 | o o |
|     | 12 o-o-o-o-o   o-o-o-o-o 12 |     |
| o o | 13 o-o-o-o-o   o-o-o-o-o 13 | o o |
| o o | 14 o-o-o-o-o   o-o-o-o-o 14 | o o |
| o o | 15 o-o-o-o-o   o-o-o-o-o 15 | o o |
| o o | 16 o-o-o-o-o   o-o-o-o-o 16 | o o |
| o o | 17 o-o-o-o-o   o-o-o-o-o 17 | o o |
|     | 18 o-o-o-o-o   o-o-o-o-o 18 |     |
| o o | 19 o-o-o-o-o   o-o-o-o-o 19 | o o |
| o o | 20 o-o-o-o-o   o-o-o-o-o 20 | o o |
| o o | 21 o-o-o-o-o   o-o-o-o-o 21 | o o |
| o o | 22 o-o-o-o-o   o-o-o-o-o 22 | o o |
| o o | 22 o-o-o-o-o   o-o-o-o-o 22 | o o |
|     | 23 o-o-o-o-o   o-o-o-o-o 23 |     |
| o o | 24 o-o-o-o-o   o-o-o-o-o 24 | o o |
| o o | 25 o-o-o-o-o   o-o-o-o-o 25 | o o |
| o o | 26 o-o-o-o-o   o-o-o-o-o 26 | o o |
| o o | 27 o-o-o-o-o   o-o-o-o-o 27 | o o |
| o o | 28 o-o-o-o-o ^ o-o-o-o-o 28 | o o |
+-----------------------------------------+
```

Loading this package will automatically load the rest of the packages needed by
your Bread::Board configuration.

# EXPORTED FUNCTIONS

The functions of this package provide syntactic sugar to help you build your
Bread::Board configuration. You can build such a configuration by constructing
the objects manually instead, but your code may be more difficult to
understand.

## `container`

### simple case

```
container $name, \&body;
```

This function constructs and returns an instance of [Bread::Board::Container](https://metacpan.org/pod/Bread::Board::Container).
The (optional) `&body` block may be used to add services or sub-containers
within the newly constructed container. Usually, the block is not passed
directly, but passed using the `as` function.

For example,

```perl
container 'MyWebApp' => as {
    service my_dispatcher => (
        class => 'MyWebApp::Dispatcher',
    );
};
```

If `$name` starts with `'+'`, and the container is being declared inside
another container, then this declaration will instead extend an existing
container with the name `$name` (without the `'+'`).

### from an instance

```
container $container_instance, \&body
```

In many cases, subclassing [Bread::Board::Container](https://metacpan.org/pod/Bread::Board::Container) is the easiest route to
getting access to this framework. You can do this and still get all the
benefits of the syntactic sugar for configuring that class by passing an
instance of your container subclass to `container`.

You could, for example, configure your container inside the `BUILD` method of
your class:

```perl
package MyWebApp;
use Moose;

extends 'Bread::Board::Container';

sub BUILD {
    my $self = shift;

    container $self => as {
        service dbh => ( ... );
    };
}
```

### with parameters

```
container $name, \@parameters, \&body
```

A third way of using the `container` function is to build a parameterized
container. These are useful as a way of providing a placeholder for parts of
the configuration that may be provided later. You may not use an instance
object in place of the `$name` in this case.

For more detail on how you might use parameterized containers, see
["Parameterized Containers" in Bread::Board::Manual::Concepts::Advanced](https://metacpan.org/pod/Bread::Board::Manual::Concepts::Advanced#Parameterized-Containers).

## `as`

```
as { some_code() };
```

This is just a replacement for the `sub` keyword that is easier to read when
defining containers.

## `service`

```
service $name, $literal;
service $name, %service_description;
```

Within the `as` blocks for your containers, you may construct services using
the `service` function. This can construct several different kinds of services
based upon how it is called.

### literal services

To build a literal service (a [Bread::Board::Literal](https://metacpan.org/pod/Bread::Board::Literal) object), just specify a
scalar value or reference you want to use as the literal value:

```perl
# In case you need to adjust the gravitational constant of the Universe
service gravitational_constant => 6.673E-11;
```

### using injections

To build a service using one of the injection services, just fill in all the
details required to use that sort of injection:

```perl
service search_service => (
    class => 'MyApp::Search',
    block => sub {
        my $s = shift;
        MyApp::Search->new($s->param('url'), $s->param('type'));
    },
    dependencies => {
        url => 'search_url',
    },
    parameters => {
        type => { isa => 'Str', default => 'text' },
    },
);
```

The type of injection performed depends on the parameters used. You may use
the `service_class` parameter to pick a specific injector class. For
instance, this is useful if you need to use [Bread::Board::SetterInjection](https://metacpan.org/pod/Bread::Board::SetterInjection)
or have defined a custom injection service.  If you specify a `block`, block
injection will be performed using [Bread::Board::BlockInjection](https://metacpan.org/pod/Bread::Board::BlockInjection). If neither
of these is present, constructor injection will be used with
[Bread::Board::ConstructorInjection](https://metacpan.org/pod/Bread::Board::ConstructorInjection) (and you must provide the `class`
option).

### service dependencies

The `dependencies` parameter takes a hashref of dependency names mapped to
[Bread::Board::Dependency](https://metacpan.org/pod/Bread::Board::Dependency) objects, but there are several coercions and sugar
functions available to make specifying dependencies as easy as possible. The
simplest case is when the names of the services you're depending on are the
same as the names that the service you're defining will be accessing them with.
In this case, you can just specify an arrayref of service names:

```perl
service foo => (
    dependencies => [ 'bar', 'baz' ],
    # ...
);
```

If you need to use a different name, you can specify the dependencies as a
hashref instead:

```perl
service foo => (
    dependencies => {
        dbh => 'foo_dbh',
    },
    # ...
);
```

You can also specify parameters when depending on a parameterized service:

```perl
service foo => (
    dependencies => [
        { bar => { bar_param => 1 } },
        'baz',
    ],
    # ...
);
```

Finally, services themselves can also be specified as dependencies, in which
case they will just be resolved directly:

```perl
service foo => (
    dependencies => {
        dsn => Bread::Board::Literal->new(
            name  => 'dsn',
            value => 'dbi:mysql:mydb',
        ),
    },
    # ...
);
```

As a special case, an arrayref of dependencies will be interpreted as a service
which returns an arrayref containing the resolved values of those dependencies:

```perl
service foo => (
    dependencies => {
        # items will resolve to [ $bar_service->get, $baz_service->get ]
        items => [
            'bar',
            Bread::Board::Literal->new(name => 'baz', value => 'BAZ'),
        ],
    },
    # ...
);
```

### inheriting and extending services

If the `$name` starts with a `'+'`, the service definition will instead
extend an existing service with the given `$name` (without the `'+'`). This
works similarly to the `has '+foo'` syntax in Moose. It is most useful when
defining a container class where the container is built up in `BUILD` methods,
as each class in the inheritance hierarchy can modify services defined in
superclasses. The `dependencies` and `parameters` options will be merged with
the existing values, rather than overridden. Note that literal services can't
be extended, because there's nothing to extend. You can still override them
entirely by declaring the service name without a leading `'+'`.

## `literal`

```
literal($value);
```

Creates an anonymous [Bread::Board::Literal](https://metacpan.org/pod/Bread::Board::Literal) object with the given value.

```perl
      service 'dbh' => (
          block => sub {
              my $s = shift;
              require DBI;
              DBI->connect(
                  $s->param('dsn'),
                  $s->param('username'),
                  $s->param('password'),
              ) || die "Could not connect";
          },
          dependencies => {
            dsn      => literal 'dbi:SQLite:somedb',
            username => literal 'foo',
            password => literal 'password',

          },
      );
```

## `depends_on`

```
depends_on($service_path);
```

The `depends_on` function creates a [Bread::Board::Dependency](https://metacpan.org/pod/Bread::Board::Dependency) object for the
named `$service_path` and returns it.

## `wire_names`

```
wire_names(@service_names);
```

This function is just a shortcut for passing a hash reference of dependencies
into the service. It is not typically needed, since Bread::Board can usually
understand what you mean - these declarations are all equivalent:

```perl
service foo => (
    class => 'Pity::TheFoo',
    dependencies => {
        foo => depends_on('foo'),
        bar => depends_on('bar'),
        baz => depends_on('baz'),
    },
);

service foo => (
    class => 'Pity::TheFoo',
    dependencies => wire_names(qw( foo bar baz )),
);

service foo => (
    class => 'Pity::TheFoo',
    dependencies => {
        foo => 'foo',
        bar => 'bar',
        baz => 'baz',
    },
);

service foo => (
    class => 'Pity::TheFoo',
    dependencies => [ qw(foo bar baz ) ],
);
```

## `typemap`

```
typemap $type, $service;
typemap $type, $service_path;
```

This creates a type mapping for the named type. Typically, it is paired with
the `infer` call like so:

```perl
typemap 'MyApp::Model::UserAccount' => infer;
```

For more details on what type mapping is and how it works, see
[Bread::Board::Manual::Concepts::Typemap](https://metacpan.org/pod/Bread::Board::Manual::Concepts::Typemap).

## `infer`

```
infer;
infer(%hints);
```

This is used with `typemap` to help create the typemap inference. It can be
used with no arguments to do everything automatically. However, in some cases,
you may want to pass a service instance as the argument or a hash of service
arguments to change how the type map works. For example, if your type needs to
be constructed using a setter injection, you can use an inference similar to
this:

```perl
typemap 'MyApp::Model::UserPassword' => infer(
    service_class => 'Bread::Board::SetterInjection',
);
```

For more details on what type mapping is and how it works, see
[Bread::Board::Manual::Concepts::Typemap](https://metacpan.org/pod/Bread::Board::Manual::Concepts::Typemap).

## `include`

```
include $file;
```

This is a shortcut for loading a Bread::Board configuration from another file.

```
include "filename.pl";
```

The above is pretty much identical to running:

```
do "filename.pl";
```

However, you might find it more readable to use `include`.

## `alias`

```
alias $service_name, $service_path, %service_description;
```

This helper allows for the creation of [service
aliases](https://metacpan.org/pod/Bread::Board::Service::Alias), which allows you to define a
service in one place and then reuse that service with a different name
somewhere else. This is sort of like a symbolic link for
services. Aliases will be [resolved
recursively](https://metacpan.org/pod/Bread::Board::Traversable#fetch), so an alias can alias an
alias.

For example,

```perl
service file_logger => (
    class => 'MyApp::Logger::File',
);

alias my_logger => 'file_logger';
```

# OTHER FUNCTIONS

These are not exported, but might be helpful to you.

## `set_root_container`

```
set_root_container $container;
```

You may use this to set a top-level root container for all container
definitions.

For example,

```perl
my $app = container MyApp => as { ... };

Bread::Board::set_root_container($app);

my $config = container Config => as { ... };
```

Here the `$config` container would be created as a sub-container of `$app`.

# ACKNOWLEDGEMENTS

Thanks to Daisuke Maki for his contributions and for really
pushing the development of this module along.

Chuck "sprongie" Adams, for testing/using early (pre-release)
versions of this module, and some good suggestions for naming
it.

Matt "mst" Trout, for finally coming up with the best name
for this module.

Gianni "dakkar" Ceccarelli for writing lots of documentation, and
Net-a-Porter.com for paying his salary while he was doing it.

# ARTICLES

[Bread::Board is the right tool for this job](http://domm.plix.at/perl/2013_04_bread_board_is_the_right_rool_for_this_job.html)
Thomas Klausner showing a use-case for Bread::Board.

# SEE ALSO

- [Bread::Board::Declare](https://metacpan.org/pod/Bread::Board::Declare)

    This provides more powerful syntax for writing Bread::Board container classes.

- [IOC](https://metacpan.org/pod/IOC)

    Bread::Board is basically my re-write of IOC.

- [http://en.wikipedia.org/wiki/Breadboard](http://en.wikipedia.org/wiki/Breadboard)

# AUTHOR

Stevan Little <stevan@iinteractive.com>

# BUGS

Please report any bugs or feature requests on the bugtracker website
https://github.com/stevan/BreadBoard/issues

When submitting a bug or request, please include a test-file or a
patch to an existing test-file that illustrates the bug or desired
feature.

# COPYRIGHT AND LICENSE

This software is copyright (c) 2019, 2017, 2016, 2015, 2014, 2013, 2011, 2009 by Infinity Interactive.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.