Imagine something along the lines of:
get( => 'hello' => sub ($app) {
my $hello_service = $app->get_instance_of('hello_service');
my $message = $hello_service->greet('world');
template 'hello', {message => $message};
});
hello_service it a bit complex to initialize, I would not want to have to new it up on every request. I also don't want it to store it in package variable. Is there something in Dancer2 or some other trick that could allow me injecting services like that?
I like to initialize my service style classes with Bread::Board then expose them globally through a namespace like
MyApp::Service, something like:There may be another way to do this, but this is the way I've always done it for
Mojolicious,Dancer, etc. I know you mentioned not wanting to do it in a package variable (sorry).