LightWire and model integration

I have been experimenting (a lot) with LW and auto wirining in my
model and did not get it to work. I saw a couple of threads were
people had the same problem - Injecting dependencies with FW from one
object in to another object in the model. I tried everything
cfproperty, <cfproperty name="User" type="model" scope="instance" />,
and also set up model maping, tried beanFactory: <cfproperty
name="beanFactory" type="coldbox:plugin:beanFactory" />, tested
autowiring Transfer with <cfproperty name="Transfer" type="ocm"
scope="instance"> and GetModel(), but as mentioned in the excellent
documentation this will only work in handlers, plugins etc, as I
understand it - not in the model. I have read everything a couple of
times and now I am rereading it again. Maybe it´s possible with
ColdSpring?

I want to have the advantage of object caching, autowiring and DI
otherwise I could just createObject() where I need it in my object by
composition.

For now I just have skiped the intercommunication between my objects
and instead use autowiring in my handlers and when I need a Transfer
object in one of my bussiness objects I instantiate them with getModel
() and pass in the Transfer objects in the constructor.

I had to re-think and refactor my BO:s but overall they tend to be
more low coupled, encapsulated - more OO-ish, i think is the right
word.

Hey Goran, this might help...

http://groups.google.com/group/coldbox/msg/f7c49b0713cbca3e

I tried the following in my mode:

Created a BaseModel.cfc

<cfcomponent name="BaseModel" output="false">
  <cfproperty name="dsn" type="coldbox:datasource:myAlias" />
  <cfproperty name="beanFactory" type="coldbox:plugin:beanFactory"
scope="instance" />
<!--- Dependencies injected by COldBox Bean Injector --->
  <cffunction name="getModel" access="public" returntype="any">
  <cfargument name="model" type="any">
   <cfreturn instance.beanFactory.getModel
(argumentCollection=arguments) >
  </cffunction>
</cfcomponent>

And tried to use it in one of my event handler:

<cfset rc.testobj = getPlugin("ioc").getBean("BaseModel").getModel
("linkService") />

Then I got the error:

Element BEANFACTORY is undefined in a Java object of type class
[Ljava.lang.String;

Don´t know why..

I thougt it might be something wrong in my app so I created a new app
from the dashboard. Created the nessesary LightWire files and
settings. And tried this:

In your model object you can:

1.) Use cfproperty to inject the setting:

<cfproperty name="htmlBaseURL"
type="coldbox:setting:htmlBaseURL" scope="instance">

<cffunction name="test" access="public" returntype="any">
  <cfset var test = "">
  <cfset test = instance.htmlBaseURL>
  <cfreturn test>
  </cffunction>

And in my event handler:

<cfset rc.testobj = getPlugin("ioc").getBean("BaseModel").test() />

In my view:
<cfoutput>
#rc.testobj#
</cfoutput>

I reinit the framwork and bam:

Application Execution Exception
Error Type: Expression : [N/A]
Error Messages: Element HTMLBASEURL is undefined in INSTANCE.Tag
Context:
ID: ??
LINE: 16
Template: C:\www\minapp\model\BaseModel.cfc
ID: CF_TEMPLATEPROXY
LINE: 76
Template: C:\www\minapp\handlers\general.cfc
ID: CFINVOKE
LINE: 445
Template: C:\www\coldbox\system\controller.cfc
ID: CF_TEMPLATEPROXY
LINE: 163
Template: C:\www\coldbox\system\coldbox.cfc
ID: CF_UDFMETHOD
LINE: 51
Template: C:\www\minapp\Application.cfc

Still no progress..

Hi Goran,

Please read this guide of lightwire
http://ortus.svnrepository.com/coldbox/trac.cgi/wiki/cbLightwireGuide

You are trying to mixing of different concepts (Lightwire and Model
Integration)

For model integration you can do this
getModel("BaseModel").test() then this will inject required
dependencies.

Thanks
Sana

You can also use cfproperty injections alongside lightwire and
coldspring but you need to tell coldbox to wire them up also because
they are coming from a factory already. So what you are missing is the
autowire property on the cfcomponent tag.

Great! It was the autowire property that was missing.

So now I have this class in my model directory:

<cfcomponent name="BaseModel" output="false" autowire="true">
<!--- Dependencies injected by ColdBox Bean Injector --->
<cfproperty name="htmlBaseURL" type="coldbox:setting:htmlBaseURL" />

<cffunction name="test" access="public" returntype="any">
  <cfset var baseURL = "">
  <cfset baseURL = variables.htmlBaseURL>
  <cfreturn baseURL>
</cffunction>
</cfcomponent>

Wired in LightWire (/config/LightWire.cfc):
// BaseModel
    addSingleton("#cfcModelRoot#.model.BaseModel","BaseModel");
    addConstructorProperty("BaseModel","coldbox",getController() );

And in my default event handler (general.cfc) I have this method:

<cffunction name="index" access="public" returntype="void"
output="false">
    <cfargument name="Event" type="any">
    <!--- RC Reference --->
    <cfset var rc = event.getCollection()>
<!-- my test object --->
    <cfset rc.testobj = getPlugin("ioc").getBean("BaseModel").test() />

    <!--- Do Your Logic Here to prepare a view --->
    <cfset Event.setValue("welcomeMessage","Welcome to ColdBox!")>

    <!--- Set the View To Display, after Logic --->
    <cfset Event.setView("home")>
  </cffunction>

And it´s working, with LightWire and CB DI. Cool!

So now I can use: <cfproperty name="beanFactory"
type="coldbox:plugin:beanFactory"
scope="instance" />

And then use the beanFactory in my model when I need to have transfer
objects injected in to my model. If I remember the autowire property!
And if I do it right I only have one object (BaseModel) that knows
about the framework, and my event handlers will have less code.

Thanks for your patience, and help!

Ten Four!
(from a bridge at Junibacken, Stockholm)