Monday, October 10, 2005

For those ardent followers of my blog, my apologies for the noticeable absence.  I've been out of town working on the next release of our software package and that has consumed every single waking hour (from 8:00 AM to 3:00 AM) each day.  So while I've had a mountain of ideas and concepts to blog about, time has simply not been on my side.  Today, however, I find myself with at least a breather.

I'd like to comment on a concept that I actually formulated and coded a few months ago, and always meant to blog about but simply never got around to it.  In fact, a little over a year ago, I blogged about serializing a class from Xml to simplify reading Xml elements from a .config file.  The issue with this particular approach is that the data doesn't roundtrip; that is, the Xml doesn't ever get written back to the .config file.  Sure, there are ways to do it, but my solution didn't address any of those techniques primarily for one reason: I'm not of the camp that believes the .config file is for storing user preferences.  Config files are there so that an application can be tweaked to operate with a set of predefined, but customizable parameters.  They are designed to be set before the application starts and remain that way.

User settings, on the other hand, are preferences that the user may customize from usage to usage.  For that we need a mechanism separate from, but sort of related to, the .config file.  If you're a user of the .NET 2.0 Framework, you may already be familiar several of the new classes in the System.Configuration namespace (namely the SettingsBase, SettingsProperty, SettingsProvider, et al).  These classes facilitate a very similar set of functionality to what I've created, but using a different mechanism.

The primary issue I set out to resolve for my particular applications was that I wanted to create a mechanism through which I could load and read user settings/preferences and simultaneously be able to persist their preferences back.  This is pretty trivial in concept but there can often be hiccups and gotchas in real world scenarios.  For example, suppose your application is being run in a heightened security zone (such as the intranet or internet zones).  If you want to persists user settings to disk you can't simply open a pipe to the root c:\ and start writing.

To this end I created a set of utility classes that encapsulate the logic needed to store and retrieve settings.  The principal class is called UserSettingsManager.  The UserSettingsManager is responsible for loading, saving, and deleting user settings.  For simplicity (both in implementation and details) I chose to rely on XmlSerialization for the storage of user settings.  I also decided to store user settings using the System.IO.IsolatedStorage classes for a few reasons:

  • The location of the file is something I don't have to worry about (the OS handles that for me) - all I care about is the file name - even then, I only care that the information can be retrieved and persisted.
  • I can make settings roaming if roaming profiles are used.  That way, settings move with the user as he logs in to different computers.
  • The files are just that: isolated.  I can only see settings pertaining to my application/user.  Isolated storage is a protected (but not encrypted) storage mechanism when using managed code.
  • Users/applications with lower privileges on the system can still save and read data from IsolatedStorage.

In order to make it generic, I also created a marker interface (that is, an interface with no methods) called IUserSettings.  Implementing classes can then be passed to the Save() method or retrieved via the Load() method in a generic way.  Classes that implement this interface should also support XmlSerialization (possibly using XmlRootAttribute and friends) because the UserSettingsManager relies on XmlSerialization to save and read the settings.

Lastly, I created an attribute called UserSettingsFileAttribute that can applied to the assembly.  This attribute simply designates the name of the file used for storing user settings.

That's pretty much all there is to it.  Here's a little run down of the code (without comments).  You can download the full source complete will comments here.

[AttributeUsage(AttributeTargets.Assembly, AllowMultiple=false)]
public sealed class UserSettingsFileAttribute : Attribute {
   private string _settingsFile;

   public UserSettingsFileAttribute(string settingsFile) {
      _settingsFile = settingsFile;
   }

   public string SettingsFile {
      get { return _settingsFile; }
   }
}

///////////////////////////////////////

public interface IUserSettings { }

///////////////////////////////////////

using System;
using System.IO;
using System.IO.IsolatedStorage;
using System.Reflection;
using System.Xml.Serialization;


public sealed class UserSettingsManager {
   private UserSettingsManager() { }

   public static void Save(IUserSettings settings) {
      Type type = settings.GetType();
      XmlSerializer ser = new XmlSerializer(type, string.Empty);

      using ( IsolatedStorageFile file = getIsolatedStore() )
      using ( IsolatedStorageFileStream fs = new IsolatedStorageFileStream(getFileName(type), FileMode.OpenOrCreate, FileAccess.Write, FileShare.None, file) ) {
         ser.Serialize(fs, settings);
         fs.SetLength(fs.Position);
      }
   }


