Sunday, 18 January 2009

MVC / MVP in RIA programming

I have been working on an application where Flex is being used as a front end for a Java middle tier and Oracle database back end. In theory, the Flex code is all about the user interface / experience. The Java code is dealing with the business logic. So where does MVC come into this environment?

The bulk of our commands are designed to initiate service requests to the middle tier. In effect, they build our model. Our model primarily reflects the business model represented in the underlying database although extended to make it easier to perform some of the interface interactions. Does this mean that MVC in this context is the distribution across the layers or there a place for MVC or MVP in the rich internet application (RIA)?

As my thinking has clarified, I have become more convinced that MVP has its place in the user interface layer of the RIA. I see code where the user gesture has been captured and the view updated without going to the model. In some cases, there were resulting problems because of the tight coupling. In one such case, the simple solution was to create a command that updated the model within the RIA and then allow the view to update through the data binding mechanisms.

This isn't a sudden realisation that maybe we should have been writing our code this way all the time. My first piece of Flex code using the Cairngorm framework operated this way. I didn't have a middle tier or a back end. I simply had the memory based model that I was updating as user gestures required. Each user gesture cause a command to be activated which updated the model and the resulting changes where reflected in the view through data binding.

My view simply comprised an MXML file with a very small amount of ActionScript. That has changed with my current work in that the view comprises an ActionScript class (the mediator) that inherits the base MXML component used to build the view, and an MXML class that inherits the ActionScript class. This means that the MXML has full access to all of the public and protected variables and methods of the ActionScript class. In my coding, I have ensured that the ActionScript class knows nothing of the MXML component. This approach leads to easier unit testing. The MXML component only contains ActionScript within the property settings of the components added to the component container. We do use renderers, formatters, and validators but the basic principle is tht these can be tested independently of the MXML component. The MXML mediator acts as a staging area to gather together the various data required to complete the major user interface action.

Each user interface action is carried out through a command. In some cases, this might involve a series of commands that initiate service requests to the middle tier but the command or commands could simply initiate updates to the internal model. These changes are reflected back to the MXML through data binding.
The model is both the domain model and a presentation model. In some cases, the presentation model inherits a domain object and extends it to achieve the requirements of the interaction. In effect the model represents the current state of the interaction.

