CAUBLESTONE INK

.net development and other geeky stuff

Book Review: Release It!: Design and Deploy Production-Ready Software

Posted on January 17th, 2008


After reading on several blogs that this was a book worth getting I decided to get myself a copy. I will say that this is a great book and that it should be on every programmer’s bookshelf. This is right up there with Code Complete. If you have not already gotten a copy you should do so.

When it comes to technical books I like to only purchase books that I will continue to use over the years vs ones that are very targeted and will go out of date. Thus architectural books are a prime candidate for purchase. However knowing which to buy can be daunting. After all who wants to plop down $50 or more for a book that might only have 1 or 2 good chapters in it or is a rehash of books you already have. That’s where these reviews come in. I picked up this book based on reviews on other blogs and I was not disappointed. While the information in the book is presented by an obvious Java programmer it in no ways is tied to Java programming. It provides insight into several projects from the real world and things that they learned from them. As such it is more of a best practices book vs a code book. From Anti-Patterns to Patterns it walks you through various scenarios and things to watch out for when developing release ready software.

After reading the book myself I was able to make changes to the way I was coding my components to take advantage of these patterns. By making use of this pattern I was able to take a service interface layer and change it such that the application using it would not crash from this component as it had done in the past. I was able to accomplish this by using the circuit breaker pattern that he describes in his book. Basically what it does is very much what you would expect. Just like in a home if you plug too many items into your outlet and it overloads the circuit it throws the breaker so you don’t burn your house down. So in code what this does it allows you to implement your code such that if you run into problems it can shut down the interface or process without taking down your application with it. After all who wants to crash their application or website because an integration point is down or broken.

There are quite a few other patterns some of which I have already taken advantage of like the Fail Fast pattern. I can honestly say that after reading this book almost any developer should be able to walk away a better programmer and will most likely start to use some of the patterns presented in this book. I know I have.

All in all this was an excellent book and it was written very well and not all that expensive. If you would like to see a more in-depth review see this blog post, Ayende’s Blog.
This book should be on every developer’s bookshelf. Get it now. You can get it here: Release It

Tip: Open a .Net Command Prompt Here

Posted on December 21st, 2006


Download (1k)

If you find you want to open a command prompt on a certain folder and have access to all the underlying Visual Studio.Net <your version here> options this is for you.

This is based on similar updates to your windows system in which you can add a new option to your right-click context menu inside of your explorer windows. What this particular tip does is shows you how to add a item to your menu that will open a VS.Net 2003 command prompt on the specified folder, however you follow the same procedure for all versions of visual studio.

Note: This involves updating the registry. You should always backup your registry before making manual changes as your changes could make your system unstable. If you are not comfortable with doing these types of changes you should pass on this tip.

Following the steps below:

1. Open regedit.exe
  Start->Run->regedit.exe
2. Navigate to the following location:
HKEY_CLASSES_ROOT/Directory/shell
3. Add a new key
  VS2003CMD
4. In this key create another key called:
  command
5. Click on the key created in step 3. On the right double-click the Default value and type in the following: (this will be displayed in the context menu).
  Open &VS2003 Command Prompt
6. Click OK
7. Click on the command subkey and then double-click on the default value on the right. Enter the following: (this is the command that will be run).

cmd.exe /k “cd %L && “c:/Program Files/Microsoft Visual Studio .NET 2003/Common7/Tools/vsvars32.bat” && ver”

Make sure you entered the quotes. This also assumes that you have installed VS.Net 2003 in the default folder. If you did not then adjust accordingly.

That should do it. Now if you go into windows explorer you should see a new menu item when you right-click.

To make this easier we have also included a download of the reg file that you can just apply to your system. As with anything to do with the registry you should always double check files you download that update the registry to make sure nothing malicious exists.

Enjoy.

Tutorial – Google Desktop Sidebar – Hello World Part 4, Loading and Saving our properties

Posted on December 10th, 2006


Requirements

.Net Framework SDK 1.1
Google Desktop SDK

Downloads

The zip file listed here is all inclusive of all four tutorial parts. We broke up the article into four parts for ease of reading. Included in the zip is a Word doc version of all four articles.
Download (231KB)

