I'm hitting a wall on a requirement for a Flex application which utilizes the LCDS version included with ColdFusion.
I have the project setup to use DAO's, Assemblers and ValueObject (cfc's) which I call from Flex via DataService implmentation and the fill(), getItem(), etc... methods.
I need to setup my ColdFusion layer to access multiple datasources from the same Flex application. However, because of the way LCDS gets called from Flex, it doesn't seem that Application or Session variables in ColdFusion persist when I set them. I was thinking I could just have the HTML (cfm) wrapper page for my Flex application set that, but it is not recognized when I go to make a fill or get call against the assembler.
I thought about passing the datasource as a parameter each time a call is made, but while that would work when I initiate fill and get methods, the sync methods are "invisible" and I wouldn't be able to append an argument to them.
So my issue is... how do I go about having ColdFusion read a dynamic variable to define it's datasource?
Tom Jordahl provided the answer on this for me. Here's the solution:
1) Prior to implementing this solution, it's important to realize that because LCDS manages the data, switching datasources dynamically could potentially cause issues if there is overlap between identifiers on the records you are retrieving. In my case, I'm using GUID fields as primary key and identifiers in my destinations, so this is not a problem. However, for example, if you were to use an incrementing integer for the identifier, then you could feasibly have LCDS managing a record, which would match identifiers with a record if you switched datasources on the fly.
2) I'm using ColdFusion as my middle-tier layer, but this could be applied to any form by accessing the flex.messaging.FlexContext class within your solution of choice.
3) The solution involves extracting the ID value of Flex client which makes calls against your Destination. Grab the identifier by calling FlexContext.getFlexClient().getId() to get a unique key for the "session". I use the term "session" loosely, as it's really just representative of the particular swf instance which has been loaded with your Flex Application. However, the ID doesn't change and persists across all calls you make against your destinations.
4) On application load, make a call to your CF layer which gets the ID, and sets it to the Application scope of the CF server as follows:
6) The last consideration is that each connection that is made via Flex will be setting a new variable in the Application scope on the Coldfusion server. This is a pretty minor concern for me, and I'll just run a schedule to clear the Application scope once a day, but if you have a high volume of users, a better solution would have to be found.