DI/1 constants confusion

179 views
Skip to first unread message

Geoff Parkhurst

unread,
May 23, 2013, 7:31:50 AM5/23/13
to framew...@googlegroups.com
Hello

I'm having difficulty with the constants struct passed to di1 - not
sure if I've misunderstood what it's for...

Given this CFC:

<cfcomponent>
<cffunction name="init" access="public" output="false" returntype="test">
<cfargument name="foo" type="string" required="true">
<cfset variables.foo = arguments.foo />
<cfreturn this />
</cffunction>

<cffunction name="getFoo" access="public" output="false" returntype="string">
<cfreturn structkeyexists(variables,"foo") ? variables.foo : "UNDEFINED">
</cffunction>
</cfcomponent>

I can invoke it and call the init method - all good:

<cfset a=createobject("component","test").init(foo="bar")>
<cfoutput>#a.getFoo()#</cfoutput>

But doing the same through DI1, I get "bean not found: foo; while
resolving constructor arguments for test"

I'm using this code:

<cfset bf = CreateObject("component", "ioc").init(folders = ".",
constants={foo="bar"}) />
<cfdump var="#bf.getBean("test")#">

It seems DI1 thinks the "foo" argument should be a bean. I was kind of
hoping it could be a big 'ol name-value struct (which at the moment,
we pull from a .json file).

Do I need to wrap that name / value struct into a config.cfc bean with
its own getters / setters?

(I've seen something to that effect in earlier posts to this group but
couldn't work out from that thread if it's the 'right' thing to do -
sounded more like a "what _I_ did was ...." type reply!)

Many thanks

Geoff
Message has been deleted

David Schaller

unread,
May 23, 2013, 9:33:44 AM5/23/13
to framew...@googlegroups.com
Geoff,
In my case I am passing a 'config' struct which contains the 'constants' struct.
Perhaps like this:
<cfset bf = CreateObject("component", "ioc").init(folders = ".", config = { constants={foo="bar"} }) /> 

David

Geoff Parkhurst

unread,
May 23, 2013, 9:47:27 AM5/23/13
to framew...@googlegroups.com
d'oh. Thanks David - good spot. Works a treat.

Gregory Thrush

unread,
Apr 21, 2014, 9:53:07 PM4/21/14
to framew...@googlegroups.com
Hello,

The ability to pass constant values when setting up the bean factory is nice. When I declare my database credentials as hard coded constants that then get passed as arguments to the init() of my datasource cfc, it works like a charm. 

 function setupApplication() {
    setupEnvironment(getEnvironment());
    // writeDump(var=variables.framework.dsn, abort=false label= "variables.framework.dsn");
    var bf = new framework.ioc( folders ="model", config = {constants = {dsn = "theDsn", userName="theUserName", dbpassword="thePassword"} });
        setBeanFactory( bf );
    }

As you can see from the above code, I also want to use FW1's Environment Control to give me the correct credentials for a given application environment . I'd tried to set the constants like this:

var bf = new framework.ioc( folders ="model", config = {constants = {dsn = variables.framework.dsn, userName= variables.framework.userName, dbpassword= variables.framework.dbpassword} });

CF doesn't like this.  It throws the error  'Element FRAMEWORK.DSN is undefined in VARIABLES'.  When I dump the variables.framework struct I see that variables.framework.dsn (and the other two db credential variables) exist.  Is it possible to set constants within framework.ioc() in this manner?

Thanks,
Greg

Sean Corfield

unread,
Apr 21, 2014, 10:37:24 PM4/21/14
to framew...@googlegroups.com
First off: you don't need to call setupEnvironment() - FW/1 does that for you.

Second: show us your variables.framework structure in Application.cfc.

Third: show us your getEnvironment() function (in Application.cfc).

Fourth: if you have defined your own setupEnvironment() function (in Application.cfc), show us that.

As a general guide, you get much better help if you provide all the relevant information when you post a question... which should generally include: which CFML engine are you using, which version, which O/S, which exact version of FW/1 are you using etc.

Sean
signature.asc

Gregory Thrush

unread,
Apr 22, 2014, 1:46:50 PM4/22/14
to framew...@googlegroups.com
My apologies for not including enough code and information. Here's it is:

in application.cfc

variables.framework = {
unhandledPaths = "/mxunit",
suppressImplicitService = true,
suppressServiceQueue = true
};
   
variables.framework.environments = {
      dev = { reloadApplicationOnEveryRequest = true, trace = true, dsn="theDsn",  username = "theUserName",  dbpassword="thePassword" },
      dev_qa = { reloadApplicationOnEveryRequest = false, trace = false, dsn="theDsn",  username = "theUserName",  dbpassword="thePassword"  },
      prod = { reloadApplicationOnEveryRequest = false, trace = false, dsn="theDsn",  username = "theUserName",  dbpassword="thePassword" }
};
  
function setupApplication() {
getEnvironment();
// writeDump(var=variables.framework, abort=false label= "variables.framework");
var bf = new framework.ioc( folders ="model", config = {constants = {dsn = variables.framework.dsn, userName= variables.framework.userName, dbpassword= variables.framework.dbpassword} });
setBeanFactory( bf );
}

public function getEnvironment() {
if ( findNoCase( "web", CGI.SERVER_NAME ) ) return "prod";
if ( findNoCase( "dev", CGI.SERVER_NAME ) ) return "dev";
else return "dev_qa";
}

I used FW1's setupEnvironment(). 

I'm running FW/1 v 2.5RC1, ACF 9.x.


Regards,
Greg
Reply all
Reply to author
Forward
0 new messages