CAUBLESTONE INK

.net development and other geeky stuff

Effective use of Nullable<T>

Posted on May 22nd, 2009


In this post I will give a quick usage scenario for using Nullable<T> as well as the nice shortcut of the null coalescing operator ??.

I know when I first ran into some code with this ?? thing in it I was like what the heck is that. Well it is something that every developer should know about. So what does it do? If you are working with objects or value types that can be null you can use this to guarantee that you have a value.

Simple example of how this works:

string test = null;
Console.WriteLine(test ?? "We had a null value");

What this will do is print “We had a null value” to the console. Now in .Net 2.0 they introduced Nullable<T> objects as well. The null coalescing operator can be used with these quite effectively as well.

Let’s look at a Date object and how we used to have to use it:

DateTime today = new DateTime();
if (today.Year == 1900)
{
   today = DateTime.Now;
}

In the old days we would have old date times out there instead of a nice null value. So with .net 2.0 we can now do something like this.

DateTime? today = null;
if (!today.HasValue)
{
   today = DateTime.Now;
}

Now that still looks like a lot of code. This is where the null coalescing operator comes into play. Take a look:

DateTime? today = null;
today = today ?? DateTime.Now;

What this does is allows us to say, hey, if the today variable is null set it to DateTime.Now. It is clean and concise.

Now you may be also asking what the ? is after the DateTime variable. Well this is shorthand for the Nullable<T> object. You could also define the same code like this and it would mean the exact same thing. I just find the ? easier to read.

Nullable<DateTime> today = null;
today = today ?? DateTime.Now;

As you can see not only are nullable objects handy but the null coalescing operator is even handier. So now you might ask can I use it on my own objects. The answer is YES you can use the ?? operator on anything that can be null. This gives this operator true versatility and should be in every developers playbook.

Using Predicate<T>

Posted on May 14th, 2009


Unless you have been living under a rock or unable to use a newer version of .net since 1.1 you have probably run across the Predicate<T> object while using the List<T>. I know lots of people that use these Lists and there are other objects in the framework that use Predicate<t> as well. Another common one would be the Array object. The basic usage of the Predicate<T> object is to provide a delegate with a method that takes as a parameter the same data type of the object in your list. Then the List, Array, or some other object will essentially enumerate over your collection and test each object with this method. Thus the reason it returns a boolean.

What I’m going to show you here is a simple find process that will allow you to create a reusable delegate for use in the Predicate<T> processes using reflection.

First let’s get our pieces in place. Lets first see how to do it inline which is what most people use by default.

First lets define our person class we will use.

class Person
{
   private string _firstName;
   private string _lastName;
   private int _age;

   public Person(string firstName, string lastName, int age)
   {
      this._firstName = firstName;
      this._lastName = lastName;
      this._age = age;
   }

   public string FirstName
   {
      get { return this._firstName; }
      set { this._firstName = value; }
   }

   public string LastName
   {
      get { return this._lastName; }
      set { this._lastName = value; }
   }

   public int Age
   {
       get { return this._age; }
       set { this._age = value; }
   }
}

Next in our main app lets add some data.

List<Person> people = new List<Person>();

people.Add(new Person("John", "Smith", 35));
people.Add(new Person("Caitlin", "Smith", 13));
people.Add(new Person("Steve", "Long", 23));
people.Add(new Person("Justin", "Short", 45));
people.Add(new Person("Karigan", "Patterson", 16));

Now that we have data in our object we want to search. First I will show you the delegate method in-lined.

// Find one result
Person p = people.Find(delegate(Person p1) { if (p1.LastName == "Long") return true; else return false; });
Console.WriteLine("{0}, {1} - {2}", p.LastName, p.FirstName, p.Age);

As you can see we have created a delegate using the delegate keyword and we are passing in a object of the same datatype as our list as a parameter. Since we are doing a simple find operation we are looking for just 1 person with a last name of Long.

