Fling, a Spring-ish IoC Framework for Flex
Posted by Joe Rinehart at 3:58 PM
1 comments - Categories:
Fling | Flex
Configuring your Flex app for different environments got you down? Tired of -services? Wish you had something like Spring (or ColdSpring) to configure your Flex apps? If so, you need some Fling! Project URL
Fling is available through RIAForge at http://fling.riaforge.com. The current download contains the library, an example app that shows how to use Fling instead of service-config.xml to talk to ColdFusion, and an RSS reader that shows how business delegates can be used (and swapped out) to change backend formats without changing the application itself.
It's still under 72 hours old: expect issues here and there and API cleanup to take place.
Quick Example
I've put an example application up (a simple RSS reader) that allows you to switch configurations via URL variable. In "mock" mode, which simulates having a backend, the configuration .swf contains fake data. In "production" mode, a real business delegate is used to talk to RSS feeds.
Why'd I Write This?
The other day, I was working on an app where I was sharing a good sized Flex library between AIR and Flex applications. Each app, though, had its own set of business delegates, but all the controllers cared about was getting something implementing IAuthenticationDelegate. I hit a point where I really needed to separate configuration so that my controller could just say:
...where that magical factory would figure out if I should be using the AIRAuthenticationDelegate or the FlexAuthenticationDelegate.
Ok, big words around a simple concept: I wanted to separate configuration from my code, and I wanted to do it like Spring (or ColdSpring) in Flex.
I stayed up the past few nights and hacked out "Fling."
The Dime Tour
- You create a component extending Fling's "Configuration" class (which extends Application) - it then contains all of your <Bean> definitions and compiles into a .swf
- In your application, you use two tags to create a bean factory and load your configuration .swf into the bean factory (multiple configs can be loaded into the same factory, and local configs are supported as well)
- The implementation so far supports properties of type:
- Value (simple)
- List (array)
- Map (object)
- Ref (reference to another bean)
- Bean (inline bean)
- Singletons and transients are boths supported.
Deeper Thoughts
Configuration as .swf?
It makes sense, really. If configuration was done via .XML, you'd have to explicitly link in your concrete implementations of your interfaces to your application, which is information your application-at-large shouldn't know about: it should just care about the interface. In other words, you could move IoC config to xml all day long but still have to do this in your code:
// Implementation of IServiceDelegate
import business.XMLServiceDelegate;
With a config .swf, your main application just links in the interface: your configuration swf links in the concrete implementation automatically, because it's referenced in the bean definition via binding (note the curly braces!):
<fling:Bean id="ServiceDelegate" type="{business.XMLServiceDelegate}" />
Not-quite-Spring-syntax MXML
The configuration syntax isn't _quite_ as clean as Spring's XML. That's because it's using MXML to instantiate things like Bean, List, Map, Ref, etc., and we have to follow the appropriate syntax. However, sticking to this leaves us open to creating configuration apps via AS3 instead of MXML, which would be more terse but less Spring-ish.
"Distributed" Flex APIs leveraging .swf configuration
This is a wild thought. Let's say you wrote the killer Flex application. One that's so great that other Flex developers are clamoring to drop its capabilities into their own applications, including the ability to talk to your backend services. You know, something like a to-do list :).
Using the idea of a configuration .swf, this could be done nicely. You distribute an SDK that contains nothing but interfaces, like ITodoService, with methods like getTodos().
You then create and host a Fling configuration .swf that contains all the configuration for talking to your business tier. It contains the real ColdFusionTodoService or JavaTodoService - your 3rd party developers don't care. They just load your configuration .swf into their application's Fling BeanFactory, and they can suddenly ask the beanfactory for the TodoService, getting back whatever you've configured.
Food for thought, if you're developing something that could be resold/rebranded!
Coming Soon to Fling
BeanFactoryUtils to manage getting hooks to specific bean factories from anywhere in your app (at which point I'll kiss getInstance() the heck goodbye)
Jason Holden wrote on 07/29/07 1:26 AM
This is really cool! Is it possible to integrate this with ModelGlueFlex?