Futurescale, Inc. PureMVC Home

The PureMVC Framework Code at the Speed of Thought


Welcome, Guest. Please login or register.
November 20, 2017, 03:53:45 PM
Home Help Search Login Register
News: Please DM @puremvc on Twitter to gain forum access. Spambots are why we can't have nice things.
Pages: [1]
Print
Author Topic: request for small mod to facade api  (Read 4781 times)
jason
Jr. Member
**
Posts: 13


View Profile
« on: July 12, 2011, 06:15:53 PM »

Hi All,
 I've been using puremvc here and there for a while now and like lots of what it has to offer,
 (dont like all the boiler plate though).
I also use spring a lot, and in order to properly springify things I found it convenient to be able to get/set Lists for registered Proxies, Mediators, and Commands. I end up creating duplicate Lists of each, with getters & setters in my ApplicationFacade, which are injected directly by spring, and then I do something like this in the spring afterPropertiesSet:
   public void afterPropertiesSet() throws Exception {
      for (IMediator mediator : instance.mediators) {
         instance.registerMediator(mediator);
      }
         
      for (IProxy proxy : instance.models) {
         instance.registerProxy(proxy);
      }

      registerCommand(MVCCommand.STARTUP.name(), startupCommand);

   }

I also then just iterate these lists in a shutdown command which saves boilerplate and ensures I unregister everything.
ie I dont have to maintain a list of stuff like this: getFacade().removeCommand(MVCCommand.SHUTDOWN.name());

It would be easier if the facade had getCommands(), getProxies(), getMediators(), and would be really nice if there were also a
setProxies(List<IProxy> proxies),
setCommands(Map<ICommand,String notification>) etc too so I dont have to create duplicate lists.

Thoughts?
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW
« Reply #1 on: July 13, 2011, 07:55:04 AM »

Not really sure why the ordinary startup process of creating and registering mediators doesn't work. I suppose I'm not clear on what 'springifying' is meant to do here. Can you give us a slightly bigger picture?

-=Cliff>
Logged
jason
Jr. Member
**
Posts: 13


View Profile
« Reply #2 on: July 13, 2011, 09:11:52 PM »

1. I want to be able to dependency inject my proxies, commands, mediators.
2. I want to be able to specify an initial configuration/register required components at startup.
2. I want to clean them up again without manually managing the process as much as possible.

so for example I have inside a spring context something like this:
Code:
<!--
 this MUST be initialised before any mediators
   --><bean  id="applicationFacade" class="com.my.application.ApplicationFacade" factory-method="getInst" init-method="start">
    <property name="app" ref="demoApp"></property>
    <property name="mediators">
    <list>
        <ref local="appMediator"/>
    <ref local="homeUIMediator"/>
   </list>
    </property>
    <property name="models">
    <list>
    <ref local="propertyProxy"/>
    </list>
    </property>
     <property name="commands">
    <list>
    <ref local="propertyChangeCommand"/>
    <ref local="botStateChangeCommand"/>
        <ref local="startupCommand"/>
    </list>
    </property>
   </bean>

  <!-- MVC Command Beans -->
<!--I chose to define my command mappings in code
but could have used a Map structure here instead, or I could have embedded the notification Name in
each of my command classes(not good if u have same command class associated with different notifications in different cores or projects, but otherwise fine).
<property name="commandMappings">
<map>
    <entry key="abc" value-ref="myCommand"></entry>
   </map>
 </property>
-->
   <bean id="startupCommand" class="com.my.mvc.controller.StartupCommand">
     <constructor-arg value="${multitonControllerKey}"/>
     <property name="propertyChangeCommand" ref="propertyChangeCommand"/>
     <property name="botStateChangeCommand" ref="botStateChangeCommand"/>
     <property name="loadDataCommand" ref="loadDataCommand"/>
     <property name="homeUIView" ref="homeUIView"/>
   </bean>
... etc

then in StartupCommand I have something like :
Code:
   ApplicationFacade facade=(ApplicationFacade) getFacade();

       //just register everything
       for (IMediator mediator : facade.getMediators()) {
facade.registerMediator(mediator);
}

for (IProxy proxy : facade.getModels()) {
facade.registerProxy(proxy);
}
        facade.registerCommand(Command.PROPERTY_CHANGED.name(), propertyChangeCommand);
        facade.registerCommand(Command.STATE_CHANGED.name(), botStateChangeCommand);
        facade.registerCommand(Command.LOAD_USER_DATA.name(), loadDataCommand);

so it makes my code much more manageable, makes the spring config straightforward, and makes puremvc more easily springy/DI-able.
instead of having each of my components aware of a shutdown notification, I override onRemove in each
them (why doesnt ICommand have an onRemove?) in a single Shutdown command, iterate all of the components and remove them.
if I could do this without needing to maintain a separate list, and I could get access to the current list of
registered components it would be quite helpful.

At least like this I can use DI for all my components, inject those into the facade, my code does not require knowledge of spring, my config files are reasonably readable, and I dont have to worry about forgetting to register/unregister something.

I hope this makes sense.

fyi I'm currently using a similar mechanism with roboguice + puremvc in Android. more painful, and more incursion but thats just guice.

Cheers.



Logged
jason
Jr. Member
**
Posts: 13


View Profile
« Reply #3 on: July 13, 2011, 09:25:09 PM »

Not really sure why the ordinary startup process of creating and registering mediators doesn't work.

-=Cliff>
the ordinary startup mechanism is not very DI friendly, with spring at least I'd have to use methodinvokingfactorybeans all over the place, and it would not be worth the trouble or mess, and I'd still have no way to clean up. PureMVC all by itself makes no attempt to call onRemove for components when it shuts down or a core is removed, so if it needs to be done at all, it needs to be done manually for each component.

if anyone has a better/alternate approach I'd love to hear about it.
Cheers.


Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW
« Reply #4 on: July 14, 2011, 10:12:48 AM »

Quote
makes no attempt to call onRemove for components when it shuts down or a core is removed, so if it needs to be done at all, it needs to be done manually for each component.
That is because when you remove a core, the items in the core should not be referenced outside the core itself. So when the core is removed the contents go with it when the garbage collection facility is run.

Quote
It would be really nice if there were also a setProxies(List<IProxy> proxies), setCommands(Map<ICommand,String notification>) etc too so I dont have to create duplicate lists.

PureMVC was meant to be extended easily in the field to do anything you want. For instance to add these methods in a reusable way, just create an AbstractDIFacade that extends Facade and does nothing more than add these methods. Put that class in a library and include it with PureMVC in any project you want. Then instead of creating your ApplicationFacade based on Facade, you'd instead extend AbstractDIFacade.

-=Cliff>
Logged
Pages: [1]
Print
Jump to: