tag:robotlegs.tenderapp.com,2009-10-18:/discussions/questions/233-is-it-possible-to-have-more-contexts-in-one-non-modular-applicationRobotlegs: Discussion 2018-10-18T16:35:14Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/24088182010-07-29T15:14:48Z2010-07-29T15:14:48ZIs it possible to have more contexts in one non-modular application?<div><p>If your problem is just about 'loading' the modular stuff /
injections between modules, then you could look at using the older
version of the modular solution - I'm still using it in my current
applications, and it works nicely whether you compile it all-in-one
or actually load modules at runtime.</p>
<p>Basically it has a module-to-module event dispatcher, and each
context also has its own eventDispatcher.</p>
<p>Module mediators act as gateways to relay module/internal events
in either direction as required.</p>
<p>It is NOT suitable where you're going to unload modules at
runtime (the version of the ModuleCommandMap in here doesn't clean
up after itself fully when modules are unloaded). But if you're
looking at compiling it all together anyway then you should be
fine.</p>
<p>The modular utility code is at:</p>
<p><a href=
"http://github.com/Stray/robotlegs-utilities-Modular">http://github.com/Stray/robotlegs-utilities-Modular</a></p>
<p>and there's a demo here:</p>
<p><a href=
"http://github.com/Stray/robotlegs-modular-e-learning-demo">http://github.com/Stray/robotlegs-modular-e-learning-demo</a></p>
<p>The hooking up of the modules is fairly simple. Here's how I'm
doing it (the modulesList is a vector of modules to be
integrated).</p>
<p>If you want to go this way then I'm happy to share the
ModuleStatusService classes as well - I found I needed to manage
whether all the modules had run their startup before I started
doing stuff:</p>
<pre>
<code>public function integrateModules(modulesList:Vector.<ModuleContextView>):void{
_modulesList = modulesList;
var startupModuleStatusService:ModuleStatusService = new ModuleStatusService();
startupModuleStatusService.eventDispatcher = _moduleEventDispatcher;
_moduleEventDispatcher.addEventListener(ModuleStatusServiceEvent.STATUS_CHANGE_COMPLETE, allModulesStartupCompleteHandler, false, 0, true);
startupModuleStatusService.startMonitoringModuleStatus(modulesList, null, ModuleContextEvent.MODULE_STARTUP_COMPLETE, ModuleContextEvent);
var nextModule:ModuleContextView;
for each (nextModule in modulesList){
nextModule.setModuleDispatcher(_moduleEventDispatcher);
nextModule.startup();
contextView.addChild(nextModule);
}
}
private function allModulesStartupCompleteHandler(e:ModuleStatusServiceEvent):void
{
fireCompletion();
}</code>
</pre>
<p>private function fireCompletion():void{</p>
<pre>
<code> trace("fireCompletion");
var evt:StateChangeEvent = new StateChangeEvent(StateChangeEvent.STATE_COMPLETED);
_moduleEventDispatcher.dispatchEvent(evt);
}</code>
</pre>
<p>Stray</p></div>Straytag:robotlegs.tenderapp.com,2009-10-18:Comment/24088182010-07-30T07:05:27Z2010-07-30T07:05:27ZIs it possible to have more contexts in one non-modular application?<div><p>Thanks Stray but if I don't want to use Modules, is there a way to partition my contexts?</p></div>borekbtag:robotlegs.tenderapp.com,2009-10-18:Comment/24088182010-07-30T07:16:29Z2010-07-30T07:16:29ZIs it possible to have more contexts in one non-modular application?<div><p>That's an interesting question.</p>
<p>My understanding is that multiple-contexts would be the equivalent of modular anyway. The modular set up is just multiple contexts + a shared dispatcher and a shared commandMap. It's not 'Modules' in the Flex sense.</p>
<p>Anyone else got a suggestion?</p></div>Straytag:robotlegs.tenderapp.com,2009-10-18:Comment/24088182010-07-30T10:24:57Z2010-07-30T10:24:57ZIs it possible to have more contexts in one non-modular application?<div><p>Sorry for the delay - was offline for a couple of days. I think we need to figure out how, exactly, you'd like to partition things. If you aren't loading in new "modules" at runtime, then there is generally very little point to building your app out of multiple contexts. A context is simply a bootstrapper, responsible for instantiating, configuring and holding on to the various framework apparatus (an Event Dispatcher, Injector, Command Map, Mediator Map etc). Perhaps you have been setting up all your dependency injection rules in the context directly and would like to split that up - this is usually done by moving that configuration into a couple of commands.</p>
<p>If you really do want to go with multiple contexts then you need to decide how you would like to communicate between them, and how you would like to structure your dependency rules. The reason that this is not built in to the framework directly is that there are so many ways to architect modular hierarchies that a one-size-fits-all approach might limit rather than enable people.</p>
<p>Perhaps you could explain the requirements of your application in a little more detail, and we can see what would work best for that setup.</p></div>Shaun Smithtag:robotlegs.tenderapp.com,2009-10-18:Comment/24088182010-07-30T14:49:56Z2010-07-30T14:50:40ZIs it possible to have more contexts in one non-modular application?<div><p>Basically I'm going to have quite a lot of mapping rules and I'm looking for ways to avoid one long AS3 file (it may have hundreds or thousands of lines). I tried something like:</p>
<p><br />
<br />
...</p>
<p>but that will create more EventDispatcher and the communication didn't seem to be going on.</p>
<p>Shaun, you mentioned doing mappings in Commands, I may need to look at that. Are there any blog posts / articles on how to do that?</p></div>borekbtag:robotlegs.tenderapp.com,2009-10-18:Comment/24088182010-07-30T15:18:13Z2010-07-30T15:18:13ZIs it possible to have more contexts in one non-modular application?<div><p>There is significant overhead when using multiple contexts (primarily due to the Mediator Map), so I'd suggest sticking to one context unless you plan to load and unload separate contexts at runtime.</p>
<p>Setting up (and tearing down) mappings in Commands is easy. Here's one approach:</p>
<pre>
// In your context:
override public function startup():void
{
commandMap.mapEvent(ContextEvent.STARTUP, ConfigureControllerCommand, ContextEvent, true);
commandMap.mapEvent(ContextEvent.STARTUP, ConfigureModelCommand, ContextEvent, true);
commandMap.mapEvent(ContextEvent.STARTUP, ConfigureServiceCommand, ContextEvent, true);
commandMap.mapEvent(ContextEvent.STARTUP, ConfigureViewCommand, ContextEvent, true);
dispatchEvent(new ContextEvent(ContextEvent.STARTUP));
}
</pre>
<p>Then, those commands set up the actual mappings:</p>
<pre>
// In ConfigureControllerCommand:
override public function execute():void
{
commandMap.mapEvent(ContextEvent.STARTUP_COMPLETE, StartRouterCommand, ContextEvent, true);
commandMap.mapEvent(SessionModelEvent.CURRENT_USER_CHANGED, LoadCurrentUserSummaryCommand, SessionModelEvent);
commandMap.mapEvent(SystemEvent.USER_SIGNED_OUT, ShutdownCommand, SystemEvent);
}
</pre>
<pre>
// In ConfigureModelCommand:
override public function execute():void
{
injector.mapSingleton(SessionModel);
injector.mapSingleton(GameModel);
injector.mapSingleton(HistoryModel);
}
</pre>
<pre>
// In ConfigureViewCommand:
override public function execute():void
{
mediatorMap.mapView(GameIntroView, GameIntroMediator);
mediatorMap.mapView(GamePlayView, GamePlayMediator);
mediatorMap.mapView(HistoryBarView, HistoryBarMediator);
// Always map the app mediator last - eager instantiation ahead!
mediatorMap.mapView(MyApp, MyAppMediator);
// Configuration complete, let's rock!
dispatch(new ContextEvent(ContextEvent.STARTUP_COMPLETE));
}
</pre>
<p>Notice that I bound all these commands to the STARTUP event. They will execute in the order they were mapped. Once everything is ready I dispatch a STARTUP_COMPLETE event and proceed from there.</p>
<p>This is just one way you can split your mapping/configuration up - you're free to split it up as you please.</p></div>Shaun Smithtag:robotlegs.tenderapp.com,2009-10-18:Comment/24088182010-08-01T08:50:55Z2010-08-01T08:50:55ZIs it possible to have more contexts in one non-modular application?<div><p>Thanks that has been helpful.</p></div>borekb