Purpose

In this article I intend to show you how to extend our simple Google Desktop Sidebar (GDS) component. All the source is in C# 1.1. We assume that all developers looking at this article are familiar with writing code in C# and the .Net framework. For this component we will be doing a simple Hello World type sample. We will show screenshots where appropriate. Otherwise we assume you know your way around.

What it Shows

In this part of our tutorial we will be showing you how to save and load the changes you have made. We do not take it so far as to keep up with the content but it gives you the general idea.

Storing our changes

Ok, as we all know if you make a change to something and you want it to stick we must save it. Well the GDK provides that functionality as well. In order to provide this save / load functionality we must implement the interface IPersistStreamInit. To start with we need to add some basic data to our class. We are going to add the following member variables.

const int dataVersion = 1;
private bool isDirty;

The dataVersion property can probably be replaced to read from your assemblyInfo.cs file but we will leave that to you. We need this property so we can support versioning of our component. It will also help if you happen to have multiple versions running that you get the correct data loaded for your component. You don’t really need this but it does provide a bit of added support for later. The isDirty flag will be set and used so we know when anything has changed so that we know when to save our data.

So, for us to know that something has changed we need to add it to our layout property. Let’s change our code:

public GoogleDesktopDisplayContentItemLayout Layout
{
  get { return this.layout; }
  set { this.layout = value; this.isDirty = true; }
}

All we have done here is add the isDirty flag to our property. Now we also need to initialize it so let’s add some code to our constructor.

this.isDirty = false;

Now we can start looking into the IPersistStreamInit interface. The first thing you will probably notice is that it has an IsDirty property. This is used by the GDK to determine if the Save operation needs to occur. So lets modify the code, note that we need to change a Boolean value into an unsigned integer.

public UInt32 IsDirty()
{
  return (UInt32)(isDirty ? 1 : 0);
}

Now that we can tell if our component needs to be saved letss add the code to save our changes. We will do this by adding code to the Save method.

public void Save(UCOMIStream stm, bool clearDirty)
{
  MemoryStream memStream = new MemoryStream();
  BinaryWriter binWriter = new BinaryWriter(memStream);
  // first our version
  binWriter.Write(dataVersion);
  // then the data.
  binWriter.Write(this.Layout.ToString());
  // cleanup binWriter.Flush();
  // write to actual output stream
  byte[] data = memStream.ToArray();
  stm.Write(data, data.Length, IntPtr.Zero);
  if (clearDirty) isDirty = false;
}

Now again, we assume you know how to use the framework and that includes the IO process. So, what we are doing is setting up a memory stream since the GDK actually handles the file IO. To Write to the stream you should be able to use any type of writer and pass it the memory stream to write to. The first thing we write out is our dataVersion and then we write out the value of our Layout property. You will notice that we are writing the string value representation, this will make it easier to pull back in later. Once we are done we convert the memory stream into an array of bytes so we can pass it to the underlying COM stream that the GDK has provided to us. Last but not least we set the dirty flag only if the stream write was successful.

Once we have saved our data we can now add code to load our data. This works the same way just in reverse. In a more advanced scenario there is no reason why you could not serialize / de-serialize a class into this stream. Let’s add our Load code.

public void Load(UCOMIStream stm)
{
  const int maxDataLen = 1000;
  // read a large enough number of bytes for us.
  byte[] data = new byte[maxDataLen];
  stm.Read(data, data.Length, IntPtr.Zero);
  MemoryStream memStream = new MemoryStream(data);
  BinaryReader binReader = new BinaryReader(memStream);

  int version = binReader.ReadInt32();
  if (version != dataVersion)
  {
    MessageBox.Show(&quot;Invalid data version, using default values&quot;);
  }
  else
  {
    string value = binReader.ReadString();
    switch (value)
    {
      case &quot;GDD_CONTENT_ITEM_LAYOUT_NEWS&quot;:
        this.Layout = GoogleDesktopDisplayContentItemLayout.GDD_CONTENT_ITEM_LAYOUT_NEWS;
        break;
      case &quot;GDD_CONTENT_ITEM_LAYOUT_EMAIL&quot;:
        this.Layout = GoogleDesktopDisplayContentItemLayout.GDD_CONTENT_ITEM_LAYOUT_EMAIL;
        break;
      case &quot;GDD_CONTENT_ITEM_LAYOUT_NOWRAP_ITEMS&quot;:
        this.Layout = GoogleDesktopDisplayContentItemLayout.GDD_CONTENT_ITEM_LAYOUT_NOWRAP_ITEMS;
        break;
    }
  }
}

