Map services with init variables? #3347
Replies: 1 comment
|
Hey Mike — there isn’t a WireBox Two other wrinkles: Recommended pattern (this is what the DI usage guide shows for JWT, etc.) is a tiny factory whose // app/lib/EmailServiceFactory.cfc
component {
public any function init() {
variables.instance = new app.lib.EmailService(
dataSourceName = application.wo.get("dataSourceName")
);
return this;
}
public any function build() {
return variables.instance;
}
}// config/services.cfm
local.di = injector();
local.di.map("emailServiceFactory").to("app.lib.EmailServiceFactory").asSingleton();
// callers: service("emailServiceFactory").build()Your “pass |
Uh oh!
There was an error while loading. Please reload this page.
Is there a recommended way to pass in config settings (e.g. get("datasourcename") ) inside a service?
You can't just call get("datasourcename") inside the service it looks like. If I try to do something in an init function it seems to just find other services and auto wire it up. https://guides.wheels.dev/v4-0-0/digging-deeper/dependency-injection-usage/#auto-wiring-init-parameters
Can I also pass in my own variables to an init? Inside services.cfm I can dump a get variable.
writeDump(var=get("datasourcename"),abort=true,label="");
But I'm not sure if it's possible to pass anything in the service mapping
local.di = injector();
local.di.map("emailService").to("app.lib.emailService").asSingleton();
I see wirebox has something like initWith that might be the closest thing to what I'm thinking.
initwith: https://wirebox.ortusbooks.com/configuration/mapping-dsl/dependencies-dsl
I'm just exploring. This isn't super urgent. Or maybe it's not even a recommended approach.
As a workaround, I can still call a function inside my service and pass in a get variable. It's just maybe not as clean.
All reactions