Name of Complete Command

svanny's Avatar

svanny

29 Jun, 2011 04:48 PM

Hi everyone,
for example I have the following case:
I have a button labeled "Select and upload image". The button dispatches an event, the associated mediator dispatches a framework event.

The framework event calls LoadImageCommand which starts FileReferenceService.selectAndLoad();

The service dispatches a framework event, when the data is loaded (with a BitmapData as payload). The called command has to update a model and perform other actions. How should I name the command, which will be called?

LoadImageCompleteCommand -> Name does not tell the implementation. Flexible implementation, Command just for this case.

or

UpdateImageModelAndContinueCommand -> Name tells the implementation. Command for multiple cases?! Problem: Its not just updating the data, it performs other actions.

Which is more flexible and more common?

Thanks and regards
svanny

  1. Support Staff 1 Posted by creynders on 29 Jun, 2011 05:35 PM

    creynders's Avatar

    Most of the times if you have a hard time describing a command, it means it does too much. Chances are you need to split it up in multiple commands.
    Think about it, will any of the actions the command executes (or a group of actions) be reused in certain situations or is everything the command does really unsplittable?
    In the latter case I would keep it as one command and I'd go for LoadImageCompleteCommand, since I imagine it will always be executed in the same situation (ie. it won't be reusable for anything else)
    Correction: I'd call it CompleteImageLoadingCommand or FinishImageLoadingCommand, since I prefer to have an active tense for command names

  2. 2 Posted by svanny on 29 Jun, 2011 05:47 PM

    svanny's Avatar

    But you often have to perform multiple actions, haven't you?

    A very simple example:
    I have a LoadConfigCommand, which calls a service and parses the data.

    So my complete command has to put the data in a model and perform the next config step. So I can't have a "UpdateConfigCommand", because it's a "UpdateConfigAndLoadNextCommand".

    How would you handle this?

    Thanks!

  3. Support Staff 3 Posted by creynders on 30 Jun, 2011 07:34 AM

    creynders's Avatar

    I wouldn't let the same command handle the next config step for various reasons:

    • if you need to change the sequence you have to search in your commands where the next step is triggered
    • it reduces (ie. destroys) reusability of said command
    • if you need to add a step in between you'll be adding it into the same command, which will end up monolithic

    Either I map the next command in the configuration sequence to the same event ( they will be called in order) or I use the macrobot utility which allows for sequencing and async commands et cetera.
    As it happens I've started an example for a more complex bootstrapping sequence, unfortunately I haven't had the time to comment it yet, but I think you'll get the general gist of it if you take a look:
    https://github.com/creynders/robotlegs-demo-bootstrap

  4. 4 Posted by svanny on 30 Jun, 2011 07:40 AM

    svanny's Avatar

    So one framework event can call multiple commands? Thats really cool. Before robotlegs I used PureMVC and that wasn't possible.

    So I could used something like this?

    commandMap.mapEvent( ConfigurationEvent.LOAD_RESPONSE , UpdateConfigurationCommand , ConfigurationEvent , true );

    commandMap.mapEvent( ConfigurationEvent.LOAD_RESPONSE , LoadFontsCommand , ConfigurationEvent , true );

  5. 5 Posted by svanny on 30 Jun, 2011 09:24 AM

    svanny's Avatar

    Ok, it seems to work.

    Back to my first case: I should delegate the loaded event to two commands, e.g. UpdateImageModelCommand and OpenPictureViewCommand, right? Or still LoadImageCompleteCommand in this case?

    Thanks again!

  6. Support Staff 6 Posted by creynders on 30 Jun, 2011 09:58 AM

    creynders's Avatar

    I'd definitely split it up into two commands.

  7. 7 Posted by svanny on 30 Jun, 2011 10:01 AM

    svanny's Avatar

    Ok, so in general the flow of my application is guided by the application context and not by the commands?

  8. Support Staff 8 Posted by creynders on 30 Jun, 2011 10:24 AM

    creynders's Avatar

    No, in general the application context does nothing more than fire up the bootstrapping command(s).
    Obviously this depends on preference, but most people use 3 bootstrap commands:
    PrepModelCommand
    PrepControllerCommand
    PrepViewCommand
    in which all mappings are defined.

    Personally I prefer having 4. The above 3 + PrepServicesCommand

    These bootstrapping commands are mapped to the ContextEvent.STARTUP event which is triggered by the application context.

  9. 9 Posted by svanny on 30 Jun, 2011 10:26 AM

    svanny's Avatar

    Ok, but then the application is guided by the PrepControllerCommand and not through the response commands, right?

  10. Support Staff 10 Posted by creynders on 30 Jun, 2011 10:50 AM

    creynders's Avatar

    Yes.

    TBH, in this case I would use a sequence command, instead of mapping commands to the same event, since there's a direct logical relation between the commands.

    I only map commands to the same event if they are not logically related and their order of execution is not important.

    In small applications it doesn't matter a lot, but in bigger ones you can easily loose track of what's going on and when, if you map multiple (related) commands to the same event.

  11. 11 Posted by svanny on 30 Jun, 2011 10:54 AM

    svanny's Avatar

    Ok, I understand. Thanks so far!

  12. Support Staff 12 Posted by Stray on 01 Jul, 2011 10:07 AM

    Stray's Avatar

    Excellent advice from Creynders: "I only map commands to the same event if they are not logically related and their order of execution is not important."

    The same holds true for event handlers in mediators - if two functions have totally separate concerns, map them separately rather than just having a clickHandler that calls both handlers. If the functions are co-dependent in some way, call them through a single handler.

    Intent, intent, intent... etc!

  13. Ondina D.F. closed this discussion on 02 Nov, 2011 05:23 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac