Reusable libraries for Flex applications
One of the things experienced developers usually ask when they begin working with Flex is: How do I reuse code? Well, my answer is that there are at least three ways of creating reusable libraries for Flex applications.
The most common (and oldest) is probably Runtime Shared Libraries (RSL). These are groups of ActionScript classes that are compiled into SWCs and loaded into your SWF at runtime. However, unless you use the same RSL for more than one SWF within a single browser session, RSLs inflict a performance hit. There are two reasons for this: 1. The RSL of necessity includes all of the Flex framework code required to make its classes compile. Most of this Flex framework code also exists in your SWF. As a result, you’re loading the same code twice. 2. When you load a SWC, you load the entire SWC, whether you actually use all of the classes it contains or not.
With Flex 3, it is possible to load the Flex framework as a signed RSL that will be stored in the Flash player’s cache so that each client only needs to download it once. This has some obvious benefits. Check out the Adobe labs wiki for details.
Another way to reuse code is with Modules. Modules are primarily designed to enable lazy loading of code. They are compiled SWFs but they cannot run as standalone apps; they need to be loaded into a SWF that serves as the root application. They have a tremendous advantage over RSLs in that they can be compiled without the framework code that’s already loaded into the root SWF. However, they also have a disadvantage if you want to use them in more than one app: If you compile your Module without the framework classes for one application, and then try to use the Module in a different application, you may end up missing some necessary framework code and running into run-time errors.
My personal preference is to build common libraries of un-compiled ActionScript classes and MXML components. This way, you compile just the classes you need into your main application. For my way of thinking this method provides the greatest flexibility and ease of use. To do this, just build your common-lib directory and then add it to your Flex project’s build path. To add a library to the project’s build path from Flex Builder, select Project Properties -> Flex Build Path -> Source Path then click Add Folder.
Having done this, you can import any of the common library classes or directories you need into your application using the standard import syntax. You can also represent your common library directories as namespaces for use in your mxml markup.
OK, so there you have it - three ways of creating reusable code. Each of these methods has its own advantages and disadvantages, and the method(s) you choose will likely vary based on your particular situation. But whatever you choose, make sure to do good work - and have fun.






Reader Comments
It seems RSLs and Modules can only be used with Flex - is this so? I’m trying to stay clear of the Flex framework for my project, but I still need a modular design with external libraries and plugins. Your 3rd method does of course work, but I don’t want redundant code being loaded when I load another swf that relies on the same libraries - and I don’t want to have to recompile all swfs that use the libs. I’ve tried compiling with Intrisic/External libraries and loading them in runtime, but that doesn’t seem to allow me to inherit from classes in external libraries.
Thanks!
@Jonas: Yes, Modules are a feature of Flex. If you want to avoid Flex, then I think SWCs are your best bet for reusable code loaded at runtime.
Allstate - Auto Indemnity Quotes Online - Formal Site for Wheels Cover, Home Guaranty, Monetary Products & More Rates and cover options (and their availability) diverge according to your state’s regulations.All you father to do is conjoin your stamping-ground and auto bond with Allstate, and you effect shelter up to 20%. Hands down, it’s the easiest speed to secure a fortune of wampum without sacrificing worth protection.