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.

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.

This function constructs and returns an instance of 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.

In many cases, subclassing 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.

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.

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 or have defined a custom injection service. If you specify a block, block injection will be performed using Bread::Board::BlockInjection. If neither of these is present, constructor injection will be used with Bread::Board::ConstructorInjection (and you must provide the class option).

The dependencies parameter takes a hashref of dependency names mapped to 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:

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

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

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

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

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 '+'.

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:

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:

This helper allows for the creation of service aliases, 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, so an alias can alias an alias.