nwebb

Flex, Flash, AIR

The Presentation Model – post #1

I read Paul Williams' post about using the Presentation Model some time ago, but ran in to a couple of issues when trying to put it in to practise in a real-world project. Although I still use PMs, they are sometimes really helpers that are not quite following all the rules. David Deraedt also has a series of posts about using the Presentation Model and mentions issues he encountered (specifically using it alongside Cairngorm).

I won't go in to details about why I want to separate my code this way (the above links cover all of that), but I thought I'd take a fresh look and so I've gone back to basics by creating a really simple app based not on the demo app, but rather on a single bit of functionality from Martin Fowler's example here:

http://www.martinfowler.com/eaaDev/PresentationModel.html

All the app does is set the enabled status of the TextInput, based on the state of the CheckBox.
(Note: I've not bothered creating the Album DTO or storing the composer string, as they weren't strictly necessary and I wanted to keep the file as basic as possible. For the same reason I'm not dispatching custom events either, so for this example every binding will dispatch a PropertyChanged event.)

Hopefully my interpretation is correct - nearly all the comments in the code are copied/pasted directly from the Martin Fowler article so you can follow along. I've also set the initial value of the checkbox to true and used binding to provide the 'updating of the view' from the model.

If you've not come across the Presentation Model before I would suggest starting with this example by looking at the CheckBox in View.mxml, and then following through the process of what happens when a user clicks the CheckBox (this way the comments should be in some order).

Here is the example. Right-click for source code:

Get Adobe Flash player

Presentation Model & 'Global' Variables

I just wanted to air one additional thought here, as I'm working on a modular project which doesn't use an established architectural framework (i.e. Cairngorm, Mate, PureMVC) but has been set up in such a way that common/shared objects are retrieved using something similar to Cairngorm's ModelLocator. Essentially any module can get hold of shared data using sytax similar to:

Actionscript:
  1. Application.application.getSharedThingy([identifier]);

Now I don't want to get in to a discussion about Singletons or global vars here - as a contractor working on someone elses project, some projects are architected to depend on globals and there isn't always the time or budget for a refactor - I think this is just a reality of the job. However, one great thing about Flex is that general views can be knocked up in MXML in minutes (anything you want to reuse is a candidate for a component or helper class). Therefore, as far as code-reuse goes for Flex views goes, I'm really not that bothered about having a reusable view.

The great thing about the Presentation Model is that it forces us to remove all non-view logic in to its own file. The resulting PM should be encapsulated, easy to unit test and have no knowledge of the view (the view does have knowledge of the PM). If I start sticking references to globals/Singletons in the PM, I break some of those rules. However, if I see my View as something disposable, why not access the singleton/global objects from within the view, and then 'inject' them in to the PM?

So for example, my PM could have a setApplicationPayload() method, and my view would get the payload object and set it on the model:

Actionscript:
  1. pMod.setApplicationPayload(Application.application.getDataObject([identifier]));

I'm not saying this is the way to go, but it does make working with globals a little better - it was just a thought and I'd be interested to hear your feedback on things you do to make using the Presentation Model setup easier.

2 comments

2 Comments so far

  1. nwebb » The Presentation Model - post #2 June 28th, 2009 12:20 pm

    [...] my last post I offered up a very simple example of the Presentation Model. Below is a simple, but slightly more [...]

  2. [...] example of using Flex and the Presentation Model together is more complicated than the last two but similar enough that you can progress through from the previous example apps. Rather than [...]

Leave a reply

Bad Behavior has blocked 517 access attempts in the last 7 days.