ColdSpring and Remote Facades

ColdSpring is just indispensable when developing any size app but as a part of its Inversion of Control, or IoC it does a lot more. When developing a project with a framework I usually have a need to have remote access to my model. One of the usual methods is to still go through the framework itself. The reason is that you've got ColdSpring (in most cases) handling all the injections and its just easier to keep it one place. For me I prefer to use a single service layer component which can access the model and this is where CS fits in.

CS can create remote facades which exposes any remote methods(access="remote") you have in a component.

What I am going to explain over a couple of posts is a technique that I am looking at implement in project thats slowly being worked on

I am assuming if you've made it this far you know CS so I'm going to jump straight in. If you don't, I know a good place to start. First of the directory structure for this guide.

  • web root
    • remote_cs
      • com
        • users
      • config

In the config folder we're going to place our ColdSpring.xml configuration file which at the moment looks like this with reference to one bean:

In the users folder add userGateway.cfc which is a gateway to the db. Generally in here you'd have you queries to get data and its these methods that we need to access remotly. For now our component is really simple and looks like this:

Now we're going to set up CS in the application scope via the Application.cfc. This part usually gets hidden in the frameworks and is done for you. If you want a more in-depth explanation on what I am about to bastardise :) check out the docs. Add the following to onApplication(). If you are using Application.cfm then just place the code at the top of the file

// ColdSpring Bean Factory // LOAD CS Bean factory into the Application scopeapplication.beanFactory = createObject('component', 'coldspring.beans.defaultXMLbeanfactory').init(); // Supplying the BeanFactory with our bean definitionsapplication.beanFactory.loadBeansFromXMLFile(expandPath('/remote_cs/config/coldspring.xml'));

Up to now it has been a basic CS setup. Next we're going to create our service component. Create a component called userServices.cfc in /remote_cs/com/users/ (the same folder as the gateway) which will look like this

Next add a bean for this to the CS config file i.e.

Our service cfc is made up of two methods. One, the init() which takes the userGateway.cfc as an argument and getUserList() which is our remote methods that will get called. Now the bit we are interested in, creating a remote proxy of userServices.cfc.

The overall approach is this:
  • Define your components normally as <bean/>'s
  • Create a new <bean/> using the coldspring.aop.framework.RemoteFactoryBean class.
    • In the RemoteFactoryBean's definition, we must define the following properties:
      • target, the actual <bean/> we are creating a remote interface cfc for
      • serviceName, the name of the resulting remote inteface cfc
      • absolutePath, the filesystem location where the remote interface cfc should be placed or relativePath, a path relative from your webroot
      • remoteMethodNames, a matching pattern for which methods in our target component we want to remote proxy.
* from the docs

Time to update our coldspring.xml configuration file adding the following bean definition. Take a look at the bean using the quote above for a reference point. Most of it is self explanatory.

application beanFactory rUserServices /remote_cs *

In order to create the remote component we need to call getBean({bean id}) on the BeanFactory and ColdSpring. We need to update our onApplicationStart() for this. We need to add the following after supplying the bean definition file

// Create our Remote beanapplication.beanFactory.getBean('userServices_remote');

On first run of our application CF will call the userServices_remote bean which will crate a proxy component using the details we supplied in the config.xml. Once run you should see in the root of remote_cs a file called rUserServices.cfc which you should then be able to call.

Thats it really. You can call the proxy CFC at http://localhost/remote_cs/rUserServices.cfc?method=getUserList and see the following result andy,helen,anthony,rob,leighton,lucy,mandy

My next blog post is going to take this a bit further and use it in conjunction with a ajax framework like jQuery

Update: Brian Kotek has taken this post and expanded on it making life even more easier. Do check it out