[coldbox 3.6.0 - ACF10] Injecting Dependencies into a all cfscript, persistant models.

I have a model which is entirely cfscript.

I need to inject dependencies into the model and have tried just about everyway I can find in the docs, but without success.

The models are persistant.

What is the best way to make a dependency available in a persistant mode, all cfscript model?

The best I could find in the docs is this:

/**

  • Init

  • @queryHelper.inject coldbox:plugin:QueryHelper

*/

public function init(required queryHelper){

return this;

}

But I receive an error that “queryHelper is required, but was not passed in”.

Any suggestions on easiest way to make plugins and other dependencies available to my cfscript, peristant models?

Many Thanks

How are you creaing that model? Are you requesting it from WireBox, or creating it yourself with createObject() or the “new” keyword (bad)?

What you are attempting is constructor injection. My favorite way to inject dependencies is via mixin injection which would look like this:

component singleton {
property name=“queryHelper” inject=“coldbox:plugin:QueryHelper”;

}

Either way should work though. Also, are you using CF or Railo. Annotation-based injection can be a little tricky.

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com

Do you need them on init.

If not, then I would just do

property name=“queryHelper” inject=“coldbox:plugin:QueryHelper” persistent=“false”;

Curt

How are you creaing that model? Are you requesting it from WireBox, or creating it yourself with createObject() or the “new” keyword (bad)?

What you are attempting is constructor injection. My favorite way to inject dependencies is via mixin injection which would look like this:

component singleton {
property name=“queryHelper” inject=“coldbox:plugin:QueryHelper”;

}

Either way should work though. Also, are you using CF or Railo. Annotation-based injection can be a little tricky.

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com

Thanks Brad & Curt,

I’m creating the object with getModel() in the requestStartHandler with:

rc.org = getModel("Org").get(cbSession.getVar('adminOrgID'));

I would much rather use the annotations approach as well, but couldn’t get that working either. This is what I was attempting

property name="queryHelper" type="coldbox:plugin:QueryHelper" persistent="false" ;

However when I then reference queryHelper I get an error that “variable queryHelper is undefined”.

I haven’t delved into LogBox yet (still relatively new to coldbox), but will take a look as you suggested Brad.

Using CF10.

Thank you!

Remember than when you use cfproperty injection, the dependencies will not be available in your init() method. They are injected after init is run. Instead, create a method called “onDIComplete”. It will be called after injection has taken place.

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com

Thanks Brad… not sure I understand what you mean by this one.

I don’t need to use the injected property in my init. Infact my model doesn’t have one (should it have one??).

If I just want to use the dependency in a standard method, what do I need to do with the onDIComplete?

Could the fact that I am creating the object in the onRequestStart handler have something to do with it?

Thanks again Brad!

I actually didn’t know where you were getting the error at, so it was just a guess. Since you were trying setter injection it stood to reason that perhaps you were also using the dependencies in the init.

Can you share more information about what errors you get when trying mixin injection and what your code looks like that uses the injected values?

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com

I could be wrong here, but properties can’t be annotated like this, annotations are for components and functions only.

You know, reading back through your code, it just occurred to me that you need to switch “type” for “inject” on your property.

property name=“queryHelper” inject=“coldbox:plugin:QueryHelper”;

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com

Thanks Brad.

Here is the property syntax I currently have (I noticed that as well and have subsequently changed), but this still gives error:

property name="queryHelper" type="coldbox:plugin:QueryHelper" persistent="false" ;

I have also tried the below, among other variants, but same result.

property name="queryHelper" type="coldbox:plugin:QueryHelper" persistent="false" scope="variables" ;

The model is getting created in the onRequestStart handler with:

rc.org = entityNew("Org").get(cbSession.getVar('adminOrgID'));

The error I am getting is:
Error Type: Expression : [N/A]
Error Messages: Variable QUERYHELPER is undefined.

The full tag context is:

As Brad stated it should be

property name=“queryHelper” inject=“coldbox:plugin:QueryHelper” persistent=“false” ;

I haven’t done much with WireBox and CF’s ORM, but according to the docs:

There is no way to intercept new() or entityNew() or createObject() calls done via ColdFusion and there is no preNew interception point exposed by ColdFusion. So if you want ORM entity injection enabled for new entities, you will have to send them manually into wirebox for wiring or use our ORM Base Services:

wirebox.autowire( entity );

http://wiki.coldbox.org/wiki/WireBox-EntityInjection.cfm

I would recommend looking into the ORM Services that ColdBox provides. If you ask it for the new object, it will make sure it gets autowired.

http://wiki.coldbox.org/wiki/ORM:BaseORMService.cfm

Also, make sure you’ve configured your eventHandler in Application.cfc. (Covered in the first link)

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com

Thanks Brad!! I will read up and let you know how I go. Cheers!

OK…I worked it out (I think … I hope).

I had gone down the ActiveEntity Path. My models were extending coldbox.system.orm.hibernate.ActiveEntity.

From what i can tell, you either go Wirebox or Active Entity… not both… (I might be wrong, but that was the impression I got from the docs)… so I focused on ensuring I was accurately setup for Active Entity.

I hadn’t put the necassary settings in config/coldbox.cfc

orm = { injection = { enabled = true, include = "", exclude = "" } };

So I put this in… still no luck,

but when I changed my property annotation to (note use of inject instead of type):

property name="queryHelper" inject="coldbox:plugin:QueryHelper" persistent="false" ;

… it worked!!

The big question this leaves me asking is what is the difference between managing models with wirebox vs activeEntity (if my interpretation that they are mutually exclusive is correct, and which path should be taken when)… Looks like I might have to kick off a new project to find out.

Thanks for pointing me in the right direction with this Brad!!

Jason

I think I am a little confused by your question here, as long as you follow the ColdBox way to load entities WireBox will work just fine. WireBox and ActiveEntity are two different things, which is where I am getting a little confused by what you’re asking.

Thanks Andrew… all good… I understand what you are saying… the question is probably more for myself to work out as I continue to get my head around the different aspects of Coldbox and DI concepts in general… Thanks!

No problems Jason, just feel free to ask.