Book Review: Pro WPF and Silverlight MVVM: Effective Application Development with Model-View-ViewModel

Pro WPF and Silverlight MVVM: Effective Application Development with Model-View-ViewModel by Gary Hall 

The book is well written, easy to read and informative. While some technology books can be so in-depth and written so technically as to make them unreadable, thankfully this author has struck a good balance between technical detail and readability.

While there are books that cover Silverlight and WPF in great detail, this isn’t one of them. The author does however give a very good overview of both, giving enough detail of the technologies to form a base for discussion and whet the appetite for more. For instance, the coverage of areas such as databinding and dependency properties are enough to give an understanding of how they work and can be used in the context of MVVM without delving into excruciating details.

The book also has a good generalized discussion of some OO concepts and design patterns such as the command and mediator patterns. Again, it is not in-depth but does give a good understanding of the topics for novices and perhaps a refresher for more experienced developers.

Overall, if you are purchasing this book to specifically learn MVVM it will be a disappointment. If, however, you want a book that offers a general discussion of topics such as validation, data access layers, unit testing and design patterns with Silverlight and WPF with an MVVM bent, then this is the book for you.

Advertisements
Posted in Book Review, General | 1 Comment

Bing Image Download

Microsoft’s Bing has some amazing images as backgrounds; however, if you want to use them as background images for your Windows Desktop you must go through the tedious process of opening your browser and saving the image as a background each day.

Unless you can automate the process that is.

Finding the images

The first step in the process is to find the images. Since they are the background on the webpage the image must be downloaded to your Temporary Internet Files folder; however, these files have sometimes cryptic names, can be removed when cleaning the temporary files, and of course, you must visit Bing to initiate the download.

Long Zheng at www.istartedsomething.com has created an excellent archive of all Bing images, yet the problem is, through there is a RSS feed available, they are not downloadable directly from this site. There are instructions available here, http://www.makeuseof.com/tag/how-to-set-a-bing-wallpaper-desktop-slideshow-in-windows-7/ on creating a Theme file using the RSS feed; however, I’ve found this method to be unreliable.

Through some investigation I found the image information is available from Bing using this URL http://www.bing.com/HPImageArchive.aspx?format=xml&idx=0&n=1&mkt=en-US

As the format parameter indicates this will return an XML formatted stream that contains information such as the date the images is for, the relative URL, description and copyright information.

The idx parameter tells where you want to start from. 0 would start at the current day, 1 the previous day, 2 the day after that, etc. For instance, if the date were 1/30/2011 using idx = 0 the file would start with 20110130, using idx = 1 it would start with 20110129, and so forth.

The n parameter tells how many images to return. n = 1 would return only one, n = 2 would return two, and so on.

The mkt parameter tells which of the eight markets Bing is available for you would like images from. The valid values are: en-US, zh-CN,  ja-JP,  en-AU,  en-UK, de-DE, en-NZ, en-CA.

Automating the download

Now that you know where to get the images from it easy to create an automated process to download them.

public class BingImages
{
private const string DOWNLOAD_PATH = @”D:\BingImages”;
private const string BING = “http://www.bing.com”;
private const string IMG_URL = “http://www.bing.com/HPImageArchive.aspx?format=xml&idx=0&n={0}&mkt={1}”;
private static string[] Markets = new string[] { “en-US”, “zh-CN”, “ja-JP”, “en-AU”, “en-UK”, “de-DE”, “en-NZ”, “en-CA” };
private const int NUMBER_OF_IMAGES = 1;

/// <summary>
/// Download images from Bing
/// </summary>
public static void DownLoadImages()
{
   // Make sure destination folder exists
   ValidateDownloadPath();

   XDocument doc = null;
   WebRequest request = null;
   // Because each market can have different images
   // cycle through each of them
   foreach(string market in Markets)
   {
      // Form the URL based on market
     // Since this will be run once per day only 1 image needs to
     // be downloaded
     string url = string.Format(IMG_URL, NUMBER_OF_IMAGES, market);
     request = WebRequest.Create(url);
     using(Stream stream = request.GetResponse().GetResponseStream())
     {
           // Load the stream into and XDocument for processing
            doc = XDocument.Load(stream);
      }
     
      // Iterate through the image elements
      foreach(XElement image in doc.Descendants(“image”))
      {
           SaveImage(image.Element(“url”).Value);
      }
  }
}

/// <summary>
/// Save image from the give URL to disk
/// </summary>
/// <param name=”url”>URL of image to save</param>
private static void SaveImage(string url)
{
   // Images can be duplicated between markets
   // so to avoid duplicates from being downloaded
   // get the unique name based on the image number in the URL
   string filename = GetImageName(url);
   if(!File.Exists(filename))
   {
      // URL is relative so form the absolute URL
      WebRequest request = WebRequest.Create(BING + url);
      using(Stream stream = request.GetResponse().GetResponseStream())
      {
          Image img = Image.FromStream(stream);
          img.Save(filename);
       }
    }
}

/// <summary>
/// Create filename for image based on URL
/// </summary>
/// <param name=”url”>Image URL</param>
/// <returns>FQN for saving image to</returns>
private static string GetImageName(string url)
{
   // URL is in this format /fd/hpk2/DiskoBay_EN-US1415620951.jpg
   // Extract the image number
   Regex reg = new Regex(@”[0-9]+\w”);
   Match m = reg.Match(url);
   // Should now have 1415620951 from above example
   // Create path to save image to
   return string.Format(@”{0}\{1}.jpg”, DOWNLOAD_PATH, m.Value);
}

/// <summary>
/// Check if download path exist and create if necessary
/// </summary>
private static void ValidateDownloadPath()
{
   if(!Directory.Exists(DOWNLOAD_PATH))
   {
       Directory.CreateDirectory(DOWNLOAD_PATH);
    }
}
}

