PureMVC Architects Lounge

PureMVC Manifold => MultiCore Version => Topic started by: rockhowse on August 30, 2007, 12:14:23



Title: PureMVC + Modules
Post by: rockhowse on August 30, 2007, 12:14:23
There isn't really a lot of documentation on integrating PureMVC with Flex Modules so I thought I would post on these forums. (see attached for source)

The code that is included is based off Vic on Flex's Cairngorm + Modules example found here:

http://viconflex.blogspot.com/2007/05/can-cairngorm-and-modules-play-nice_12.html (http://viconflex.blogspot.com/2007/05/can-cairngorm-and-modules-play-nice_12.html)

This example is implemented using PureMVC and demonstrates how to use a "shell application" to load Modules that have been implemented using the PureMVC framework. It doesn't do anything fancy, but gives someone a good place to start when trying to dynamically link in PureMVC based modules into a Flex application.

The key to this example is that you can't reference the ApplicationFacade in the main application.  I had issues trying to load an ApplicationFacade in the root app and then have each module load it's own ApplicationFacade or register Notifications to either facade. There is something interesting going on with the Singleton pattern and how the main application -> modules scope is being handled.

I am going to start working on a more elegant solution that allows cross-module notifications but for the time being, this should get people started on the road to using Modules built on the PureMVC framework.


Title: Re: PureMVC + Modules
Post by: puremvc on August 30, 2007, 07:11:45
Nate,

I will check this out. You'd mentioned the modules thing earlier, and its been on my queue of things to address. Glad you posted your findings for us to get a start with.

I've got some ideas about the coordination issues surrounding the ApplicationFacade, but I'll look at what you've got here first.

Thanks,
-=Cliff>


Title: Re: PureMVC + Modules
Post by: rockhowse on September 18, 2007, 07:02:31
Eureaka! I got the cross-Module communication functionality working =) In my first example above, I had two modules that were loaded with their own independent ApplicationFacade. They each register their own command, but can't communicate in a cross-modular fassion. If I added an ApplicationFacade to the main app, all sorts of scoping issues came into play =P

Evidently this is similar to what happens when you try to marry Cairngorm + Modules as well because of the Singleton pattern being used.  In order to accomplish cross-module communication I used a technique that makes modules play nice with Cairngorm found here:

http://blog.digitalmaelstrom.net/2007/04/cairngorm-modules-trouble.html (http://blog.digitalmaelstrom.net/2007/04/cairngorm-modules-trouble.html)

For this new demo, I am able to get two modules loaded, each registering their own seperate commands. Each is now able to dispatch those commands in a cross-modular fassion using a centralized ApplicationFacade that was registered to each module upon creation.  This should really help someone out who wants to have an application that will be loading modules dynamically at runtime.

The key was the interface that lets my Modules be "Pure MVC Aware": IPureMVCAware. It basically forces a PureMVC aware module to implement the register/unregister functions upon load/unload from the main Application. I need to dig a bit deeper into getting Views/Models included into a demo as well as implement some cleaner unregister code, but for now this shows you how to send commands in a cross module fassion.


Title: Re: PureMVC + Modules
Post by: puremvc on September 19, 2007, 09:21:49
You rock, Rock. :)

I am sorry to say that I have been so swamped I haven't even had time to look into the previous example. I was waiting till I could clear a good amount of time to look into it.

It sounds like you've nailed the problem on the head, which is that you've got to set up a sort of 'master/slave' relationship between a module and the app that loads it.  The IPureMVCAware interface seems like a reasonable approach to the problem.

I really look forward to testing this out and seeing how it works. Can the loaded module also be written to run as a standalone application as well, following its own startup process depending upon whether its been loaded by another app or not?

-=Cliff>


Title: Re: PureMVC + Modules
Post by: rockhowse on September 19, 2007, 10:23:11
Unfortunatly, I don't think you can have a module switch between Module/Application at runtime because each inherits from a different base class =(.  If you try to run the module as a stand alone app I think it just gives you a grey screen.  From what I have seen you can replace <mx:Module/> with <mx:Application/> and it works pretty well. That's how I was originally testing my modules before I realized I could debug modules by loading the <moduleName>-debug.swf instead of the <moduleName>.swf from my main application.


