views:

128

answers:

1

Hi there

I'd like to be able to understand how to develop a Flex application such that I could provide implementation classes at runtime. In the Java world I'd specify interfaces in an JAR (e.g. myapp-api.jar), the implementation in a separate JAR (e.g. myapp-impl.jar) and package these along with other resources in the application WAR (e.g. myapp.war). Within the code of the application I would instantiate the implementation classes dynamically.

Is this approach possible in Flex? I'm aware that I can instantiate classes dynamically so that's a good start. I'm a bit confused by modules, RSLs and SWCs though.

I was hoping to create a SWF application that had references to an interfaces SWC and an implementation SWC. The idea is that if I need to tweak the application for a specific customer then I could create a new implementation SWC and not have to modify the SWF or interface SWC.

Any ideas?

+1  A: 

This is a bit harder to do in flash/flex than it is in java, or at least it is a bit more obscure if you are coming from a java background). I have a couple of pointers for you.

You only have to use RSLs if you really want to have separate libraries of classes at runtime, and especially if you think this library will be used across several projects that the same set of people will be using. (They are mainly intended to decrease download/startup time)

You can also divide your app in several parts yourself - in that case you need to make sure that you don't pack the same info twice - this can be done by generating a linkreport on the first compiled swf and excluding these classes from the second compile. This is how modules work in flex.

SWCs are mainly used as a compile-time library (they are basically a zip containing a swf and a metadata xml file that describes its contents).

If you want to have a separate set of interfaces in a library you could generate a swc with the interfaces and compile another swc with the implementation, you should exclude the interfaces when compiling the implementation classes if you want to avoid having duplicate class/interface definitions. If you compile your final app when linking both the interface and implementation SWCs without excluding the classes you can just use your single final SWF to run the app - if you want to keep it really separate you should look into modules (or plain loading swfs if you are creating a pure actionscript project).

I did use SWCs myself as described above, and am now looking into RSLs and Modules myself for our curren project.

Simon Groenewolt
Thanks for the reply Simon. I managed to compile an interfaces SWC and an implementation SWC (minus interfaces) using compc/ant. I guess the next step is to compile the interfaces SWC into the app then somehow load the implementation SWC at runtime. I really want to avoid recompiling the main app if possible.Ideally I'd like to pass a param to the SWF to indicate which implementation SWC to load at runtime. I gather that only SWFs can be loaded though so I'll need to figure out how to get around this - e.g. can I convert SWC to SWF or extract the SWF from the SWC and use it?
ukdavo
You can get a swf from a swc by unzipping the swc. You can load the swf using the Loader class - if you want the code to share the same context you probably have to set the ApplicationDomain of the loader to the same domain as the rest of your application (I'm not sure if that is the default or not).
Simon Groenewolt