Hey all. Been struggling for hours trying to get D...
# box-products
j
Hey all. Been struggling for hours trying to get DI working via a cfc property with WireBox. Working in a legacy CFML application, non-coldbox. The CFC's I'd like to inject live in a
modules
folder (which for posterity is a mapped directory in
.cfconfig
for
/modules
, and lives in the same folder as
Application.cfc
) Have the following WireBox initialization in
Application.cfc
Copy code
function onApplicationStart() {
  application.wirebox = createObject("component","wirebox.system.ioc.Injector").init();
  application.wirebox.getBinder().mapDirectory("modules").noInit().asSingleton(); // Attempt 1
  application.wirebox.getBinder().scanLocations(["modules"]); // Attempt 2
}
Here's where the error comes in: I have a
UserService.cfc
component in which I am trying to inject
UserData.cfc
Copy code
component accessors="true" singleton {
 
  property name="UserData" inject="UserData";
 
  function init(){
    return this;
  }
 
   function getUser(userID) {
    return UserData.getUserByID(userID)
  }
}
But I get
variable UserData does not exist
. Seems the injection isn't happening. I was under the impression that
mapDirectory
or
scanLocations
would automatically initialize all the CFC's for WIreBox to do it's thang. Am I missing some configuration item here?
b
@Jason Ryan are you creating userservice with wirebox though?
You can't use createIbject() or new foo() any longer
j
Ohh I am using the
new
syntax
b
Otherwise wirebox never has the chance to wire the CFCs
It's in the object creation where wirebox processes injections
When you use new, you leave wirebox out entirely
j
I'm just calling this in a .cfm template
Do I need this?
application.wirebox.getInstance("UserService");
b
Yes
Also, just making sure, ALL of your cfcs are singletons?
j
No they are not
Just these two
b
Then don't use the asSingleton() dsl on the whole folder
Add the "singleton" notation to those components
j
Ah ok, I found that on a getting started guide. Makes sense!
wirtebox
.
getInstance
is just what I needed, working great now. Thanks!
👍 1