I have the following
main.cfc [singleton handler] → service.cfc [request coped model] → settings.cfc [session scoped model]
When I start my coldbox application for teh first time, and I dump out the entire session, I can’t find any reference to the settings model there.
When however, I load that same page again, the wirebox-injected settings model is present in the session scope.
In my main handler, I have an injection via cfproperty for the service; in the service I have a cfproperty injection for the settings model.
Am I wrong in thinking it would get injected in the session scope on my first request?
Is there any quick way to test why this isn’t being injected on my first request?
Can I inject it directly in my onsessionstart event, or is that bad practice?
What you are showing will cause scope widening injection. Meaning that you are creating memory leaks and bad references since you are injecting objects that are volatile. The request scoped and session scoped models should NOT be injected but requested or you can use our Provider pattern for this.
The reason is that those scopes will expire and your main.cfc references will be stale. You can read more about this in our WireBox page.
With that said, I would suggest either using providers or NOT injecting but requesting the objects from wirebox.
Yes, that would cause that affect. To request the items every time, inject WireBox into your handler and ask it for the transient any time you need it.
function index( event, rc, prc ) {
shortLivedObjectProvider.doSomething();
}
}
This will not inject the real object, but a place holder object which provides it when needed. The provider uses onMissingMethod() to proxy method calls at run time. The provider also has a method called “get()” which will get the real object for you.
At first I had skipped reading the details of wirebox, I had planned to do it later on, since it realy is a lot to digest. But man, it seems you can do a lot of wonderfull stuff with all that knowledge, so it really payed off to actually push through and read it all.
Even though I only needed to modify a few small details, a lot changed in how my dependencies are being treated, I like that.
Seems I will be playing around with this some more…