1

I am trying to access Coldbox getSetting within a model.

In the docs, and on Coldbox Google Group posts, it clearly states

The model is a completely isolated layer of your application and has no access to anything ColdBox unless you inject dependencies into them. We suggest you look at our WireBox dependency injection so you can see how to inject the models with what they need.

They point to this doc: http://wiki.coldbox.org/wiki/WireBox.cfm#The_WireBox_Injector

But other than the somewhat confusing doc and Google Group posts repeating that quote above, there is no real good example on how to do it.

I have attempted property injection at the top of my model:

<cfcomponent displayname="myComponent" output="false">
    <cfproperty name="mySetting" inject="coldbox:setting:mySetting" />
       <cffunction name="myFunction" output="false" hint="index"> 
          <cfset value = getProperty('mySetting') />
           ...

This returns Error Messages: Variable GETPROPERTY is undefined.

I also attempted an argument injection in the function of my model, but I knew that wouldn't work.

<cffunction name="myFunction" output="false" hint="index">
  <cfargument name="mySetting" inject="coldbox:setting:mySetting">

Can anyone show me how to pass getSetting to a model via wirebox injection, or really any method?

How do you inject a dependency in the model of Coldbox?

2rs2ts
  • 10,662
  • 10
  • 51
  • 95
Jay Rizzi
  • 4,196
  • 5
  • 42
  • 71

2 Answers2

2

Your injection looks fine. The issue is that you are trying to use "getProperty()" which is not defined in your component. It comes from the framework supertype and only exists in frameworks objects like handlers, etc. Property injection places the reference to the injected object directly into the "variables" scope by default, so you just access it as variables.mySetting.

You can also control the scope that the property is injected into with the scope attribute:

http://wiki.coldbox.org/wiki/WireBox.cfm#Property_Annotation

Argument injection DOES work, but only for constructors (init) since they are called automatically by the DI engine.

Also, this ref card may be a bit simpler to read through than the full WireBox docs, but it obviously doesn't cover as much information: https://github.com/ColdBox/cbox-refcards/raw/master/WireBox/WireBox-Refcard.pdf

Disclaimer: I am part of Team ColdBox.

Brad Wood
  • 3,863
  • 16
  • 23
  • I did try using variables.mySetting, it threw an error variables is undefined – Jay Rizzi Apr 11 '14 at 02:22
  • Well, it did all of a sudden start working for variables.mySetting, maybe a cache issue...thank you for the card as well, will come in handy – Jay Rizzi Apr 11 '14 at 15:08
  • Glad to hear it's working. Remember, you need to reinit the framework even if the object receiving the injection is a transient since the metadata for the mapping is cached in WireBox for performance. – Brad Wood Apr 11 '14 at 18:00
  • last question, if i was to place it in the wirebox config instead of property injection on the model, where would i place it? here's a gist of the wirebox config, its just default right now https://gist.github.com/Jrizzi1/10488828 – Jay Rizzi Apr 11 '14 at 18:09
  • Explicit mappings, if you choose to use them, go where the "// Map Bindings below" comment is. See the sample WireBox binder config I left in my other comment. – Brad Wood Apr 11 '14 at 18:33
0

In /config/WireBox.cfc, you set up aliases for your model objects:

map('KungFooDAO').to('model.path.to.KungFooDAO');

You can inject a ColdBox setting as a property right here:

map('KungFooDAO').to('model.path.to.KungFooDAO')
    .property(name='myDsn', dsl='coldbox:datasource:myDsn');

Now, inside of KungFooDAO.cfc, you can reference variables.myDsn, which has the value of the ColdBox setting.

Alternately, you can leave off the .property() call in the WireBox config and add a CFPROPERTY inside of KungFooDAO.cfc like this:

<cfproperty name="myDsn" inject="coldbox:datasource:myDsn" />

These examples are taken from live code (DSN names changed to protect the innocent).

What does your WireBox entry for your model CFC look like?

Adrian J. Moreno
  • 14,350
  • 1
  • 37
  • 44
  • I hadnt placed one yet! I'll try this first thing in the morning. P.S. i admire your work, use several of your plugins from github – Jay Rizzi Apr 10 '14 at 21:31
  • where do i place the map in the function configure(){wireBox = {}} ? – Jay Rizzi Apr 11 '14 at 13:27
  • The mapping DSL goes inside the configure() method, but is outside the "wirebox" struct declaration. See this example: https://github.com/ColdBox/coldbox-samples/blob/master/applications/ColdBoxReader/config/WireBox.cfc Of course, keep in mind the mapping DSL is an alternative to just putting the annotations and properties directly in your CFC. Two means to the same end. – Brad Wood Apr 11 '14 at 18:30