Where do I put this code?

wagster's Avatar


21 Oct, 2011 07:17 PM

I have a service that loads up a swf and an xml file and sets them in a model. When they are both set, the model dispatches an event and I then need to recurse through the entire display list of the loaded swf, combining it with the xml to create a data structure of nested value objects.

The question is, once I have the swf and the xml in the model, what class does the recursing and combining? It's about 200 lines of code. Should it go into a command? After all "when" the data has arrived, "then" do something with it. Or if that's too heavy for a command, should I be doing it with a parser class? At the moment it's in a service, which works ok but doesn't feel right somehow.

Any hints?

  1. 1 Posted by Paul Robertson on 21 Oct, 2011 07:38 PM

    Paul Robertson's Avatar

    A common recommendation is to have a separate "parser" class that does
    the job of translating data.

    Depending on personal preference, you could inject the parser into the
    service, or have it be separate and call it from a command. So the order
    would be something like:

    (Injected into service):
    1. Service loads stuff
    2. Service gets stuff and passes it into parser
    3. Parser finishes, service finds out parser is finished (e.g. through
    an event), service dispatches "done" event

    (separate from service):
    1. Service loads stuff
    2. Service gets results of loading and dispatches event with payload of
    "raw" stuff
    3. Command gets event and passes "raw" stuff into injected parser
    4. Parser finishes and dispatches event with "polished" stuff
    5. Command gets event and passes "polished" stuff into model

    There are obviously variations in between, too.

    Here are some previous threads on this topic, if you want to read some
    smart ideas from Stray and Joel:



    Paul Robertson

  2. 2 Posted by wagster on 21 Oct, 2011 07:55 PM

    wagster's Avatar

    Thanks - interesting discussion there between Stray and Joel, making the point that services should be closely coupled as they generally do their own processing. I think in my case it is quite important to decouple the loading and processing as both take a relatively long time and stuff has to be done inbetween. The point about injecting a parser into a command is a good one though. I think I'll do that.

    I seem to have taken against data payloads for no good reason except that now I have model classes, and to keep everything consistent I set data in models and let the models dispatch small events letting everyone know they have goodies for them.

  3. Support Staff 3 Posted by Ondina D.F. on 01 Nov, 2011 02:15 PM

    Ondina D.F.'s Avatar

    Feel free to reopen this discussion in case you have more questions or you need further assistance with this issue. Please open new threads for new issues.
    Thank you for posting

  4. Ondina D.F. closed this discussion on 01 Nov, 2011 02:15 PM.

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

Keyboard shortcuts


? 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