Web applications are typically built over the web frameworks like Spring MVC, Struts, ASPNET MVC, etc. With most web frameworks it is pretty straightforward to centrally handle cross-cutting concerns like exception handling, logging, database transactions, statistics gathering and even some domain specific concerns. These can be implemented because of simplicity of HTTP and robust design of the web frameworks. Use of dispatcher servlet in Spring MVC being one case in point (http://static.springsource.org/spring/docs/2.0.x/reference/mvc.html). On the other hand the developers of rich client applications do not have such luxuries. There are no in built mechanisms Swing, SWT, Win Forms, WPF, Silverlight to achieve something like this. A simple example being "displaying hour glass to the user while his request is being processed". Presentation layer aspects essentially borrows the same technique, of getting to the root of call hierarchy and providing hooks to handle such concerns. This doesn't work as neatly as web frameworks as there is no single root of all invocations rather multiple controls originating their own event handlers.
Example of the problem
{{Code showing Button.Click handling in C#, WinForm}}
In this example we are changing the mouse cursor to hour glass while action is being performed. We are also catching any exception and display a friendly error message to the user. The issue is that if we have to do this for other user actions, we would have to repeat the same kind of code in every event handler.
ControlSuperTypes decorating event origin
{{Code showing overriding of OnButtonClick method and handling cross-cutting concerns}}
ControlSuperType borrows its name from LayerSuperType. Unlike LayerSuperType it doesn't apply to entire layer rather only to a specific control type). For each control type of interest one needs to define a class. The other thing to notice from above code are the handlers for each class of concern, WaitNotificationHandler, EventExceptionHandler and so on.
I am not sure whether the ability to override the events are incidental or designed in the frameworks like WinForm/WPF. My suspicion arises from the fact that this cannot be done for all events. {{Provide an example of this}}. In such cases we can use custom events wired to the in-built ones.
{{Code showing custom event}}
While some AOP approaches and frameworks can also be applied to achieve the same but I prefer this approach for following reasons:
Simplicity I prefer not using AOP frameworks whenever there are other easier ways to get the same affect. Even best applications of AOP frameworks cannot avoid the complications introduced in debugging and re-factoring such code.
Lack of framework options In WPF/Silverlight world I have found it difficult to find a decent open source AOP framework which can solve this problem.
Notes not part of the book
AppDomain exception
Hour glass is also important when using testing framework like white which...