Title: Re: PureMVC + Modules
Post by: arend on November 10, 2007, 05:09:47
This is exactly what I was looking for!

Thanks for posting your findings.

Arend


Title: Re: PureMVC + Modules
Post by: zilet on January 14, 2008, 08:16:43
Hello,

  did you come to some good solution? I just started making modular app with flex and PureMVC and I ran into the singleton problem. The solution with passing the ApplicationFacade object is nice but it is not easy to handle registering and unregistering Mediator, commands and proxies.

I was thinking of making ModuleProxy that will handle registration. In this case the registration of meditarors will have to be called in the folowing manner:

:
moduleProxy.registerMediator(MediatorObject)
and similar for other registrationgs. ModuleProxy will be registered uppon moduleload and every time something is regitered through it, it will keep the info about registered medators, commands and proxies in array so on Module onload it can be easily unloaded.

This is the first thing that came across my mind and I don't really like the solution but it will work. The problem is that when somebody develops the modules he has to be aware of this so the module will not be so independent.

Any other idea?


Title: Re: PureMVC + Modules
Post by: zilet on January 18, 2008, 11:00:27
I had some time to consider this problem and I have come to some solution which I think can do it. It has some drawbacks but allows unregistering of all commands, mediators and proxies when the module unloads.

 I have attached a source of the module test application. It simply loads 2 modules which both use puremvc.
 In main application I have created ModuleMediator which controls loading and unloading of modules. As a helper class i have used ModuleFacade (it is located in the "util" package). ModuleFacade is a singleton which contains reference to the ApplicationFacade object (which is not needed because it is also singleton but it was kinda clearer this way for me). ModuleFacade controls registering and unregistering of modules.
Every module has a ModuleContext associated with it and it is available through ModuleFacade. ModuleContext holds all module registered commands, proxies and mediators.

The only thing that one have to do when creating a module is to create its startupclass which will initialize startupcommand.
 
In this command we have to register other things through the reference of ModuleContext object.

I'll continue on testing the solution and I would be glad to hear some comments.


Title: Re: PureMVC + Modules
Post by: puremvc on January 18, 2008, 02:24:15
Zilet,

Sounds like a straightforward approach. I'll have a look shortly. Meanwhile, I know there are a lot of module-starved folks out there and we need an official utility and demo. This could be it. Lets hear what people have to say about this.

If it seems like a solid answer, would you like to have it folded into the site as an official utility, and separate demo? If so, have a look at this post over in the Contributor Central forum for an idea about how we're handling the community contribution aspect of the project now. http://forums.puremvc.org/index.php?topic=142.0

If so, please send me a copy by separate email, and I'll put you in the queue of projects to be created and get you repo access, etc.
-=Cliff>


Title: Re: PureMVC + Modules
Post by: tjiddy on January 21, 2008, 02:48:59
I am a PureMVC noob, but what do you think about moving the actual loading of the module into a LoadModuleCommand registered to listen to a LOAD_MODULE Notification, the body of the Notification can contain the module to load.  Then the ModuleMediator can do what other mediators do, and translate events into Notifications.  This would let you load modules from anywhere in your app (from other mediators, or commands)

Todd


Title: Re: PureMVC + Modules
Post by: puremvc on January 21, 2008, 10:27:18
Right on.

-=Cliff>


Title: Re: PureMVC + Modules
Post by: zilet on January 22, 2008, 02:59:10
you've got a good point there!



Title: Re: PureMVC + Modules
Post by: tjiddy on January 22, 2008, 02:01:52
So I tried to implement this using your ModuleTest, but once I moved the module loading into a command object, I lost the ability to know which ModuleLoader to load the module into.  I'm sure you can chalk this up to my lack of experience, but how would you accomplish it?


Title: Re: PureMVC + Modules
Post by: puremvc on January 22, 2008, 05:33:35
You could use the 'type' parameter to the notification to hold the name of the module to load and let the body be the reference to the ModuleLoader to load said module into.

-=Cliff>