In this section of code we are again creating a Memory stream and this time a Reader object. You should create the same type of Reader as you did writer so that it will be compatible. Seeing as how we have only written a small amount we have set an arbitrary size of 1000 bytes to read which is way more than enough to cover getting our info. If you had a large set of data you would need to get the actual size of your data and initialize accordingly. Since we wrote out the first value of our dataVersion we can check it first to make sure we are working with the same file. If we are then we can read the rest of the data and set our Layout property to what we saved the component with last.

Well that should do it so compile your app and you should get the following prompt.

install

If you don’t fix any problems you have and then try again. You are ready to try it out. So open your GDS and then go into the properties and change the value and then exit the GDS and re-open your data should be displayed the same as when you exited the app.

The first time you run your app you will get an error saying that you have an invalid data version. We could wrap this but we will leave this to you the reader as an exercise.

What you have learned

In this last part of our tutorial set you have learned how to load and save your data changes that you make to your plug-in. If you want to make it more extensive we will leave that to you the reader as an exercise.

Conclusion

We hope that you have found these tutorials helpful. In looking through the developer forum we saw that a lot of people had asked for one and hope this meets the needs of what you were looking for.

Enjoy.

Tutorial – Google Desktop Sidebar – Hello World Part 3, Adding a Property Page

Posted on December 10th, 2006


Requirements

.Net Framework SDK 1.1
Google Desktop SDK

Downloads

The zip file listed here is all inclusive of all four tutorial parts. We broke up the article into four parts for ease of reading. Included in the zip is a Word doc version of all four articles.
Download (231KB)

Purpose

In this article I intend to show you how to extend our simple Google Desktop Sidebar (GDS) component. All the source is in C# 1.1. We assume that all developers looking at this article are familiar with writing code in C# and the .Net framework. For this component we will be doing a simple Hello World type sample. We will show screenshots where appropriate. Otherwise we assume you know your way around.

What it shows

In this part of our tutorial we will be adding a property page to our component that will allow us to change the content format among the three built-in types.

Adding Property Pages

We are assuming you have kept up so far if not pull down the code from above and you can follow along or you can work through the other two parts.

Since we are going to add a property page lets start with the obvious. Add a new Windows Form file to your project. Once complete open up the code window and add the following Using statements.

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Google.Base;
using Google.Base.Utils;
using GoogleDesktopAPILib; // Google Type libraries
using GoogleDesktopDisplayLib; // Google Type libraries

As you can see it is mostly the same as a windows form but we have added the google items to the mix. If you look in the GoogleBase folder you will see a base property page object. This is taken pretty much straight from the samples that google provided. What you want to do is change your form class to inherit from the base property page class. That will hookup the form so it can be used by our plug-in. Your class should look something like this:

[GuidAttribute("DFAEFC1E-2466-40d5-80D6-3FF5136627DA")] // CHANGE THIS in your plug-in
[ClassInterface(ClassInterfaceType.None)]
public class frmHelloProperties : Google.Base.PropertyPage

Notice that again just like our main plug-in class we have a GUID Attribute. Since the plug-in / GDS needs a handle to the property page we need the GUID so that COM can get a hook into it for our use. Next by marking our class with the ClassInterface attribute we can control whether or not the class is exposed via a typelib. Since this is used internally we can mark it as none.

Now once you get that done you can setup your form however you want. Just like a normal form. For this tutorial we are going to add a label and a combo box. So add them to the form and change the Label to say “Change Display Layout”. Then change your combobox to be a drop-down list. Next add the following values to the Items collection, NoWrap, Email, and News.

Next let’s get back to our code. We are going to override two methods on our property page, OnPageActivate and OnPageApply. As there names imply we will be handling code when you open the property page and when you hit the Apply or OK button.

