Question about using getModel and Wirebox

206 views
Skip to first unread message

Douglas Trojanowski

unread,
Jul 9, 2015, 10:04:19 AM7/9/15
to col...@googlegroups.com
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


br...@bradwood.com

unread,
Jul 9, 2015, 11:46:42 AM7/9/15
to col...@googlegroups.com
Hi Douglas, welcome to ColdBox!
 
First, here's some light reading material.  Our ColdBox getting started guide (which covers some basic object injection:
 
And second, our Wirebox reference card that covers several of the ways to use WireBox:
 
 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: br...@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com 
 
 
--------- Original Message ---------
--
--
You received this message because you are subscribed to the Google Groups "ColdBox Platform" group.
For News, visit http://blog.coldbox.org
For Documentation, visit http://wiki.coldbox.org
For Bug Reports, visit https://ortussolutions.atlassian.net/browse/COLDBOX
---
You received this message because you are subscribed to the Google Groups "ColdBox Platform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to coldbox+u...@googlegroups.com.
To post to this group, send email to col...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/coldbox/a801bef9-1821-488c-ba2a-855be771af02%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Douglas Trojanowski

unread,
Jul 9, 2015, 1:36:08 PM7/9/15
to col...@googlegroups.com
Thank you my mapping i just abbreviated it doesn't live inside the webroot. Huge help thank you.
Reply all
Reply to author
Forward
0 new messages