Title: Re: PureMVC + Modules
Post by: zilet on January 23, 2008, 10:16:41
Hola,

   I refactored some code and created Commands for loading/unloading modules. But while I was testing the solution I found one problem. After the module is unloaded next time it is loaded registered commands get called twice. I am doing unregistration of all commands, proxies and mediators that module had when it unloads.

  Right now I don't see the problem so please help  ???

  The code is attached to this post.


Title: Re: PureMVC + Modules
Post by: tjiddy on January 23, 2008, 10:27:26
You could use the 'type' parameter to the notification to hold the name of the module to load and let the body be the reference to the ModuleLoader to load said module into.

The way I got around it was inside my controller, I loaded the module using ModuleManager.getModule(), registered the module with the moduleFacade, then sent a LOAD_MODULE_SUCCESS notification with the IModuleInfo as the body.  The mediator that sent the request notification originally, is listening for that notification and then can do something like:

:
app.container.addChild( (notification.getBody() as IModuleInfo).factory.create() as Module2 );

--The positive, no moduleloaders to pass around, clean separation of controller from view components.

--The negative - The same notification is sent regardless of which module was loaded making loading multiple modules simultaniously ugly. (the mediators would have to inspect the module, or the controller could add the module name in the notifications type field.


Title: Re: PureMVC + Modules
Post by: puremvc on January 23, 2008, 12:29:24
Whan you say 'in the controller' are you referring to a Command executed by the Controller, or did you actually extend Controller and add this functionality?

-=Cliff>


Title: Re: PureMVC + Modules
Post by: tjiddy on January 23, 2008, 03:47:38
sorry ... in the command


Title: Re: PureMVC + Modules
Post by: Lord on March 20, 2009, 04:50:02
Hi all,

this approach seems to fit very well with my expectations..

a question for rockhowse...

in your sample i found TestMessageController class that listens for notifications...so i would modify this class to change view within a module...for example a module sends a notification (note and body)...then an other module takes the body and changes his views..(e.g. data provided into a datagrid). have i to implement a mediator in the module? how to rich this goal?

finally, if modules are unloaded is there a right way to follow?

Thanks in advance... ;)

Reagards Lorenzo


Title: Re: PureMVC + Modules
Post by: puremvc on March 20, 2009, 06:12:39
Don't send notifications between modules. You can't guarantee both modules define the same note constant to mean the same thing. Notes are handy to toss around inside a single core (the shell or a module) where all the actors see the same static constant, but you need to either pass a strongly typed object between cores using PipeMessages (see PipeWorks demo) or make interface calls (see Modularity demo).

Those demos, for ease of use pack the modules and shell and common code all in one project and instantiate the modules rater than loading them, but in the real world these will be separate projects.

-=Cliff>


Title: Re: PureMVC + Modules
Post by: Lord on March 20, 2009, 08:22:41
Hi Cliff,

thanks for the reply...

pureMVC is very very very cool work...congratulations...but i make difficult to understand it!!!  :'(

i would create an application that can loads modules. In particular i would have an inter module communication...

can you give me you some suggestions?

Thanks again


Title: Re: PureMVC + Modules
Post by: puremvc on March 21, 2009, 06:54:25
Yes.Firstly to ease the learning cureve I suggest reading the Framework Overview and Best Practices docs (under the 'Docs' tab of the site). While they are based on the Standard Version, the differences to MultiCore are very slight. The roles, responsibilities and collaborations of the actors are the same.

As for intermodule communication this is a unique capability of MultiCore, and there are 2 possibilities. The first is demonstrated by the Modularity demo and that is interface communication. Each module and the shell app (collectively referred to as 'cores' since each represents a separate set of MVCF actors. Each core implements one or more interfaces exposed for synchronous calls made by other cores.
The other method is asynchronous/anonymous messages passed via pipeline to other cores. This is afforded by the Pipes utility and illustrated by the
Pipeworks demo.

You can mix and match thes methods as needed within the same application as needed.

Loading and unloading of modules is beyond the scope of those demos, but on the AS3 MultiCore menu you'll find a link to a great tutorial on module loading/unloading with Garbage Collection, and a functioning demo, done by Simon Bailey.

-=Cliff>