Let’s do the Activate method first.

/// <summary>
/// Called just before the property page is displayed
/// </summary>
protected override void OnPageActivate(System.IntPtr wndParent, Rectangle Rect, bool modal)
{
  base.OnPageActivate(wndParent, Rect, modal);
  HelloWorldPlugin ctl = dataObject as HelloWorldPlugin;
  if (ctl != null)
  {
    // Get the parameter values from the plug-in
    switch (ctl.Layout)
    {
      case GoogleDesktopDisplayContentItemLayout.GDD_CONTENT_ITEM_LAYOUT_EMAIL:
         this.cboLayout.SelectedItem = "Email";
         break;
      case GoogleDesktopDisplayContentItemLayout.GDD_CONTENT_ITEM_LAYOUT_NEWS:
         this.cboLayout.SelectedItem = "News";
         break;
      case GoogleDesktopDisplayContentItemLayout.GDD_CONTENT_ITEM_LAYOUT_NOWRAP_ITEMS:
         this.cboLayout.SelectedItem = "NoWrap";
         break;
    }
  }
}

The first thing we do is let the base class do what it needs to do, then we apply our content information / changes. Each property page has a dataObject that equates to the plug-in you are using. So we need to set and cast our object to the type of our plug-in. If we have a plug-in handle then we can continue with our code. The property we have here, which we will create in just a few, is called Layout. It is of the type GoogleDesktopDisplayContentItemLayout which defines the three currently supported display formats out of the box. So what we do here is figure out what we are currently using and then set our combobox to the appropriate value.

Next lets look at the Apply process which is very similar.

/// <summary>
/// Called when user hits OK in the property page
/// </summary>
protected override void OnPageApply()
{
  HelloWorldPlugin ctl = dataObject as HelloWorldPlugin;
  if (ctl != null)
  {
    // Set the new parameter values back to the plug-in
    switch (this.cboLayout.SelectedItem.ToString())
    {
      case "NoWrap":
        ctl.Layout = GoogleDesktopDisplayContentItemLayout.GDD_CONTENT_ITEM_LAYOUT_NOWRAP_ITEMS;
        break;
      case "Email":
        ctl.Layout = GoogleDesktopDisplayContentItemLayout.GDD_CONTENT_ITEM_LAYOUT_EMAIL;
        break;
      case "News":
        ctl.Layout = GoogleDesktopDisplayContentItemLayout.GDD_CONTENT_ITEM_LAYOUT_NEWS;
        break;
    }
    ctl.GetOurStuff();
  }
  base.OnPageApply();
}

Again we get a handle to our plug-in object. However this time we do our setting of our data. Now you will also notice that we are making a direct call to the GetOurStuff method. This way we can go through and re-apply our formatting to our content items. Last but not least we call our base method so that the GDK can finish anything else it needs to.

Since we defined the layout variable in part two you should be able to just add this class. Next lets go through and hookup the property pages. To do this we need to implement the ISpecifyPropertyPages interface. It’s pretty simple and straight forward so here is the code:

public void GetPages(ref CAUUID pages)
{
  // get our guid
  Guid[] g = new Guid[1];
  g[0] = typeof(frmHelloProperties).GUID;
  pages.SetPages(g);
}

What we are doing here is getting the GUID from our property page and then setting it into the pages object provided by the GDK. It will then get the hook to the property page and display it.

Let’s go ahead and compile it and see what we have so far. When you compile your code you should hopefully get the following dialog.

If you did not fix any errors you have and then continue. Next if you do not have the GDS running start it up.

To start you should see something like the following:

Now let’s open the property page by selecting the down arrow and choosing Options. As you will see it will show the NoWrap as the current display style. This is what we set in the constructor of our main plug-in code. Try changing it around and see how it looks.

Email View

News View

What you have learned

In this part of our tutorial you have learned how to add a property page and how to apply those changes to your plug-in. In our next part of this tutorial we will show you how to save your view layout information and restore it at run-time.

Tutorial – Google Desktop Sidebar – Hello World Part 2, A simple plug-in with Content

Posted on December 10th, 2006


Requirements