   public static IUserSettings Load(Type type, out bool createdNew) {
      XmlSerializer ser = new XmlSerializer(type, string.Empty);
      IUserSettings settings;

      try {
         using ( IsolatedStorageFile file = getIsolatedStore() )
         using ( IsolatedStorageFileStream fs = new IsolatedStorageFileStream(getFileName(type), FileMode.Open, FileAccess.Read, FileShare.Read, file) ) {
            settings = (IUserSettings)ser.Deserialize(fs);
            createdNew = false;
         }
      }
      catch ( FileNotFoundException ) {
         ConstructorInfo ci = type.GetContructor(Type.EmptyTypes);
         settings = (IUserSettings)ci.Invoke(null);
         createdNew = true;
      }

      return settings;
   }


   public static void Delete(IUserSettings settings) {
      Type type = settings.GetType();
      using ( IsolatedStorageFile file = getIsolatedStore() )
         file.DeleteFile(getFileName(type));
   }


   private static string getFileName(Type type) {
      Assembly asm = Assembly.GetAssembly(type);
      object[] usfa = asm.GetCustomAttributes(typeof(UserSettingsFileAttribute), false);
      if ( null == usfa || 0 == usfa.Length )
         throw new ArgumentException(“Unable to resolve the user settings file name.  UserSettingsFileAttribute is undefined.“);
      else
         return ((UserSettingsFileAttribute)usfa[0]).SettingsFile;
   }


   private static IsolatedStorageFile getIsolatedStore() {
      return IsolatedStorageFile.GetStore(IsolatedStorageScope.Assembly | IsolatedStorageScope.User, null, null);
   }
}

Once incorporated into your project or into a utility assembly you can implement it as simply as the following:

[XmlRoot("userSettings")]
public sealed class UserSettings : IUserSettings {
   // ...
   // ...

}

// when you need to load the user settings:
private UserSettings _settings;
bool createdNew;
_settings = UserSettingsManager.Load(typeof(UserSettings), out createdNew) as UserSettings;
if ( createdNew ) UserSettingsManager.Save(_settings);

// when you need to save the settings:
UserSettingsManager.Save(_settings);

It's as simple as that and it sure makes managing user settings a snap!

Monday, October 10, 2005 4:37:00 AM (Mountain Standard Time, UTC-07:00)  #    Disclaimer  |  Comments [0]  |  Trackback
 Wednesday, September 28, 2005

Well, I'm pretty excited.  Today I received my set of Bose QuietComfort 2 headphones.  They're a bit on the pricey side, so I've been saving for a while, but I am in love with them!  The phones are awesome and extremely comfortable.  Even before putting in the single AAA battery it drowned out so much of the ambient sound I could tell I was in for a treat.

Then I put the battery in, switched 'em on and I was in an auditorium.  Perhaps appropriately, the very first musical piece I played once putting them on was Beethoven's 9th Symphony :-)

The phones fold down nicely and came with a great hard carrying case which makes them perfect for traveling.  In fact the primary reason I purchased them in the first place was because of travel.  I travel quite frequently and airplane noise really wears on me.  That, and the fact that I had to turn my speakers up very high just to hear over the engine howlings, thereby adding more noise and stress prompted me to look into a noise-cancelling alternative.  Though I haven't yet taken these on a test flight (I will on Sunday), I can tell that I'm already going to be very pleased with the result.

Wednesday, September 28, 2005 4:39:00 AM (Mountain Standard Time, UTC-07:00)  #    Disclaimer  |  Comments [0]  |  Trackback
 Tuesday, September 27, 2005

I've been dabbling with Windows Vista Beta 1...and I must say “WOW!”  I'm loving it!  I took a bit of time late this afternoon to set up a Virtual PC image with the Vista Beta 1 bits on it.  It took some some to get it up and running, so I thought I'd provide some info to ease your burden of setting it up.

First of all, when installing Vista it will not be able to recognize or identify a hard drive that you create with Virtual PC right out of the box, you need to do some configuring.  This is made manifest part way through the installation when is asks you into which drive you want to install the OS and it will not allow you to select the partition or format it, saying that it's unavailable.  Any attempt to repair this will result in failure.  What you need to do is drop down into the Windows PE (Preinstallation Environment) console by pressing SHIFT+F10 after selecting to 'Install Windows'.

