explorers' club

explorations in dev, science, sci-fi, games, and other fun stuff!

Where to ‘inject’ and where to ‘massage’ data in Cairngorm


I have been using Cairngorm for a little under one year both in Flex 1.5 and Flex 2.0, in various projects of varying scopes. There are a few questions that have come up but have never had any ‘real’ answer from another person. Some of the following questions may overlap or simply be rephrased differently. I will also follow up

  1. At what point in the command structure is data ‘injected’? This may seem painfully obvious in theory but in execution it can be somewhat unclear. Depending on the type of application, who is calling (or rather who is dispatching an event that the Controller listens to), what level of complexity of data, etc.. The point is that typically the event’s data is ‘injected’ at the UI level. For example, we would take certain data values from comboBoxes, numericSteppers, etc. to add to a ValueObject to pass as the data of the event. I will come back to this point later
  2. Is there a hierarchy for who handles the data passed through the command chain? Again, maybe it is painfully obvious, but again, depending on the situation who does the bulk of the ‘data creation’ versus who does a little bit of ‘data massaging’. I would suggest that indeed there is a hierarchy. UI => Command => Delegate. You may be thinking, well of course that is the order but I have a few issues with this order. For instance, let’s say you have a view dispatch an event, passing many parameters needed for the call, but you also have to grab some one piece of data from your model. Some may suggest that you would simply create the VO with the needed data in the UI, pass the parameters in the CairngormEvent (or subclass), then take those values in the command, and then pass it to the delegate which gets passed as the call param. This just doesn’t settle well with me. Firstly I don’t like having the UI getting things from the model to add to the UI. It should only be concerned with the data within the view itself. In this situation, I’d prefer to have the VO to begin its life in the UI, get passed to the command via the event.data, then the command ‘massages’ the VO by getting what is needed from the model before getting passed to the delegate. That way the VO’s life span is for the whole execution part of the event/command but you don’t have the UI peeking into the model. Is that wrong to feel that way?
  3. Should the UI be able to peer into the model for the needed data to pass with the event? Touching on the point above, I would say no but I would enjoy others’ input.

Maybe I am just way to anal about this but I think it is important to at least establish some good ‘rules of thumb’. It seems that when I bring this up with my coworkers, they haven’t given it much thought but agree that it should be a topic of discussion. Of course going down this route means that the developer would have to make good use of extending the CairngormEvent and also creating the respective ValueObject subclasses. If you simply pass generic objects, then you defeat the purpose of being able to ‘massage’ the data carefully. Plus you loose Flex 2’s ability to give code hints for the parameters.



6 thoughts on “Where to ‘inject’ and where to ‘massage’ data in Cairngorm

  1. I have given this more thought and I am leaning towards having the Command act as the ‘assembly point’ for all needed data. So the UI would pass the needed data in a raw format via the CairngormEvent’s subclass’s properties (which are tailor-made for the particular event occuring). Then the Command would look at the Event’s properties, create a new ValueObject subclass, grab the needed data from other points (like the model) and finally pass it on to the delegate’s methods.

    Of course this too doesn’t sit well with me because then you are only utilizing the ValueObject through one data transition. In addition to that last point, I really like the idea of utilizing ValueObjects for more than parameters to be passed to calls.

    Take for instance a view with several controls (2 comboBoxes, 5 text fields, etc.). I really like this idea of having a getter/setter property that takes/returns a VO. This way when setting that property, you can pass that VO to the view, which the setter looks into to set various more granular properties (such as the aforementioned controls). The reverse could happen on getting that value, the getter looks at the granular properties of the controls then returns a VO based on that. From there it could be bounced around or sent off via a call.

    Who knows. Maybe this is the standard in which things work and I have been in the dark. I just know that this is underutilized in the projects I have worked on in the past.

  2. You bring up valid points that should be considered when using the framework.

    Personally I like to pass VOs into Views, then have the view do what it wants with that data. I prefer this way because it makes life simpler if you want to add a variable to the view. Rather than having to create new setter/getter functions, all you do is add a class variable to the VO.

    So each of my Views has data encapsulated in a VO. The view updates the VO as necessary, then sends it via an event to a Command. There the Command can inject data or simply pass the VO to a Business Delegate.

    I have two more questions which I would like your views on:

    1. Is there a point when a Command contains too much logic? In this case, should helper class be created to assist commands?

    2. Say I have one HTTP service that can perform different actions based on paramaters based to it (e.g. mydomain.com/getdata.php?action=action1 and mydomain.com/getdata.php?action=action2). Should I create 2 Business Delegate classes, one for each action? Or should I just have one because it is essentially the same service. This becomes more complicated when the service performs, say, 10 actions. You end up with 10 methods in the Business Delegate, one for each service action. Is this acceptable?

    What do you think?

  3. Hey Ed. Sorry I just now saw the comments. Now to reply to your questions:

    1. Yeah, I think there is a point when you should consider breaking a command down into smaller bits. How that is approached is innumerable. I would make the assumption that if there is a whole bunch of logic in a command somewhere, then there is a high chance that some portion of the logic is or could be handled elsewhere and be reused by other commands or classes. No point in code duplication. But generally I would rather have the logic (if it isn’t being reused) to be in one place which is the command.

    But let me pose this to you: You have an application that saves data either to a local shared obj or to a database. Where it is to be saved is determined by seeing if a user is logged in or logged out of the app. The logic for sending the data is the same in either case. So do you A) have the view see what the log in status is, then call an appropriate command such as saveToLSO or saveToDataBase? or do you B) have one command that chooses the appropriate delegate and has just one place for the needed logic?

    2. I generally like the idea of having less places to go and more to do than more places to go and less to do. Meaning, I’d rather have a delegate with half a dozen related methods than half a dozen related delegates with one or two methods. But both approaches would be acceptable in my book.

  4. Thanks for your reply. Only just found it! Maybe a comment subscription plugin would be handy :).

    I’m not sure if your question is rhetorical, but I would go for option B. The command chooses which delegate to use. If you have multiple views each with logged in/out checks (as in option A), that would be code duplication. Having one command manage it seems simpler in my opinion.

    But then again there is a time and a place for everything! Maybe option A would be better in special circumstances.

  5. Ed,

    The question was not rhetorical. I too opt for option b). As for the comment plugin, I am using the free wordpress blog right now which affords me very little in the way of customization. I am working on getting hosting for my own sight. Its just hard to find the right place since I want SVN on the server along with other options.

  6. Pingback: Border Crossing Stats » Where to inject’ and where to massage’ data in Cairngorm jwopitz …

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s