October 13, 2006

Windows Vista as a Development Platform

As Windows Vista RC2 hit last week I've come across several mildly disturbing articles regarding the integrity of Vista as a development platform. First off was the announcement of SP1 Beta for Visual Studio 2005 on Somasegar's blog. Sounds like good news, right? Well, further down he also reveals that Vista will not support Visual Studio 2002 or Visual Studio 2003. In addition, he admits that Visual Studio 2005 will most likely suffer from compatibility issues beyond those addressed in SP1. In my mind this doesn't communicate strong support for what is supposed to be Microsoft's flagship development platform. I imagine this issue could easily stop most developers from trying Vista anytime soon due to the fact that they still need to continue supporting older applications via older versions of Visual Studio.

There have also been rumors going back and forth debating Java's ability to keep up with the new "Aero" look and feel in Vista. Thankfully, it appears as if this will only be an issue for older versions of Java.

On a positive note, IIS 7 in Vista will finally elevate PHP to a first class citizen. The newest version of Microsoft's webserver is on target to introduce a FastCGI host. Such a change will honor the single-process-per-request execution model of PHP, but do so much more efficiently than traditional CGI. For more information check out this blog entry by Mike Volodarsky (member of the IIS team).

September 23, 2006

The C# Coalesce Operator

This week I was excited to learn about the coalesce operator, a feature new to C# in .NET 2.0. Coded as a double question mark (??), it works by yielding the first non-null item from a group of two or more objects (as you might expect, this behavior is very much like the COALESCE function in MS SQL). Using this operator can help produce more elegant code if, for example, you've got something like this:
if (object1 != null)
{
return object1;
}
else if (object2 != null)
{
return object2;
}
else if (object3 != null)
{
return object3;
}
else
{
return null;
}

With the coalesce operator the above code block can be refactored into the following:
return object1 ?? object2 ?? object3;

August 23, 2006

Using FontInfo in a Custom Server Control

In many circumstances it's appropriate to assign a FontInfo structure as part of a custom server control. This easily allow programmers to adjust the look at feel of certain areas of text but can be a little tricky to implement because FontInfo doesn't have a public constructor. One way to accomplish this is to create a CSS Style object and use it's Font property to do the dirty work. This technique would look something like this:
private Style _headerFontStyle;

[Bindable(true),
DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
NotifyParentProperty(true),
Category("Appearance"),
Description("The font for the header."),
Localizable(true)]
public FontInfo HeaderFont
{
get
{
if (_headerFontStyle == null)
{
_headerFontStyle = new Style();
}
return _headerFontStyle.Font;
}
set
{
_headerFontStyle.Font.CopyFrom(value);
}
}

So is that all there is to it? Well, not exactly. Unfortunately, FontInfo is not serializable, nor does it implement IStateManager making it difficult for us to track it's state if any changes are programmatically made to the structure.

The good news is that in the code above we're using a Style object to get at the underlying FontInfo. Since Style implements IStateManager we can override the following methods to make sure that the FontInfo property is always kept in ViewState across post backs.

protected override object SaveViewState()
{
object[] state = new object[2];
state[0] = base.SaveViewState();
state[1] = ((IStateManager)_headerFontStyle).SaveViewState();
return state;
}

protected override void LoadViewState(object savedState)
{
object[] state = (object[])savedState;
base.LoadViewState(state[0]);
((IStateManager)_headerFontStyle).LoadViewState(state[1]);
}

protected override void TrackViewState()
{
base.TrackViewState();
if (_headerFontStyle != null)
{
((IStateManager)_headerFontStyle).TrackViewState();
}
}