.Net Framework SDK 1.1
Google Desktop SDK

Downloads

The zip file listed here is all inclusive of all four tutorial parts. We broke up the article into four parts for ease of reading. Included in the zip is a Word doc version of all four articles.
Download (231KB)

Purpose

In this article I intend to show you how to extend our simple Google Desktop Sidebar (GDS) component. All the source is in C# 1.1. We assume that all developers looking at this article are familiar with writing code in C# and the .Net framework. For this component we will be doing a simple Hello World type sample. We will show screenshots where appropriate. Otherwise we assume you know your way around.

What it Shows

In this part of our tutorial we will be showing you how to add content items to your basic plug-in. It is assumed that you have the code from the previous article as we will be building from that base. So have it ready.

Adding Content

As stated we are now going to look at what it takes to add content items to our GDS plug-in. We are going to take the simple approach and just build a for loop that will add 10 items to the plug-in every time it’s loaded. That’s it.

So let’s get started. First add a new class file to your project from earlier and add the following using statements:

using System;
using System.Text;
using System.Windows.Forms;
using Google.Base;
// Base module to ease plug-in development
using Google.Base.Utils;
// Base module to ease plug-in development
using GoogleDesktopAPILib;
// Google Type libraries
using GoogleDesktopDisplayLib;
// Google Type libraries

As you will see this is very similar to what we had in our earlier tutorial. Now let’s add our class definition. If you want to change the class name go ahead.

public class HellowWorldCI :  GoogleDesktopDisplayContentItemHelperClass, IGoogleDesktopDisplayContentItemHandler 

Since this class is not created directly you do not need a GUID attribute on it. As you can see we are inheriting directly from the Helper class provided by the GDK and an Interface. Make sure you use the Class viewer to implement the interface for it.

Most everything you can pretty much leave alone. We will only show you the actual methods that we will be changing from the default implementation. As per the documentation you should throw the NotImplementedException when ever you do not implement a particular method. By doing this the underlying api will know to use the default method for handling this as per the application framework.

A lot of this block of code is taken from the samples that come with the GDK only we have cleaned it up a little bit and by breaking it into a separate file we feel that it makes it easier to understand and conceptualize what is going on.

Add the following code to the ProcessDetailsViewFeedback method:

if ((flags &amp; GoogleDesktopDisplayDetailsViewFlags.GDD_DETAILS_VIEW_FLAG_TOOLBAR_OPEN) != 0)
{
   OpenItem();
   return;
}
if ((flags &amp; GoogleDesktopDisplayDetailsViewFlags.GDD_DETAILS_VIEW_FLAG_NEGATIVE_FEEDBACK) != 0)
{
  MessageBox.Show(&quot;Not interesting: &quot; + heading + &quot;Doing default processing&quot;);
}
// throw this exception to let the default processing happen for all other
// details view commands
throw new NotImplementedException(); 

The OpenItem method will be defined in just a few minutes. All it will do is popup a message box. What this code does is when you are in the detail view for an item there are several processing options. You can click on the header, the Remove item, or the X in the right hand corner. By clicking on each different element you can have it do several things. In the case of an RSS feed if you click on the title it will normally take you to the web article in your current browser. If you click on the Remove Item it normally tells the interface to delete it, etc. By handling the information in this section you can have your code execute various actions depending on how you interact with it.

Next for the OnDetailsView method add the following code:

// Here we can create any ActiveX control for displaying the details, and
// return it via the detailsControl parameter. We choose the
// 'GoogleDesktopDisplayDetailsViewHelper' control that will give us a
// UI that's inline with how the sidebar looks.
GoogleDesktopDisplayDetailsViewHelperClass details = new GoogleDesktopDisplayDetailsViewHelperClass();
// set the details control's content from our own data
string text = &quot;Hello World... How are you/r/n/r/nSome sample text/r/n/r/nwithout formatting/r/n&quot;;  details.SetContent(source, time_created, text, false, layout);
detailsControl = details;
title = heading;
flags =  GoogleDesktopDisplayDetailsViewFlags.GDD_DETAILS_VIEW_FLAG_TOOLBAR_OPEN | GoogleDesktopDisplayDetailsViewFlags.GDD_DETAILS_VIEW_FLAG_NEGATIVE_FEEDBACK;
return false;
// return false to show details view, true to cancel it.

