<cffunction name="setupApplication" access="public" returntype="void" output="false">
<cfset application.dsn = "BlogJason" />
<cfset var blogService = variables.getService("BlogService") />
<cfset var blogGateway = createObject("component", "model.blog.BlogGateway").init(application.dsn) />
<cfset blogService.setBlogGateway(blogGateway) />
</cffunction>
The code in Framework 1 that is causing me trouble starts at line 1580: https://github.com/seancorfield/fw1/blob/master/org/corfield/framework.cfc#L1580
if ( isReload ) {frameworkCache = { };frameworkCache.lastReload = now();frameworkCache.fileExists = { };frameworkCache.controllers = { };frameworkCache.services = { };application[variables.framework.applicationKey].cache = frameworkCache;application[variables.framework.applicationKey].subsystems = { };}This happens immediately *after* setUpApplication() is called. That means that my service into which I am setting my blogGateway is wiped out and recreated. So any attempts to use that gateway within the object result in errors.Shouldn't I be able to do this in FW/1? Should I be able to modify services or any other objects in setUpApplication()? Or should I be doing it somewhere else.NOTE: No doubt someone will tell me I should be using some sort of DI to inject my gateway into my service. But this is for a class I am teaching to students who are new to web development. Teaching them an MVC framework is going to be confusing enough without having to teach them all about DI as well, so I would like to be able to build the app this way.Thanks,Jason
--
FW/1 on RIAForge: http://fw1.riaforge.org/
FW/1 on github: http://github.com/seancorfield/fw1
FW/1 on Google Groups: http://groups.google.com/group/framework-one
<cffunction name="default">
<cfset variables.fw.service("BlogService.getEntries", "posts") />
</cffunction>
The error I get is: Its possible that a method called on a Java object created by CreateObject returned null.
Value must be initialized before use.
If I modify FW/1 to move the call to setUpApplication() to *after* that section, it works fine.
Jason
I've just skimmed, but it seems like maybe you're expecting the service method to be fired right away. However, fw.service () merely queues the service call, which would then execute _after_ the controller method(s) finish.
Have you tried getting the service from the bean factory and calling it directly?
Sent from my Samsung Galaxy SII
<cffunction name="setupApplication" access="public" returntype="void" output="false">
<cfset application.dsn = "BlogJason" />
<cfset var blogService = variables.getService("BlogService") />
<cfset var blogGateway = createObject("component", "model.blog.BlogGateway").init(application.dsn) />
<cfset blogService.setBlogGateway(blogGateway) />
</cffunction>
This happens immediately *after* setUpApplication() is called. That means that my service into which I am setting my blogGateway is wiped out and recreated. So any attempts to use that gateway within the object result in errors.
NOTE: No doubt someone will tell me I should be using some sort of DI to inject my gateway into my service. But this is for a class I am teaching to students who are new to web development. Teaching them an MVC framework is going to be confusing enough without having to teach them all about DI as well, so I would like to be able to build the app this way.
Since I suspect you only need the service in the blog controller
(yes?), the simplest solution is to create the gateway and then the
service in the init() of your controller.
I'd question using application.dsn since it's a global variable and
you probably want to avoid such practices in a course teaching MVC but
then your choices are something like:
* Application.cfc: this.dsn = "foo"; controllers/blog.cfc: init( fw )
{ ... variables.fw.dsn ... }
* Application.cfc: variables.framework = { ... dsn = "foo", ... };
controllers/blog.cfc: init( fw ) { ... variables.fw.getConfig().dsn
... }
Or just use the DSN name directly when initializing the gateway in the
blog controller init() - and point out that as the program gets more
complex, you'd want to move all of that initialization to a DI
framework.
I don't think there's any way of escaping a DI framework at some point
in the process unless you're really only teaching them toy apps?
--
Sean A Corfield -- (904) 302-SEAN
An Architect's View -- http://corfield.org/
World Singles, LLC. -- http://worldsingles.com/
"Perfection is the enemy of the good."
-- Gustave Flaubert, French realist novelist (1821-1880)
You're doing good work! Keep it up!