Coldbox getSetting in a model wirebox injection

1.7k views Asked by At

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?

2

There are 2 answers

5
Brad Wood On BEST ANSWER

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.

3
Adrian J. Moreno On

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?