Mojolicious::Plugin::ServiceContainer - A Dependency Injection Container for Mojolicious.
For a regular Mojolicious application, you can load this plugin using the plugin method:
$self->plugin( 'ServiceContainer', {} );
For a Mojolicious::Lite application, you can use the plugin directive:
plugin 'ServiceContainer' => {};
Mojolicious::Plugin::ServiceContainer is a minimal Dependency Injection Container implementation for Mojolicious.
A service, for the purposes of this plugin, is defined as a class (any module that inherits from Mojo::Base),
which is responsible for performing a single role within your application. A service class must be
instantiable by simply doing a MyService->new call. Database connections, Email senders, HTTP clients can be
considered as services.
A service object is an instance of the service class. An assumption that is taken here is that a single service object for a given service is sufficient for the runtime of the application. In a future version, the API might permit the creation of more service objects for the same service.
The service definitions are loaded along with the plugin:
plugin 'ServiceContainer' => {
google_auth => {
class => 'MyGoogleAuthService',
args => {
client_id => 'xxxx.xxxx.xxxx.xxxx',
client_secret => 'yyyy.yyyy.yyyy.yyyy',
ua => '$ua',
log => '$log'
}
},
mongo => {
class => 'Mango',
args => [
'mongodb://localhost'
]
},
ua => {
helper => 'ua'
},
log => {
helper => 'log'
}
}
Each service definition may have one or more of the following keys:
-
classstring: Name of the class (i.e. module) that the service is referring to.Note: If your class is
Mojolicious, you will be passed a reference to the running application and not a new instance of theMojoliciousclass like other services. This is a way by which you can use the application helpers from within your service. -
helperoptional string: Name of a Mojolicious helper method that behaves like a factory and will return a service object when called. The helper will be called in the context of the Mojolicious application object. You can use any relevant default helper or a custom one. The assumption here is that the helper will return the same singleton everytime but this may or may not be the case depending on the helper implementation. -
argsoptional arrayref or hashref: The dependencies of your service. Static values will be passed as is to the constructor of the service. Dependent services are referred to by their names prefixed by the$sign. Eg.$mongo. Dependent services are first resolved (i.e. their own dependencies are resolved) before they are injected into the original service's constructor.
As the service definitions are listed in the configuration file, they are immutable during the runtime of your application.
my $authService = $c->service( 'google_auth' );
Inside any of your controllers, you can inject a service object by using the service helper. The only
argument passed is the name of the service as given in the service definition.
Mojolicious::Plugin::ServiceContainer inherits all methods from Mojolicious::Plugin.
The "MIT" License
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
Amarnath Ravikumar amar@semantics3.com