More Bread::Board
Typemaps and Dependency Inference
Most of the classes in your application will probably only need to be instantiated in one way. You probably won't have multiple instances of a model class, for instance. This means that when you mention the model class in one of your controller classes, it's unambiguous what you're actually asking for - there's no reason that you should need to explicitly list it as a dependency, since it's already listed as the type constraint in the controller. Bread::Board understands this, and can be used to automatically introspect the dependency list for your classes via the attributes that they declare.
Given an application like this:
1: | package MyApp::Model; |
we can instead tell Bread::Board to infer the dependencies for the controller service:
1: | has controller => ( |
This looks through all of the required attributes in MyApp::Controller, finds the ones with type constraints that correspond to classes, and looks for services in the OX application whose type constraints correspond to those classes. Any services that it finds are automatically added to the list of dependencies that the service explicitly specifies (if any). This can't entirely replace manually specifying dependencies, but it can greatly simplify them.
Lifecycles
By default, any time a service is resolved, a new instance is created. This is generally what you want (to avoid inadvertently leaking data between requests), but sometimes persisting data is necessary. For instance, you probably don't want to reconnect to the database on every single request, if performance is important for your application. In this case, you can specify a different lifecycle for a particular service:
1: | has dsn => ( |
Here, the dbh
service will be instantiated the first time that it is requested, but then every subsequent time, it will return the same instance. In this example, every time the model
service is resolved, a new MyApp::Model instance will be created, but each of those instances will use the same dbh
.
In addition to the Singleton
lifecycle, you can also specify a Request
lifecycle. This will act like Singleton
, except that the cached value will be cleared between requests.