Thursday, February 08, 2007
« Programming MOSS / WSS v3 Web Service AP... | Main | Virtual PC 2007 Download »

The Challenge

  • Store some search results for a custom application on the users desktop.
  • Find a tidy way to maintain state whilst maintaining performance.

The Solution

Use the Isolated Storage to persist the data you wish to maintain whilst keeping it alive using a Singleton class to avoid multiple I/O.

The sample I knocked up was more for my own benefit and teh actual implementation(done by someone else) was significantly more thought through.

I created two classes, one that would represent the data I wanted to persist and a utility service that would tale care of all the other stuff.

SearchItem.cs (2.17 KB)

SearchHistory.cs (6.17 KB)

The code, as you will see ,is the quality of prototype at best, but I trust it will help in solving similar problems.

Below is the code is used to test is from a Windows App. Note you have the option to sort the results and define the number of entries to maintain. The default would simply grow.

 

using (SearchHistory searchHistory = SearchHistory.GetSearchHistory())
{

searchHistory.HistoryCount = 10;
searchHistory.OrderItems = SearchHistory.Order.Descending;

searchHistory.AddSearchItem(new SearchItem("one"));
Thread.Sleep(1000);
searchHistory.AddSearchItem(new SearchItem("two"));
Thread.Sleep(1000);
searchHistory.AddSearchItem(new SearchItem("three"));
Thread.Sleep(1000);
searchHistory.AddSearchItem(new SearchItem("four"));
Thread.Sleep(1000);
searchHistory.AddSearchItem(new SearchItem("five"));
Thread.Sleep(1000);
searchHistory.AddSearchItem(new SearchItem("six"));
Thread.Sleep(1000);
searchHistory.AddSearchItem(new SearchItem("seven"));
Thread.Sleep(1000);
searchHistory.AddSearchItem(new SearchItem("eight"));
Thread.Sleep(1000);
searchHistory.AddSearchItem(new SearchItem("nine"));
Thread.Sleep(1000);

List<SearchItem> searchItems = searchHistory.GetSearchItems();

searchHistory.Persist();
}

Have Fun...