Module 101 – What is module & how it works

Starting from Flex 2.0.1, modules are supported.

What is module? To quote Adobe Documentation:

Modules are SWF files that can be loaded and unloaded by an application. They cannot be run independently of an application, but any number of applications can share the modules.

Why do we need modules? A lot of reasons, I say. Here is two major reason for me. Modules can be used to partition your application into logical block. While this would encourage agile development, this would decrease the
chance of poorly written code to cause the problems in other part of the application. Not all developers write the best code like us, agree? Secondly, it would reduce the initial load time of your application. Why would you want user to wait longer to use your awesome software? If you are still not convinced, read more on Adobe live documentation on modular application section.

I am not a fan of repeating so refer here how to write your first module and here how to compile it. Or here is my sample FlexBuilder project to get it started.

How does it work? Modules can be loaded by the ModuleLoader class with the expected “loadModule()” and “unloadModule()” command using “url” property of the ModuleLoader. When the module is loaded, Flex create the factory for the module and stored in the ModuleManager with url as unique identifier. So, if you have multiple ModuleLoader loading the same module url, on the second time around, Flex doesn’t need to load the module from the network and instead it will create the instance from the already loaded factory. The main reason I have been talking about factory object for module is, inside Flex, there is no isolation / partition for the module and its children. There is no properties to tell whether particular component is created inside which instance of the modules. The only way so far I could think of is to go through the parent hierarchy to find out under which module does a particular component belongs to. However, there is one method provided by the ModuleManager, “getAssociatedFactory(object)”, which tells you whether the particular object belongs to which module factory (note: just module factory not module instance)

Here are some note worth points you want to remember while working with modules.

  • Modules are loaded into the child of the current application domain. You can specify a different application domain by using the applicationDomain property of the ModuleLoader class.
  • When two classes of the same name but different implementations are loaded, the first one loaded is the one that is used.
  • If you are doing something to module internal components based on the module ready event, make sure you pay attention to ModuleEvent.Ready event being fired before initialize and creationComplete event are fired.

Here is the sample module application with source, my intention is to improve upon this project for the upcoming Cairngorm & Module blog entry.

What’s next? I am planning to follow up with how modules and Cairngorm framework work together. Here is the sneak peek. It works for most of the cases out of the box.

Other Useful Links
Alex Harui Presentation
Flex Documentation Team: Creating Modular Applications

Trackbacks & Pingbacks 2

  1. From How module works in flex « Preenu’s Weblog on 25 Mar 2008 at 1:26 pm

    [...] How module works in flex refer the link [...]

  2. From How module works in flex « Preenu’s Weblog on 13 Jan 2009 at 8:57 pm

    [...] How module works in flex click here [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *