How will you make it if you never even try?

April 2, 2009

Example where calling Extension Methods on null references is useful: Parameter Validation

Filed under: C# — Tags: , , , , , — charlieflowers @ 2:56 am

In a recent post, I pointed out that extension methods can be called on null references. For example, this works perfectly fine:

public static void PrintToConsole(this string self)
{
   if(self != null)
      Console.WriteLine("The string is: " + self);
   else
      Console.WriteLine("The string is NULL.");
}

// Elsewhere in the code
string myString = null;
myString.PrintToConsole();

I said I’d give some examples of where this would actually be useful (not just a gimmick as it might appear on first blush).

One such case is parameter validation. Rick Brewster has come up with a fantastic approach for parameter validation, which lets your code look something like this:

public static void Copy(T[] dst, long dstOffset, T[] src, long srcOffset, long length)
{
    Validate.Begin()
     .IsNotNull(dst, “dst”)
     .IsNotNull(src, “src”)
     .Check()
     .IsPositive(length)
     .IsIndexInRange(dst, dstOffset, “dstOffset”)
     .IsIndexInRange(dst, dstOffset + length, “dstOffset + length”)
     .IsIndexInRange(src, srcOffset, “srcOffset”)
     .IsIndexInRange(src, srcOffset + length, “srcOffset + length”)
     .Check();
  
     // Further code snipped.
}

No doubt that’s beautiful syntax. But one of Rick’s main goals was this: Incur the least possible overhead if the parameters are all correct. In particular, don’t instantiate any additional objects if the parameters are correct.

And the way this is achieved depends on the fact that extension methods can be called on null references. If all the parameters are OK, the Begin() method, the IsNotNull() method, and so on, all return null. However, they still have a return type, and that return type has extension methods on it called “Begin”, “IsNotNull”, “IsPositive” and so forth.

You can learn more about Rick’s approach here and here.

Advertisements

April 1, 2009

NHibernate and FluentNHibernate Rock!

Filed under: C# — Tags: , — charlieflowers @ 9:18 pm

Saw a very interesting presentation last night by Brendan Erwin, on how he uses NHibernate and FluentNHibernate. FluentNHibernate is a tool that lets you specify your mappings between the database and your domain objects in C# code, instead of in XML. This is awesome for several reasons:

  1. You can refactor common bits of mappings into helper methods, thus keeping your mappings more DRY.
  2. It is much more refactoring friendly. It uses lambda expressions and is able to avoid strings for property names. Thus if you rename a property using .NET refactoring or Resharper, the update applies everywhere.
  3. You get intellisense as you create and edit mappings. You’d be surprised how greatly this improves the process.

Create a free website or blog at WordPress.com.