An example of how this structure impacts coding is the implementation of a select all check box. Each row on the data grid has a check box but some are disabled because of the status in the underlying model. The header has a check box that can be used to select all enabled rows in the data grid (more correctly the eligible data objects in the underlying model.

The original implementation attempted to mange this selection and tracking of status in the renders for the header check box and the row item check boxes. It appeared to work until a different sort order was chosen and then the selected check boxes no longer corresponded to the rows that should have been selected.

My solution was to have the underlying model remember what was selected or not selected. This included the status of the select all check box. When the select all check box is selected, it fires off a command that updates the underlying model. Data binding looks after updating the view and ensures that the correct check boxes are selected.

The core class to achieve the select all is below. There is a comment where the dispatch of the command should occur.

import flash.events.MouseEvent;

import mx.controls.CheckBox;
import mx.controls.DataGrid;
import mx.controls.dataGridClasses.DataGridColumn;
import mx.events.DataGridEvent;

public class CheckBoxHeaderRenderer extends CheckBox
{
public function CheckBoxHeaderRenderer()
{
super();
}

private var _data:DataGridColumn;

override public function get data():Object
{
return _data;
}

override public function set data(value:Object):void
{
_data = value as DataGridColumn;
DataGrid(listData.owner).addEventListener(DataGridEvent.HEADER_RELEASE, sortEventHandler);
selected = ApplicationVariables.isSelected;
}

private function sortEventHandler(event:DataGridEvent):void
{
if (event.itemRenderer == this)
event.preventDefault();
}

override protected function clickHandler(event:MouseEvent):void
{
super.clickHandler(event);
// Dispatch of event to initiate command to update model
DataGrid(listData.owner).invalidateList();
}

}

We haven't been consistent in this implementation and it shows in the difficulty of maintaining some of the code. The solution to the select all check box problem can be applied to other problems where one component has to be updated because of an event caused by another component.

As I reflected on this solution, I thought of the idea of a metaphor promoted by the extreme programming community. The metaphor provides a framework for thinking about solutions to the particular application problem. The more that I think about Flex coding, I am thinking of it as a series of commands initiated by events. Sometimes the series of commands might be initiated by a particular user gesture or may a gesture simply fires one command.

Games and Religion

This entry is the result of a recent discussion in the Games Network mailing list on research into the way in which religion is portrayed in games. Most of the games that I play are of a strategy type and if religion is included, it is simply through some of the symbolism (i.e. the cathedral in Carcassonne).In these cases, there is no attempt to communicate a perspective about religion.

In a game like Civilisation IV, seven religions are represented but they are simply represented as opiate to the people. That is it improves their happiness and consequently leads to a more settled civilisation. It isn't what I would call a wonderful portrayal of faith.

However, there is another category of games that claim to foster improved Biblical knowledge. These follow an adventure game style with the player exploring the game environment to collect various objects. Along the way, questions on Biblical topics are asked. Objects in the game represent biblical objects or characters. However, the game play has little to do with faith or the depicted religion. These games are about entertaining the player in a religious context. On one site promoting one of these games, the key promotional phrase is “Conquer the Canaanites, Amorites and Hittites as you collect silver and gold, race through deadly mazes, and defeat the enemies of God!” Sure the main character of the game, Joshua, did destroy other nations but the representation in the game doesn't depict the setting in which Joshua found himself or help the player understand why for Joshua and Israel at that time, this seemed to be the appropriate action.

In my opinion, these games do more harm than good. The authors have a very narrow agenda and they haven't really thought through what it means to have faith and how to communicate the real meaning and principles of faith. When I look at the Joshua game, I don't see it as having changed much since the early versions that I reviewed and played in the late 1908s. I didn't find them explicitly Christian then and I still don't now. They are little more than secular games give a biblical story wrapping. Where do these games provide the challenge to applying the foundational principles of faith to the everyday interactions of life? Maybe the authors of these programs really don;t know their faith or the person whom they claim to follow.

A more interesting project is the "The Night Journey." On their website, they say“The objective here is to explore how to represent the issues of personal enlightenment through a game. On the web page, they say “what is the "game mechanic" of enlightenment? How can we abstract and systemize such an intensely personal, yet archetypal experience?” This should be the same challenge that should face all Christian game authors. How do we represent a growing personal relationship with a living saviour? How can we bring the challenge of that trust and faith relationship that involves recognising his personal guidance in our lives into a game scenario?

It is easy to write a trivia game that encourages people to learn scripture stories. It is a completely different issue to communicate principles and truths that God seeks to get across through his relationship with us.

Sunday, 11 January 2009

Speaking with Authority

In Barclay's (1975) commentary on Luke 4:31-37, he talks about Jesus speaking with authority. Jesus didn't say “Rabbi so and so says,” he said “I say to you.” His authority came from himself. Jesus spoke as one who knew.

In research writing, the emphasis is placed on the authority of prior research papers and the interpretation of the data in the light of accepted research methods. Authority is gained through the references made and the acceptance for publication of your work by the research community. It is unacceptable to say that this is my understanding or from my perspective. This method of recognising the authority of work is a protection against fraudulent research and a technique for ensuring that the researcher has read the literature in the field.

Yet, this method of providing authority does cause problems when innovative solutions are proposed. The tendency is to accept work that conforms to the current understanding and direction of research in the field. Anything that challenges that current understanding or direction of research is likely to be rejected until they have built up sufficient support to be able to get their work recognised. Kuhn (1996) describes some of these issues in his writings.

In my research based on phenomenography, which assumes that different people will see or understand a phenomenon in different ways, I would like to argue that each researcher interprets their data based on their understanding of the phenomenon understudy. Certainly, I am conscious that my results are an interpretation of the interview data based on what I saw in that data. Yes, I endeavoured to follow the analysis methods of phenomenography and I revisited the data many times endeavouring to look at it from different perspectives. However, all of those ways that I looked at my data depended on me recognising that they were possible ways of looking at the data. Sure my reading helped broaden the possibilities but in the end, if I was blind to a particular perspective (I didn't know of that perspective, I don't know what I don't know (Armour 2000)) then it wasn't included in my analysis. For a particular approach to be taken then the researcher has to be aware of that approach. A lack of awareness means that it won't be included in the study.

There is another claim to authority that I want to look at and it is one that I have found reoccurring as I return to work in industry. It is a claim along the lines of “This is the way that it is done in this language.” Often what is meant by the person expressing this claim is that “This is how I do it in this language” or “To the best of my knowledge this is how to do this in this language.” The difficulty with the original claim is that there is no room for different ways or different understandings. It is making an absolute claim that leaves the hearer with no opportunity to propose an alternative. The other issue is that it actually lacks any supporting authority at all. The speaker isn't saying it is based on their experience or authority and they are providing no other source to support their claim.

When a person says “this is my understanding” or as Jesus did “I say to you,” they are clearly placing emphasis on their own experience and knowledge. Whether we accept that person's claim will then depend on our acceptance of their authority. In the case of Jesus, his authority to speak was rejected by those who knew his background but accepted by those who heard and saw what he did. When we speak from our own authority, we need to ensure that we are backing up what we say by what we do. If I claim a particular way of doing things in a programming language or as a result of my research, I better be able to show that my approach to programming works and that my results can be seen for what I claim them to be. If that doesn't happen then I have no grounds for claiming authority.

References

Armour, P. G. (2000). The five orders of ignorance: Viewing software development as knowledge acquisition and ignorance reduction. Communications of the ACM, 43(10), 17-20.

Barclay, W. (1975). The gospel of Luke (revised ed.). Edinburgh: The Saint Andrews Press.

Kuhn, T. S. (1996). The structure of scientific revolutions (3rd ed.). Chicago: University of Chicago.

Sunday, 4 January 2009

Convention versus patterns

With Ruby on Rails, one of the catch phrases has been “convention over configuration.” They argue that it is preferable to use standard conventions or approaches to implementation rather than utilise complex configuration options. This promotes faster development and more consistent systems. Most of the time the configuration comparisons have been with Java stacks where there are multiple layers all having to be configured to talk to one another. Programming a Flex user interface for a Java middle ware running in a JBoss server with an Oracle back end database with PL/SQL stored procedures, I am conscious of some of the configuration issues and the problems that occur if you get it wrong.

A convention aims to reduce the amount of set up required and to improve the development time. The convention tends to enforce certain implementation structures of the programmer. In the case of Rails, this tends to be CRUD (Create, Retrieve, Update, and Delete) style applications.

The more general meaning of convention is the notion that “this is how we do it.” When I taught DotNET programming, most of the available introductory text books used the DataSet as the means to access and manipulate data. This removed the notion of a domain model but made it fairly easy to write CRUD applications (I used to say CRUD by name and crud by nature). As a solution technique, the DataSet has its place and the programming conventions that developed around its use also have their appropriate context and problem space. The issue is that the convention isn't applicable in every circumstance.

Working in Flex but having done most of my thinking with respect to object-oriented with Java and the DotNET languages, I am finding that my approaches are often being challenged as “not the way that it is done in Flex”; that is I am not conforming to the Flex convention. The simplest case is that I tend to implement properties using get and set functions. It is quicker to write public variables but as an object-oriented convention that is frowned upon. In Java and DotNET, you can also use public variables but the clear message is to use getters and setters. I have simply transferred that thinking to Flex. Although I have to write more code, I would argue that using getters a setters potentially gives me better control.

I have previously argued that the MVC frameworks (Cairngorm and PureMVC) are more about conventions rather than the actual code framework that they provide (see post Flex MVC Frameworks). In relation to Cairngorm, those conventions can make it difficult although not impossible to write tests for and can add to the maintenance difficulties.

My thought is that if conventions are based on implementation patterns then those conventions will probably prove more durable. If a pattern has been defined correctly or at least following the convention for patterns then it should capture the common solution to a reoccurring problem. The pattern definition should provide the context in which the pattern is applicable, the problem to which it applies, the forces that are present in that situation, the essence of the solution, and the resulting context (both positive and negative consequences).

If conventions were defined in this way then a person looking at a convention would be able to tell whether that convention was appropriate for their situation. The problem is that a convention carries the stigma that this is the way that it is done.

The problem with frameworks, even though their original development may have been based on patterns, the way that they are used and talked about is often as conventions. But each framework has its restrictions and advantages. The user needs to be conscious of these when making the decision on which to apply.

The convention is a solution to a common problem but the programmer has not been made aware of the problem or the context in which it applies. Further the programmer is not provided with the resulting context especially the negative consequences.

If conventions were written up as a pattern language then the applicability of the conventions and their appropriate use would become more visible.