This block of code is executed when you click on an item and it goes to open the expanded details view for an article. The base class used here as seen is the GoogleDesktopDisplayDetailsViewHelper class. This is the base class used by the API to display the standard details view. If you want to implement your own you would need to write the appropriate ActiveX control to handle this. What we are doing I this class in our case is setting the internal details for each item when opened into details view. This is not good for dynamic content but for this example it works perfect. The SetContent method will take the values of the object in question and use it for rendering the object.

Add this code for the OpenItem method:

MessageBox.Show(&quot;Open: &quot; + this.heading);

All are doing here is that when you double click on the item it will display the heading for the item that you double-clicked on.

That pretty much does it for this class. Now we need to update the main plug-in class from Part 1. First we need to add a property to our class to handle the Layout of our content. This will play a bigger part in the later tutorials. For now let’s add this code.

public GoogleDesktopDisplayContentItemLayout Layout
{
  get { return this.layout; }
  set { this.layout = value; }
}

On our main plug-in class lets add the following method:

public void GetOurStuff()
{
  // clear our stuff out first this.
  RemoveAllContentItems();
  // setup the basic display options needed to add to object.
  GoogleDesktopContentItemDisplayOptions options =        GoogleDesktopContentItemDisplayOptions.GDD_ITEM_DISPLAY_IN_SIDEBAR |        GoogleDesktopContentItemDisplayOptions.GDD_ITEM_DISPLAY_AS_NOTIFICATION_IF_SIDEBAR_HIDDEN;
  // We are going to add 10 hello world items.

  for (int x=0; x&lt;=9; x++)
  {
    GDP_HelloWorld.HellowWorldCI content = new HellowWorldCI();
    content.heading = &quot;Hello World - &quot; + x.ToString();
    content.source = &quot;CaubleStone Ink&quot;;
    content.snippet = &quot;Sample Content Item from CaubleStone Ink.&quot;;
    content.time_created = DateTime.UtcNow;
    content.flags = GoogleDesktopDisplayContentItemFlags.GDD_CONTENT_ITEM_FLAG_NONE;
    content.layout = this.Layout;
    this.AddContentItem(content, options);
  }
}

The first thing for this method is that we will remove all the existing items from the plug-in. Essentially clear all. In the real-world this should be dynamic but for this tutorial it allows us to recreate all the items easily. Next we set our base options in this case we tell the item that it will be displayed in the Sidebar and that if the Sidebar is set to auto-hide you will get a toast notification of the change. After that we will be adding our content. We setup the loop to add 10 items to our content page. To do that create an instance of our content class that we created earlier. Then set the heading, source, snippet, etc. For the time use the UtcNow otherwise it will not calculate the added time correctly. We have also setup a property on our main class called Layout that will retrieve our base layout. If you look at Part 1 you will see that by default it is set to the No Wrap option. This will also come into play for our Part 3 tutorial. The last and final step is to add our content item.

Next we need to modify the SetSite method so that we can call our setup method and add our content to the screen.

displaySite = (IGoogleDesktopDisplaySite)site;
// Display our content here...
if (displaySite != null)
{
    this.SetFlags(GoogleDesktopDisplayPluginFlags.GDD_PLUGIN_FLAG_NONE,
                GoogleDesktopDisplayContentFlags.GDD_CONTENT_FLAG_HAVE_DETAILS);
  this.GetOurStuff();
}

What we have added is the IF condition and it’s contents. The first thing we do is set the flags on the plug-in to tell it that "Hey we have content here that needs to be displayed". After that we call our GetOurStuff method. If you do not add the flags the content will not be displayed.

Ok it’s now time to compile and test. If you get the following screen that’s good, if not adjust your errors until it compiles. This is what you should see.

Once you click yes open your GDS unless it is already open. You should see something like the screen below.

What we learned

In this part of our tutorial we have walked you through the process of adding content items to your component. You have seen how you can add individual items, remove those items, and the different changes needed in order to add it to your plug-in.

Page 3 of 41234