Apparently, what happens is when you set up your disk (I chose to create a fixed 16GB drive image, though I don't think that's required) it will create the image to the proper size, but the installer will think that there are 0 bytes free on the disk for some reason. Once in the console, you'll need to blow away (CLEAN) the partition, and recreate a RAW partition and format it.  After that, the installer will locate the disk during install and it's off to the races....very slow races mind you, but races nonetheless.  The installer will take over an hour and a half (possibly much more).  So here's what you do (at least this is what I did and it worked):

At the PE console type in the following commands, pressing [ENTER] between each one:

  • DISKPART

DISKPART will drop you down to a DISKPART command line where you enter the following commands:

  • SELECT DISK 0
  • CLEAN
  • CREATE PARTITION PRIMARY
  • ACTIVE
  • FORMAT
  • ASSIGN LETTER=C

Then restart the installation (perhaps rebooting via Action menu -> CTRL+ALT+DEL option).

Once Windows is installed (which will take a considerable amount of time, but which is also completely “hands off” once started), it will not be able to detect drivers for the devices, and will render the UI in 4-bit color at 800x600.  You'll need to install the Virtual Machine Additions via the Action menu.

I must say, that I am incredibly impressed with not only the UI, but how clean and professional it is.  Windows Vista is going to rock, I'm so excited!  I'm gonna start putting it through it's paces to see how my apps work on it and start checking it out.

Tuesday, September 27, 2005 3:07:00 PM (Mountain Standard Time, UTC-07:00)  #    Disclaimer  |  Comments [0]  |  Trackback

In all our copious free time we've had a chance to do a few upgrades to the house.  We built a shed and finished it, replaced the garage door, etc.  Recently, we took the courtyard/patio in the front yard and finally enclosed it in a brick wall with columns and lights.  I say finally because it was kind of an eyesore for about a year...we had the courtyard poured with 3/4” PVC pipe sticking up out of it in three places.  When we had the concrete poured we preanticipated that the wall would be built so I ran the pipe underneath the stairs into the basement and then up and out where the columns were to be.  It feels so much better now to have it completed.  I have a few pictures that show the work in progress and the finished product.  The last remaining task is to run the wire up the wall to the switch to the lights on the posts switch on with the other exterior lights.  Oh, and that reminds me, I also changed out the lights on the house (I hated the stock lights that were included with the house)...these are so much nicer.

In Progress:


And the finished wall:

Tuesday, September 27, 2005 4:22:00 AM (Mountain Standard Time, UTC-07:00)  #    Disclaimer  |  Comments [1]  |  Trackback
 Sunday, September 25, 2005

At some conference, IBM was showcasing their voice recognition system, and issuing voice commands on the command prompt of his laptop.

Someone from the first row shouted out "format c:"

Someone else shouted out "return; yes; return"

(credit to Juan, commentor on ASP.NET Resources - What Goes Around Can't Be Recalled)

Sunday, September 25, 2005 2:14:00 PM (Mountain Standard Time, UTC-07:00)  #    Disclaimer  |  Comments [1]  |  Trackback
 Saturday, September 24, 2005

This is proving to be a blast of a weekend - a MUCH needed reprieve from the usual 'writing code' all weekend and never enjoying myself.  This is actually the first time in a long time (almost 3 years) in which I've taken any personal time.  Last night my dad, 5th cousin Maja, and I went to a performance of Beethoven's 9th Symphony by the Utah Symphony.

Today, we all went four-wheeling.  My brother has a few four-wheelers, a three-wheeler, and a dirt bike so we had a blast.  I've not done that in years.  I have some good friends (Jason Walker and Justin Long [edited 09/26/2005 - sorry Justin]) who would go four-wheeling pretty much every weekend, until Jason's accident back in April.  It was a lot of fun to go jumping the dunes and hills.  Perhaps I'll post up some pictures here shortly.

To cap off the evening we're all going down to The Roof for dinner tonight.  Maja goes back home to Slovakia on Tuesday so this is kind of a farewell dinner in which we could all get together.  Looking forward to it!

Saturday, September 24, 2005 9:57:00 AM (Mountain Standard Time, UTC-07:00)  #    Disclaimer  |  Comments [1]  |  Trackback
 Friday, September 23, 2005

This evening I had the opportunity to attend a very moving and exhilirating concert by the Utah Symphony with my dad and 5th cousin Maja Jantolakova (I hope I spelled that correctly) who has been in the US visiting for the past several weeks.

The orchestra was conducted by Maestro Stanislaw Skrowaczewski.  At first, they played an extremely powerful and complicated piece: Grosse Fuge (Grand Fuge).  It was awesome!  I'm going to have to listen to it in its original, intended form - for a string quartet, but it was impressive in symphonic form.

Following a brief intermission the orchestra and choir came out and collectively performed Beethoven's Symphony No. 9.  The 9th has always been my favorite of Beethoven's symphonies (followed closely by the 7th, 5th, and 3rd).  Was an awesome performance!

One aspect of tonight's events that provided a particularly nice touch was a brief, 30 minute talk preceding the performance with Maestro Skrowaczewski.  He along with the host of the meeting (I don't recall his name) discussed the two pieces, particularly the Grosse Fuge.  Apparently this piece is beyond complicated.  It has been tagged as 'unplayable' and an impossible piece.  This was the first time it's ever been performed in Utah by the Utah Symphony, so that was a particular treat.  In addition to being an enjoyable evening at Abravanel Hall, it was enlightening and educational.

