Follow up: JSON value object from Transfer via ColdSpring remote proxy
transfer , ORM , AOP , AJAX , coldspring 1 Comment »I think I found myself a solution. First, here's the problem.
The Problem
I have Transfer managing my ORM. It works awesome, I love it, and now I want to start building AJAX-based tools to manage the data through Transfer. But Transfer hides all properties of a class behind getter and setter methods, so when I hit a remote CFC to get, say, new("person") or get("person", 42), all that comes back in a JSON string is {}. No properties visible, so nothing comes back.
The Solution
My LTOFactory (Lightweight Transfer Object) is a component that will talk to Transfer, get the object I'm looking for, and then return the product of Transfer-ORM's built in getMemento() method, which is a simple struct that contains all the properties - publicly exposed. Now my JSON string has the properties!
The same concept for the get method can be, with a little elbow grease, made to work also for list, save, and delete if I choose. If I do that, LTOFactory probably isn't a very good name anymore, but I'm talking results here, not semantics.
The Code
Here is my LTOFactory code.
<cfcomponent output="false">
<cffunction name="init" returntype="Any" output="false">
<cfargument name="transfer" type="Any" required="true" />
<cfset variables.instance["oTransfer"] = arguments.transfer />
<cfreturn this />
</cffunction>
<cffunction name="new" output="false">
<cfargument name="class" type="string" required="true" />
<cfscript>
// Feign a local private scope (coming soon in CF9!)
var local = {};
// Retrieve the new Transfer Object (TO) from the Transfer factory
local.o = variables.instance["oTransfer"].new(arguments.class);
// Return the memento for the new object
return local.o.getMemento();
</cfscript>
</cffunction>
<cffunction name="get" output="false"><!--- not yet implemented ---></cffunction>
<cffunction name="list" output="false"><!--- not yet implemented ---></cffunction>
</cfcomponent>
Here is my ColdSpring configuration that sets up Transfer, its factory, the LTOFactory with dependency injection, and the AOP remote bean configuration so I can hit the LTOFactory via AJAX.
<?xml version="1.0" encoding="iso-8859-1"?>
<beans>
<!-- Transfer -->
<bean id="transferFactory" class="transfer.TransferFactory">
<constructor-arg name="datasourcePath"><value>datasource.xml</value></constructor-arg>
<constructor-arg name="configPath"><value>transfer.xml</value></constructor-arg>
<constructor-arg name="definitionPath"><value>/definitions</value></constructor-arg>
<singleton>TRUE</singleton>
</bean>
<bean id="transfer" factory-bean="transferFactory" factory-method="getTransfer" />
<bean id="LTOFactory" class="LTOFactory">
<constructor-arg name="transfer">
<ref bean="transfer" />
</constructor-arg>
</bean>
<bean id="remote.LTOFactory" class="coldspring.aop.framework.RemoteFactoryBean">
<property name="target">
<ref bean="LTOFactory" />
</property>
<property name="serviceName">
<value>RemoteLTOFactory</value>
</property>
<property name="relativePath">
<value>/remote/</value>
</property>
<property name="remoteMethodNames">
<value>get,list,new</value>
</property>
<property name="beanFactoryName">
<value>bf</value>
</property>
</bean>
</beans>
Recent Comments