In many of today’s popular ColdFusion frameworks, a common practice being utilized is storing instantiated objects in the Application scope as singletons. This becomes problematic when we need to access an object stored in the Application scope but the request comes from outside of the application (such as remote calls through AJAX). So what do we do when we want to access these objects remotely? Well, you can’t -- at least not directly.
The solution to this accessibility problem is to instead interface with a remote proxy in place the original inaccessible object. Some of you may be wondering, “What is a remote proxy?” Some of you may be wondering the same thing while simultaneously scratching your head. Some of you may be wondering how I know what you’re thinking and doing as you're reading this blog post (but I digress).
A remote proxy is a specific type of Proxy Pattern. To help illustrate what a proxy is, here is a great analogy from JavaWorld:
A friend of mine -- a medical doctor, no less -- once told me that he convinced a friend to take a college exam for him. Someone who takes the place of someone else is known as a proxy. Unfortunately for my friend, his proxy drank a bit too much the night before and failed the test.
So for our case a remote proxy is an accessible object that takes the place of the inaccessible object. Think of it as a substitute object. Got it? Great.
A remote proxy allows you to interface with an object located in another address space. From a ColdFusion prospective, our Remote Proxy is a light weight CFC that is accessible from the web root and has its access set to remote in order to permit external access (you can hard code your own remote proxies or use a framework like ColdSpring to generate them for you). Let’s take a look a simple remote proxy example.
ProductProxy.cfc
<cfcomponent output="false">
<cffunction name="getProducts" output="false" access="remote" returntype="any">
<cfreturn APPLICATION.productService.getProducts() />
</cffunction>
</cfcomponent>
In this example we our ProductProxy.cfc accesses a ProductService object stored in the ColdFusion Application scope. It calls the getProducts() method that returns the products data to the remote caller.
As you can see from this example, our remote proxy functions as a surrogate interface to an object that we originally intended to access. It is fairly trivial to implement and provides you access to objects that you would otherwise have not been able to leverage. In a future post I will go over a simple AJAX application that leverages remote proxies to access objects in Application scope and consumes the returned data.
snoozer bed
November 26, 2009 at 11:13 PM