I'm so glad I had the chance to go tonight and be with Maja and my dad.

Friday, September 23, 2005 5:32:00 PM (Mountain Standard Time, UTC-07:00)  #    Disclaimer  |  Comments [1]  |  Trackback
 Wednesday, September 21, 2005

[EDIT: 07/10/2006 - Fixed stupid spelling error]

I created a very simply control the last night that might come in handy to someone.  If you've ever had the need to have a series of images cycle at some interval on your webpage then this might be useful.  The script to create such functionality on your site is quite trivial (though perhaps it gets slightly more redundant and complicated if you need multiple, distinct, rotating images on your page simultaneously, each with a varying interval between switching images).  However, I wanted to have an ASP.NET control that I could just drop on a page, set up its sequence of images, have it work, and call it good.

Well, I've created a control called ImageRotator that provides this functionality.  Simply add the control library (dll) your website's \bin directory, create the little markup needed, and you're up and running.  The .zip file contains the full source (which really isn't much at all) and the 'release-build' bits for both .NET 1.1 and 2.0, along with a readme tutorial on how to use it.

By way of example, in order to set it up on your page, all you have to do is this:

<%@ Register tagPrefix="devstone" namespace="Devstone.Web.WebControls" Assembly="ImageRotator" %>

<devstone:imagerotator id=imgRotator runat=server interval=3000>
   <devstone:imageref imageurl="~/images/image001.jpg" />
   <devstone:imageref imageurl="~/images/image002.jpg" />
   <devstone:imageref imageurl="~/images/image003.jpg" />
</devstone:imagerotator>

Pretty simple, but all it does is help solve a simple problem.

You can download the .zip file here.

Wednesday, September 21, 2005 8:36:00 AM (Mountain Standard Time, UTC-07:00)  #    Disclaimer  |  Comments [23]  |  Trackback
 Tuesday, September 20, 2005

In conjunction with a post that I made a few days past, I've embraced another web debugging tool.  A few days ago I mentioned the Internet Explorer Developer Toolbar.  Well, a developer at Microsoft by the name of Nikhil Kothari has created a very cool component that sits inside your browser window and monitors all HTTP/S traffic that flows within that browser session.  Unlike other HTTP debugging proxy tools that I use (such as Fiddler) which monitor all HTTP traffic on the box, the Nikhil's Web Dev Helper tool isolates traffic filtering to the IE browser instance in question so it's pretty nice.

NOTE: The tool does require the .NET Framework 2.0 Beta 2.  I didn't realize that at first (that goes to show how well I read instructions) so when I was trying to GAC-register the .dll using v1.1 GACUTIL it was failing.  Once installed (which is incredibly painless) it appears as an option in the IE Tools menu.

Very cool!  Thanks Nikhil!

Tuesday, September 20, 2005 7:24:00 AM (Mountain Standard Time, UTC-07:00)  #    Disclaimer  |  Comments [0]  |  Trackback
 Monday, September 19, 2005

One of the new technologies to debut at PDC, providing attendees the first glimpses is that of Office 12.  I recall talk about Office 12 from my days in Microsoft Consulting Services a few years back, how it was going to be a complete rewrite from the ground up, eliminating any remnant of 16-bit code, etc.  Well, I can definitely vouch for the overhauled user experience - it looks NOTHING like its predecessors.  I am absolutely looking forward to giving it a whirl when it's available!

Check out the screenshots and read about it!

Monday, September 19, 2005 7:47:00 AM (Mountain Standard Time, UTC-07:00)  #    Disclaimer  |  Comments [0]  |  Trackback