This statement is a little long but not too bad in the grand scheme of things. However what if you were writing an application where you had to do a lot of finds based on just 1 property. That in itself would be come very tedious and you would end up with a lot of repetitive code.

So now lets build a class that we can use to help us with this.

public class SimpleFind<T>
{
  private string _property;
  private object _valueToFind;
  private PropertyInfo _p;

  public SimpleFind(string property, object value)
  {
    this._property = property;
    this._valueToFind = value;
    this._p = typeof(T).GetProperty(this._property);
    Protect.Against<nullReferenceException>(p == null, string.Format("Property {0} not found on type {1}", this._property, typeof(T).FullName));
  }

  public bool Find(T t)
  {
    try
    {
      if (this._p.GetValue(t, null).Equals(this._valueToFind))
      {
        return true;
      }
      else
      {
        return false;
      }
    }
    catch
    {
      return false;
    }
  }
}

So let’s go over the class itself. First, you should notice that the class uses Generics in the class definition. The type you specify needs to match the type you use in your list. Next is the constructor. Since the delegate needs to take the type of object that matches your data type we need to pass in the information we need in the constructor. In this case since we are trying to find data based on a property value we specify the Property Name that we are going to search against and the Value we want to search for. Now the find method does all the real work though. In the find method you see we are getting the property via reflection and making sure that we actually have that property before we use it. The Protect object was discussed here. Other than that you will notice that the code pretty much matches what we did earlier.

Now to see the difference:

// Find one result
Person p = people.Find(new SimpleFind<Person>("LastName", "Long").Find);
Console.WriteLine("{0}, {1} - {2}", p.LastName, p.FirstName, p.Age);

As you can see it’s a bit shorter and highly reusable. So what if you wanted to find more than one record. The same class can be used again.

// Find one result
List<Person> p2 = people.FindAll(new SimpleFind<Person>("LastName", "Smith").Find);
Console.WriteLine(p2.Count.ToString());

Well that’s it. It’s a pretty straight forward process. However I find it very useful and easy to use and the nice thing about it is that since it’s an object you could re-use it with just a little bit of tweaking.

Enjoy.

*** UPDATE ***
After doing some more testing with this class I found that I needed to move the Property Info object up into the constructor to improve performance and reduce overhead. As well I moved the protect statement to the constructor as well as any error was causing the system to just return false and keep trying to process the find. It should stop. Last but not least the If condition in the find method was changed to just use the .Equals method.

Introduction to the Circuit Breaker pattern

Posted on March 6th, 2008


Download the code (14kb)

I was reading the book Release It! about a month ago and ran across this pattern for creating code that has the ability to essentially shut itself off.  It just so happens that I had a use for this type of process and it helped me to create my final design.  What I was working on was a component that used a third party webservice for batch processing.  In this batch we would send over anywhere from 50-1500 web request one right after the other.  As such if we had any problems on the service end we could have thrown 1500 or even more error messages depending on how it was coded.  Of course we did not want that as it could potentially cause even worse problems by letting our app continue to try and process our data even if the service was down.  So instead of needlessly killing each of our apps I took a queue from the book and implemented the circuit breaker pattern. 

Now you might ask what is the circuit breaker pattern?  Well lets take a look at a real world scenario that everybody is familiar with.  Every home today has a main power box that is the central point at which power enters the home.  Now depending on how old the home is you could either have circuit breakers, fuses, or even worse a piece of metal jammed between the connections (I’d hope not).  Now fuses and circuit breakers all work the same way and for the same reason, to regulate the amount of load that is allowed over the electrical wire.  This is important because if there were no limit then it would cause an electrical short somewhere and possibly a fire.  It happened a lot before the fuse was created.  So, the use of a fuse or circuit breaker is meant to make sure we don’t put too much of a load on the circuit or wire.  Once a threshold is met then the circuit is tripped or the fuse is blown.  Thus stopping the flow of power.  Now if we look at our code base we want the same thing to occur.  So if I were to start sending records to the service in question and they start constantly failing as long as we have setup a threshold for error counts or failures we can then trip our circuit so to say and stop the flow of data to this service. 

