[wirebox-2.0.0] Handler vs. Model Injection

I’m developing an app using ColdBox 4.1. I have questions regarding injection using Wirebox.

What is the difference if I did scoping in handler vs. model?


property name=“personService” inject=“PersonService” scope=“session”;


component scope=“session” accessors=true {…

Thank you!

The first example is not valid. It may not error, but it won’t do what you want. You’re confusing the persistence scope of your user service and injection target where the handler will get access to it.

Firstly, if you want a model to live for the user’s session, put that in the model itself. The scope in the property tag simple says where in the handler the user service will be injected.

Second, don’t ever inject a session-scoped model into a singleton like a handler. That is known as scope-widening injection. The injection will only happen once the first time the handler is built, and all subsequent requests will be sharing the same model which is not what you want.

When a CFC with a long scope needs access to a shorter-scoped CFC that changes on a request or session basis, it needs to ask WireBox for a reference every time and only keep it in the function local scope.

So here’s the model:
component scope=“session” {}

And here is the handler:
component {
function foo() {
var thisSessionsModel = getInstance( 'PersonService ’ );

Every time our handler runs, it asks WireBox to fish up the correct session-scoped PersonService model for us.



ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com

Hi Brad,

Thanks so much for the explanation. I understand now.