Lately, I have been revisiting Flex application architecture design instead of blindly following a particular framework's guideline. The basic Cairngorm framework was never renowned for its architecture even with UM's extensions to make it usable. I have moved on to Swiz last year and have been loving it dearly. However, Swiz still promotes an implementation of MVC architecture not so different from Cairngorm. Some of these practices were questionable to me and have been bothering me for quite a while.
The biggest of these concerns was the fact that both frameworks in Flex promote what Martin Fowler calls the 'Anemic model'.
http://martinfowler.com/bliki/AnemicDomainModel.html
I do know that most frameworks in Flex are designed for enterprise applications where the backend has the domain logic to manipulate the data and the frontend merely displays the data returned. When you look at the big picture, Flex applications are only the view of the entire application with the controller and model on the server. A separate domain model on the client would only make things more complicated.
On the client MVC, model is used exclusively for storing client state. The client is merely determining which backend API to call in response to a user action thus the logic simple enough to reside on the controller as a transactional script while the more complex, UI logic is solved by using the presentation model pattern. Sometimes user interface elements can be have very complex logic which is then encapsulated in components.
Now that was the typical case of Flex being used for enterprise applications. If you need a self-contained AIR application with SQLite or don't have the liberty of adding/changing the backend services, everything starts to fall apart. A lot of domain logic would end up in the controller you either end up with a copy&paste mayhem or have many "helper" objects containing the logic that really belongs in the domain model. These are essentially procedural scripts manipulating the model that run in response to an event and go againt the whole idea of OOP.
For example, to update a Person model using a CakeVO using anemic model.
EatCakeCommand.as
...
if(person.foodVolumeInStomach + cake.volume <= Person.MAX_STOMACH_VOLUME)
{
person.foodVolumeInStomach += cake.volume;
person.foodInStomach.addItem(cake);
if(person.foodVolumeInStomach > Person.MAX_STOMACH_VOLUME * 0.7)
person.hungry = false;
if(cake.isOff)
person.sick = true;
}
...
Instead of encapsulating the logic associated with eating inside the person and simply doing:
EatCakeCommand.as
...
person.eat(cake);
...
In these circumstances I feel that Swiz does not provide a good solution so I either have to ignore the Swiz's guideline or to use PureMVC and give up easy binding and/or dependency injection available in Swiz (I use PureMVC in Flash projects).
Maybe it's time I consider using a third generation frameworks like Robotlegs.
Sunday, January 17, 2010
Subscribe to:
Comments (Atom)