With this code you can create a Windows Service or any other type of application to automate the download process, then set the Windows Desktop backgrounds to use the designated folder.

Posted in CodeProject, General | Leave a comment

SPQuery and SPSiteDataQuery Managed Metadata columns

SharePoint 2010 supports Managed Metadata and of course you can include a column of this type in any list definition. However, when querying a list and specifying a Manage Metadata column in the ViewFields different information is returned depending on which type of query you use.

For testing I created a new Managed Metadata Service Group and TermSet with a couple Terms, nothing outlandish.

Termstore

Then I created a Custom List and added a Managed Metadata column named “Tax” that references the MySet TermSet.

TaxList

For testing purpose I created a very simple and minimal console application. As you can see from the code below both methods shared the ViewFields so I expected the same results in the Tax field.

private static string VIEW_FIELDS = "<FieldRef Name='Title'/><FieldRef Name='Tax'/>";

static void Main(string[] args)
{
   using(SPSite site = new SPSite(http://MyServer))
   {
      using(SPWeb web = site.OpenWeb())
      {
         SPList list = web.Lists.TryGetList("TaxTest");
         UseSPQuery(list);
         UseSPSiteDataQuery(web);
      }
   }
   Console.ReadLine();
}

private static void UseSPQuery(SPList list)
{
   SPQuery query = new SPQuery();
   query.ViewFields = VIEW_FIELDS;    
   DataTable dt = list.GetItems(query).GetDataTable();    
   Console.WriteLine("SPQuery:\t\t{0}", dt.Rows[0]["Tax"]);
}

private static void UseSPSiteDataQuery(SPWeb web)
{   
   SPSiteDataQuery query = new SPSiteDataQuery();   
   query.ViewFields = VIEW_FIELDS;    
   DataTable dt = web.GetSiteData(query);   
   Console.WriteLine("SPSiteDataQuery:\t{0}", dt.Rows[0]["Tax"]);
}

The results however where this,

Result1

As you can see the SPQuery returned the Managed Metadata field as the Term name then the Term ID while the SPSiteDataQuery returned it as WssId then the Term name.

To correct this and have both queries return the same you need to set the ViewFieldsOnly property to true for the SPQuery.

SPQuery query = new SPQuery();
query.ViewFields = VIEW_FIELDS;
query.ViewFieldsOnly = true;

With this property set the results are now what are expected.

Result2

Posted in CodeProject, SharePoint, SharePoint 2010 | 1 Comment

Work-Life Balance

Posted on J.D. Meier’s blog, this is a great example of how maintaining a work-life balance makes a team more effective and how the traditional expectation of heroic efforts by bleary-eyed, overworked developers do more to hurt the project, and organization, than help.

http://blogs.msdn.com/b/jmeier/archive/2010/10/21/40-hour-work-week-at-microsoft.aspx

Posted in General | Leave a comment

Code Camp 2.0

Thanks to everyone who attended Pittsburgh Code Camp 2010.2.

Posted in Events | Tagged | Leave a comment

Pittsburgh Code Camp 2010.2

The second installment of Code Camp in the Pittsburgh area will be held Oct 16th, 2010 at

Department of Computer Science
Sennott Square
University of Pittsburgh
Pittsburgh, PA 15260

Registration begins at 8:00 and sessions begin at 9:00. For more information and to register go here; http://codecamppgh.com/default.aspx

I will be presenting the following session in room 5-313 at 9:00 AM

SharePoint 2010 Development with Visual Studio 2010

Microsoft has made some significant enhancements to both SharePoint 2010 and Visual Studio 2010. By combining these tools it is possible to develop, debug and deploy SharePoint based applications more rapidly than with previous editions. This session will present an overview of some of the new features of SharePoint 2010 and Visual Studio 2010 as it relates to SharePoint development. Topics discussed will include creation of Site Definition, List Definitions, Content Types, WebParts, Application Pages and LINQ to SharePoint in addition to others.

Posted in Events | Tagged | Leave a comment