Question about using getModel and Wirebox

Started playing around with Coldbox and I wanted to make sure I understood everything before I dig way deep into an application.

My wirebox mapping

`
map( “DocBean” ).to( “DocBean” );
map( “DocManager” ).to( “DocManager” ).asSingleton();
map( “DocDao” ).to( “DocDao” ).asSingleton();

`

What i want to do try to achieve is

DocBean i use as setter and getter.
DocManager receives DocBean and does an action
DocDao contains all the stored procs.

So here is how I am using it in my handler.

`
var docBean = populateModel(“DocBean”);

prc.qResults = getModel(“DocManager”).searchDocuments(docBean=docBean);
event.setView(view=“doc/results”,nolayout=true);

`

my DocManager

`
component
{
public query function searchDocuments(required DocBean docBean){
var results = application.wirebox.getInstance(“DocDao”).FLsearchDocuments(docBean=docBean);
return results;
}
}

`

my DocDao is a simple function with stored proc that returns query results.

Now is this the correct way in a sense that I will not have a scoping issue.

If Bob and Mary both hit same request they both populate DocBean with their own form request and go search documents they will get back the their own correct results.?

Also is there a prettier way in DocManger to do application.wirebox.getInstance since get getModel is not available?

Thanks

Hi Douglas, welcome to ColdBox!

First, here’s some light reading material. Our ColdBox getting started guide (which covers some basic object injection:
http://coldbox.ortusbooks.com/content/getting_started_guide/index.html

And second, our Wirebox reference card that covers several of the ways to use WireBox:
https://github.com/ColdBox/cbox-refcards/raw/master/WireBox/WireBox-Refcard.pdf

map(“DocBean”).to(“DocBean”);
map(“DocManager”).to(“DocManager”).asSingleton();
map(“DocDao”).to(“DocDao”).asSingleton();

Are the CFCs in the root of your site? I would have expected path.to.DocBean, etc. The easiest away is to just drop them in you “models” folder. Then you don’t actually need to map them (not that it’s hurting anything other than just being extra typing). When you run getInstance( ‘foo’ ), the first place that is looked is models.foo.

Also, another way to specify the singleton bit is to simply put the “singleton” annotation in the actual component:

component singleton {}

There, now only one instance of the component will be created!

getModel(“DocManager”)

getModel() is an deprecated method from the olden BeanFactory plugin days. Please use getInstance() instead.

Now that said— as a general rule don’t use getInstance() to get singletons. I mean, you can-- but it’s a waste of time. Instead just inject a hard reference to the singleton in your CFC so it gets injected once when the CFC is created and never again.

component {
property name=‘DocManager’ inject=‘DocManager’
}

Now, that CFC will automatically have a reference to DocManager in the variables scope. ONLY do this for singletons (or more technically, objects with a longer scope than the CFC they’re being injected into. For transients you want a fresh, threadsafe object each time.

var results = application.wirebox.getInstance(“DocDao”).FLsearchDocuments(docBean=docBean);

No need for this, simply use the injection DSL I showed above and Wirebox will automatically inject the instance of DocDao when your DocManager is created. Handlers extend the framework supertype so they get the getInstance() method automatically, but models are “outside” the framework so no automatic methods for them.

Now is this the correct way in a sense that I will not have a scoping issue.

Well, “results” is local to that method call so it is safe to contain data for a single user’s request. So yes, your code is thread safe. Here’s the component using injection:

component singleton {
property name=‘DocDao’ inject’DocDao’;

public query function searchDocuments(required DocBean docBean){
var results = DocDao.FLsearchDocuments(docBean=docBean);
return results;
}
}

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

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

Thank you my mapping i just abbreviated it doesn’t live inside the webroot. Huge help thank you.