August 2004 Entries
Ice Cream Choices in Japan
I'm leaving for Japan this week, but I think I'll be steering clear of the Ice Cream. I wouldn't want to run into any of the treats in this Photo Journal by Ryann Connell at the Mainichi Daily News. The selections include Raw Horseflesh Ice Cream, Shark Fin Noodle Ice Cream and Oyster Ice Cream.
ASP.NET 1.1 Page Execution. Respect it.

There's probably been plenty written by this time on the execution lifecycle of ASP.NET v1.1 pages. However, I still run into a lot of issues and questions around what order events and methods get executed during the lifecycle of a request. Its essential to understand this order when debugging what can be complex bugs in Page and WebControl code.

Here's a short exercise that will help you internalize whats happening during the creation and execution of a page. Do this without looking up the answer, and it will really make you think through what is happening. Put the following events in order, assuming the request is a PostBack resulting from a Button click:

  • Unload
  • Pre-Render
  • Handle Postback Events
  • Dispose
  • Save State
  • Load View State
  • Load
  • Button Click Handler
  • Process Postback Data
  • Initialize
  • Send Postback Change Notifications
  • Render

You can lookup the answer in MSDN here, but realize that at least one of the events is out of order in the documentation.

Consider the events in you Global.asax class on the first request received by an application not yet started:

  • Authorize Request
  • Application Start
  • Authenticate Request
  • End Request
  • Page Execution
  • Begin Request
  • Session Start

The next time you try to access ViewState during the Init event, you'll know why it doesn't work correctly.

XML to Nant is like PB&J to Bread

Ok, that's probably a terrible analogy, but XML is what makes Nant taste so good. Why? Not because it makes build files easy to understand or read, because it doesn't. In fact, its pretty messy to look at all those tags and angle brackets.

If your not familiar with Nant, you can get familiar at this sourceforge site: http://nant.sourceforge.net

XML makes Nant cool because of the power of the transform. Being able to easily generate Nant build files using Xslt has benefited me again and again. Because C# project files are in an XML format, its no sweat to run a .csproj file through an Xslt and have it output a Nant build file with source files, references and anything else all intact. I have an application in development now that has several projects getting updated daily by various developers. Being able to generate new build files off of their recent changes saves a lot of time.

However, I've gotten to the point where I'm considering generating the files that generate my build files (yeh, two layers of generation). It appears to be the one way I can refactor the whole thing to make it easier to add projects to the process. Currently, there are three steps. 1) Get from source control 2) Generate build files 3) Build the application. We'd also like to have a fourth step to automatically deploy the application to a test server. The way its setup now, we have to add a project to each stage. My thought is to start out with a simple XML file with a list of the projects and generate the rest.

All this makes me wonder what patterns (not tools) are out there for structuring the build files for Nant. Any comments on how Msbuild will make organization of a large number of build projects easier?

ContextBoundObject and Orthogonal Design

Discovered ContextBoundObject a few weeks ago. Its been there in the framework all along but has remained elusive for me. I knew something like the ContextBoundObject was in the framework, since there was a lot of talk over Aspect Orient Programming just after .NET went live, but I just never made the connection.

When you create an object that inherits from ContextBoundObject, you have the ability to define rules and properties that apply to calls that cross the context boundary created for that object. What can you do with these rules and properties? Well, there's probably all kinds of interesting scenarios you can come up with. The most obvious are providing services such as tracing, security and validation for your classes. Implementing these services in the context of the object means the services do not impact the implementation of the object and the object does not impact the implementation of the services.

A ServicedComponent (Enterprise Services) is an example of a ContextBoundObject. All of those services that a ServicedComponent makes available are implemented seperately from your object. Likewise, you don't implement a lot of security/transaction/messaging code in your class. By inheriting ServicedComponents, these services become part of the context that your object is instantiated in.

The word for this type of seperation of responsibility is orthogonal, but I hate that word because I always have to grab a dictionary when I hear it. Encarta says orthoganal means “relating to or composed of right angles.“ That really clears it up, doesn't it? ;) Let's try a geek dictionary, wikipedia.com: “Orthogonality is a system design property which enables the making of complex designs feasible and compact. The aim of an orthogonal design is to achieve that operations within one of its components do not create nor propagate side-effects to other components. For example a car has orthogonal components and controls, e.g. accelerating the vehicle does not influence anything else but the components involved in the acceleration. It would be a non orthogonal design were for example the acceleration to influence the radio tunning, or the display of time.”

Imagine implementing security services without causing side-effects to the components that those security services apply to.  Tracing, validation and other services could also be implemented in an orthogonal way, rather than sprinkling code with helper statements. What other services can you think of that could be created orthogonally from the rest of your application?

I like the option of using a ContextBoundObject because I can get some of the flexibility of services without the overhead and deployment cost of Enterprise Services. Since ServicedComponents are also ContextBoundObjects, there is a nice migration path as well. Those “aspects“ that you implement for your ContextBoundObject should still be usable if your objects begin inheriting from ServicedComponent.

Is this an architecture/design pattern that you've used in the past? What have been your experiences with it? Is it something you could see as useful in the future? I've got some code snippets if anyone is interested in me writing more about the ContextBoundObject class.