Well you might ask how can we do this?  In our code bases is really not all that difficult to implement once you get the concept down.  So let’s look at some code to create a small example application.  Now you don’t have to create an interface but I wanted to really show what you need to do to add this functionality to your existing code.

public interface ICircuitBreaker
{
    void BreakCircuit();
    void ResetCircuit();
    bool CircuitBroken { get; set; }
    int Threshold { get; set; }
}

Next I’m going to embed an entire class with the interface implemented.  To show how this works I have created a class that writes to a file.  If there is a problem writing to the file for a specified number of tries then it will fail gracefully vs throwing an exception.

public class FileWriterWithCB : ICircuitBreaker
  {
    private bool _circuitBroken;
    private int _threshold;
    private string _fileName;
    private int _batchSize;

    public FileWriterWithCB()
    {
      this._circuitBroken = false;
      this._threshold = 5;
      this._fileName = "CBTest.txt";
      this._batchSize = 50;
    }

    public string FileName
    {
      get { return this._fileName; }
    }

    public int BatchSize
    {
      get { return this._batchSize; }
      set { this._batchSize = value; }
    }

    public void WriteToFile()
    {
      FileStream fs = new FileStream(this._fileName, FileMode.Open, FileAccess.ReadWrite);

      StreamWriter sw = new StreamWriter(fs);

      string data = "This is a test file write";

      sw.Write(data);

      sw.Flush();
      sw.Close();
      fs.Flush();
      fs.Close();

      sw = null;
      fs = null;
    }

    public void RunBatchWithCircuitBreaker()
    {
      int failureCount = 1;

      for (int x = 0; x &lt;= this._batchSize; x++)
      {
        if (failureCount &gt; this._threshold)
        {
          Console.WriteLine("Threshold reached breaking circuit.");
          this.BreakCircuit();
        }
        if (this.CircuitBroken)
        {
          Console.WriteLine("Circuit is Broken exiting code loop.");
          return;
        }

        try
        {
          // This should throw a FileNotFound Exception
          this.WriteToFile();
        }
        catch (Exception ex)
        {
          failureCount++;
          // Just write to the console to show that it is throwing the errors.
          Console.WriteLine(ex.Message);
        }

      }

    }

    #region ICircuitBreaker Members

    public void BreakCircuit()
    {
      Console.WriteLine("Circuit is being broken.");
      this._circuitBroken = true;
    }

    public void ResetCircuit()
    {
      Console.WriteLine("Circuit is being reset.");
      this._circuitBroken = false;
    }

    public bool CircuitBroken
    {
      get
      {
        return this._circuitBroken;
      }
      set
      {
        this._circuitBroken = value;
      }
    }

    public int Threshold
    {
      get
      {
        return this._threshold;
      }
      set
      {
        this._threshold = value;
      }
    }

    #endregion
  }

Ok, so let’s look at what this is doing.  First the class is used to write to a file.  There are two methods to write to the file.  One is a single write only the other is a batch writer for us to show the use of the circuit breaker pattern.  By default we set the system to perform 50 writes and the threshold is 5 errors, at which point we would expect the circuit to be proverbial broken.  In the attached code you will find a unit test that you can run to see the results for yourself. Needless to say when you run the codebase you should see that there are five errors thrown at which point the codebase effectively shuts down.  Try playing with the threshold.

Overall the pattern shown here can be very effective in integration scenarios but it’s especially effective in batch processes.  In the integration scenario you could have your app try to connect to say an external service and if it fails you want it to return a specified value vs crashing or throwing an exception.  This can be easily accomplished by adjusting how you use the circuit breaker.  All you would need to do is set your boolean variable if your external service is down.

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

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.

Page 1 of 212