Title: An Introduction to Integrating ColdFusion, ColdSpring, and Flex 3
1An Introduction to Integrating ColdFusion,
ColdSpring, and Flex 3
- A Presentation to the Kansas City Adobe RIA User
Group - March 25, 2008
- Download this presentation at http//www.brucephil
lips.name/blog
2Goals
- Learn how to use ColdSpring to manage
dependencies between ColdFusion Components (CFCs) - Learn how to use ColdSpring to automatically
create CFCs that Flex can use
3What Do You Need To Already Know?
- How to create and use CFCs
- Service, Gateway, DAO, Bean CFCs
- A basic understanding of how to create a Flex
application that uses ColdFusion and remote
object services - mxRemoteObject component
4What Do You Need To Have?
- ColdFusion version 7.0.2 or higher
- ColdSpring 1.0 final release (see ref. 1)
- Flex Builder 2 or 3
5Example Projects
- Example projects
- Example1NoColdSpring, http//www.brucephillips.nam
e/flex/coldspringexample/example1nocoldspring.zip
- Example1, http//www.brucephillips.name/flex/colds
pringexample/example1.zip - Example2, http//www.brucephillips.name/flex/colds
pringexample/example2.zip - Microsoft Access or MySQL Database
- http//www.brucephillips.name/flex/coldspringexamp
le/data.zip
6Setup Example Projects
- Download the zipped project files
- Import into Flex Builder
- File Import Other General Archive File
- Download the zipped data file and unzip it. Use
justCSdb.sql to create a MySQL database or use
the justCSdb.mdb Microsoft Access database - Create a data source in ColdFusion administrator
named justCSdbMySQL that refers to the database
7ColdSpring
- Developed by Dave Ross (www.d-ross.org) and
several others - Manages the dependencies between CFCs
- Read an Introduction to ColdSpring and ColdSpring
and Dependency Injection for the beginner part 1
(ref. 3 and 4)
8CFCs Dependencies
- UserService CFC
- UserDAO CFC
UserService m_userDAO UserDAO getUserByID()
UserDAO read()
The UserService CFC is dependent upon the UserDAO
CFC. It must have an object of type UserDAO.
9Managing CFC Dependencies Without Using ColdSpring
- Hardwire the dependencies
- Show Code example (example1NoColdSpring)
- Within the UserService CFC create an object of
the UserDAO CFC - On large projects with lots of Service, Gateway,
DAO, Bean CFCs that are dependent on each other,
hardwiring these dependencies can be cumbersome
and time consuming to manage
10Get and Setup ColdSpring
- Download ColdSpring zipped file (ref. 1)
- Unzip it to your web root
- Should now have a ColdSpring folder under your
web root - C\ColdFusion8\wwwroot\coldspring
- Under the coldspring folder are an examples and a
docs folder
11Integrating ColdSpring and A ColdFusion
Application
- Add code to Application.cfm (or Application.cfc)
to create the ColdSpring BeanFactory object - A bean factory is a container that holds all of
your CFC objects - The ColdSpring BeanFactory object is stored in
application scope - See example1 Application.cfm
- See example1 Application.cfc
12Provide ColdSpring Your Applications
Configuration Parameters
- ColdSpring reads an XML file that describes the
CFCs, their dependencies, and any additional
properties for your application - This line in Application.cfm/cfc reads the
configuration XML file - ltcfset application.beanFactory.loadBeans(expandPat
h("services.xml"))/gt
13Services.xml Telling ColdSpring About Your CFCs
and Dependencies
- See example1 - services.xml
- CFCs are defined in the XML by the bean element
- The bean child element named property defines
dependencies - See bean definition with an id of userService
- For each property we need a comparable set method
in the CFC - See example1 - UserService CFC setUserDAO method
14Services.xml continued
- See the bean definition with an id of userDAO
- Can also pass values to a CFCs init method using
the constructor-arg child element of the bean
element - See init method for the UserDAO CFC and note the
argument named dsn
15Changing Services.xml or a CFC
- Append reloadApp1 to the index.cfm URL
- See example1 - Application.cfm
- See example1 Application.cfc (onRequestStart
function) - Forces the BeanFactory to be recreated and
ColdSpring to read the services.xml file
16Using CFCs Being Managed By ColdSpring
- When you need an object of a CFC being managed by
ColdSpring use the BeanFactorys getBean method - See example1 index.cfm
17CFCs Are Ignorant of ColdSpring
- The UserDAO and UserService CFCs are ignorant of
ColdSpring - ColdSpring makes has no requirements except that
your CFCs have an init method - Using an init method as a pseudo-constructor is a
ColdFusion best practice (ref. 11) - These CFCs could easily be used in an application
that isnt using ColdSpring to manage dependencies
18ColdSpring and ColdFusion Frameworks
- Most major ColdFusion Frameworks (Mach-II,
Model-Glue, FuseBox) include the ability to
integrate ColdSpring - Complex applications often use ColdSpring and a
framework together
19ColdSpring and Flex
- If ColdSpring is managing the dependencies
between CFCs how can we use those same CFCs with
Flex? - Cannot call the UserService CFC directly from
Flex because its dependencies have not been
resolved - See example2 UserService CFC - getUserByID
method - Call from Flex will fail because
variables.m_userDAO object doesnt exist - Need to connect Flex to the UserService CFC being
managed by ColdSpring
20ColdSpring and Remoting
- ColdSpring can automatically create a remote
façade CFC that we can connect to with Flex - The remote façade CFC will expose any CFCs being
managed by ColdSpring with their dependencies
resolved
21Creating A Remote Façade CFC
- Add a bean element to the Services.xml
configuration file - class attribute value should be RemoteFactoryBean
- See example2 services.xml
- Specify the target (the CFC to create a remote
façade for) - Specify the name of the remote CFC
- Specify where to write the remote CFC
- Specify the methods to expose
22Have ColdSpring Create The Remote Façade CFC
- Modify the Application.cfm so that when
ColdSpring finishes processing the configuration
XML file it will create the remote CFC object in
application scope - See example2 Application.cfm/cfc
- The first time the application runs ColdSpring
will create the remote façade CFC - In this example UserServiceRemote CFC
23Remote Façade CFC
- See example2 UserServiceRemote CFC (in model
folder) - Note the function getUserByID
- The remote façade CFC can be used by Flex or Ajax
applications
24Connect Flex to The Remote Façade CFC
- Use the mxRemoteObject component to connect Flex
to the Remote Façade CFC - See references 7, 8, and 9 for how to connect
Flex to ColdFusion CFCs - See example2 example2.mxml (in src folder)
- Note Flex is connecting to the UserServiceRemote
CFC - Should be able to run example2 Flex project,
click on the Test Connection button, and get
Bruce Phillips back from the ColdFusion CFC
25Cautions
- Make sure the remote façade CFC exists (has been
created by ColdSpring) before trying to connect
to it using Flex - Research how to integrate Flex into ColdFusion
applications that use a framework (Mach-II,
Model-Glue, FuseBox) combined with ColdSpring - How does the framework integrate ColdSpring and
can ColdSpring still create remote façade CFCs?
26Questions?
- This presentation is posted on my blog at
http//www.brucephillips.name/blog - My email address is Bruce_at_Phillips.name
27References
- ColdSpring Framework, http//www.coldspringframewo
rk.org/ - ColdFusion Developer's Journal Special
"Frameworks" Focus Issue ColdSpring
http//coldfusion.sys-con.com/read/176181.htm - The Blog of Andy Jarrett, ColdSpring and
Dependency Injection for the beginner part 1 - ColdSpring Online Documentation,
http//www.coldspringframework.org/docs
28References continued
- Remote Synthesis Blog, Objects and Composition -
Connecting To Flex - Developing With ColdSpring, ColdSpring and
Remoting - Flex 3 Remote Object Components,
http//livedocs.adobe.com/flex/3/html/data_intro_2
.html - Flex 3 Using RemoteObject Components,
http//livedocs.adobe.com/flex/3/html/data_access_
4.html
29References
- Using ColdFusion With Flex 2, http//download.macr
omedia.com/pub/documentation/en/flex/2/using_cf_wi
th_flex2.pdf - FuseBox and ColdSpring, http//corfield.org/blog/i
ndex.cfm/do/blog.entry/entry/Fusebox_5_and_ColdSpr
ing - ColdFusion MX Coding Guidelines - Good Practice,
http//livedocs.adobe.com/wtg/public/coding_standa
rds/goodpractice.html