[ColdBox] Accessing variable injected as property in handler

Hi, I am having a small problem with accessing a value that is being injected into a handler:

So I user.cfc as a handler into which I am injecting the following, which is simply to get access to a DSN, which has been added to the settings structure in Coldbox.cfc

property name=“appDSN” inject=“coldbox:configSettings:appDSN”;

I can access this value without issues within the handler actions, for example:

function index( event, rc, prc ){
prc.config = appDSN;
//database stuff here.
}

But I cannot access it outside of the handler actions, for example:

dao = new database.dao( dsn = appDSN );

This is before any handler actions are defined, so that all handlers have access to this dao object.

I get this error message:

Type: expression
Messages: variable [APPDSN] doesn’t exist

Its probably something very simple that I am missing. Any pointers would be appreciated.

Thanks.
Dee

I’m not exactly clear on what you mean by “outside of a handler”. Where does the code creating the dao live?

dao = new database.dao( dsn = appDSN );

So, basically don’t do that, ever. createobject() and the “new” keyword are not to be used. Always leverage Wirebox to create any CFC instances, and then simply inject the DSN with a cfproperty the same way you did in your handler!

OK, I’ll ditch the new and createObject() method.

What I have done now is to inject using the following:

property name=“dao” inject=“database.dao”;

However, in order to create this CFC instance, I need to pass it an initation variable which is dsn = “databasename” otherwise it throws an error.

This is a third party CFC, so I can’t really play with its code. How can I pass this variable using the wirebox / property injection method?

Sorry, I am new to ColdBox and just getting to grips.

Thanks.

OK, I managed to sort it…

The plugin I am using is here:

https://forgebox.io/view/dao

the DSN can be specified as an application variable in Application.cfc and then instantiated through wirebox with:

property name=“dao” inject=“database.dao”;

Glad you got it working. To answer your question though-- the proper way to map a CFC in wirebox that requires constructor args that you can’t change (ewww) is to switch to an explicit mapping in your /config/WireBox.cfc where you tell WireBox what the CFC is, how it needs to be constructed, and how you’d like to refer to it.

map(“DAO”)
.to(“database.dao”)
.into(SCOPES.SINGLETON)
.initArg( name=“dsn”, dsl=“coldbox:setting:myDSN” );

So now, you just as wirebox to inject “dao” which is the name of your mapping. You don’t have to worry about the full CFC path as that detail is abstracted away inside of this new custom mapping we’ve created. You may or may not want the dao to be a singleton. I’m not familiar with that library but I would assume it can be a singleton. I’m also assuming the name of the constructor arg is “dsn” and I’m making the assumption you have the name of the datasource stored in a ColdBox setting called “myDSN”. Adjust that code as necessary. Or if you just wanted to hard code it, you could do

.initArg( name=“dsn”, value=“actualDSNName” );

Please familiarize yourself with the Wirebox docs which explain this and more.

https://wirebox.ortusbooks.com/configuration/mapping-dsl/dependencies-dsl