[wirebox-2.0.0] Handler vs. Model Injection

21 views
Skip to first unread message

Ancient Programmer

unread,
Nov 7, 2015, 11:23:09 PM11/7/15
to ColdBox Platform
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?

/handlers/Person.cfc
property name="personService" inject="PersonService" scope="session";

vs.

/models/PersonService.cfc
component scope="session" accessors=true {...


Thank you!

br...@bradwood.com

unread,
Nov 8, 2015, 3:14:29 AM11/8/15
to col...@googlegroups.com
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.

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: br...@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com 
 
 
--------- Original Message ---------
--
--
You received this message because you are subscribed to the Google Groups "ColdBox Platform" group.
For News, visit http://blog.coldbox.org
For Documentation, visit http://wiki.coldbox.org
For Bug Reports, visit https://ortussolutions.atlassian.net/browse/COLDBOX
---
You received this message because you are subscribed to the Google Groups "ColdBox Platform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to coldbox+u...@googlegroups.com.
To post to this group, send email to col...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/coldbox/41ef09a4-cf39-4bd1-8fb7-021d4f54c50f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ancient Programmer

unread,
Nov 8, 2015, 11:08:38 AM11/8/15
to ColdBox Platform
Hi Brad,

Thanks so much for the explanation. I understand now.
Reply all
Reply to author
Forward
0 new messages