Monday, February 23, 2009

Extension methods series: Extension points

As mentioned earlier (the basics, managing the scope, use interfaces) it is important to manage the scope of your extension methods. One other way to do that is to use extension point concept. An extension point is itself an extension method which is only purpose is to transform your object into another type on which you have define plenty of extensions.

The following sample comes from an open source project called Umbrella.

Let’s first try to wrap this concept into an interface:

public interface IExtensionPoint
{
  object ExtendedValue { get; }
  Type ExtendedType { get; }
}

This interface define the basis of an extension point. “ExtendedValue” will hold the source object reference and “ExtendedType” the type of the extended object. Now here is it generic base implementation:

public class ExtensionPoint<T> : IExtensionPoint<T>
{
  private readonly Type type;
  private readonly T value;

  public ExtensionPoint(T value)
  {
      this.value = value;
  }

  public ExtensionPoint(Type type)
  {
      this.type = type;
  }

  #region IExtensionPoint<T> Members

  public T ExtendedValue
  {
      get { return value; }
  }

  object IExtensionPoint.ExtendedValue
  {
      get { return value; }
  }

  public Type ExtendedType
  {
      get { return type ?? (value == null ? typeof (T) : value.GetType()); }
  }

  #endregion
}

This generic class will be used as a base class for all extension points. It contains all the logic to store the value and some read-only properties to get information about it.

Let’s say we want to build some xml serialization extensions, we can start by creating our xml serialization extension point:

public class SerializationExtensionPoint<T> : ExtensionPoint<T>
{
  public SerializationExtensionPoint(T value)
      : base(value)
  {
  }

  public SerializationExtensionPoint(Type type)
      : base(type)
  {
  }
}

Like I said earlier, this class doesn’t do a lot. It’s purpose is only to convert an extension point of T into a serialization extension point of T. To use this we must have a converter extension method in scope:

public static class SerializationExtensions
{
  public static SerializationExtensionPoint<T> Serialize<T>(this T value)
  {
      return new SerializationExtensionPoint<T>(value);
  }
}

The “Serialization” extension method is called to get access to all other serialization extension methods.

Somewhere in you code you will have this method. This method can be applied to any type because it takes T as a source. The last step is to define an extension method on SerializationExtensionPoint:

public static string ToXml<T>(this SerializationExtensionPoint<T> extensionPoint)
{
  using (var stream = new MemoryStream())
  {
      Xml(extensionPoint, stream, extensionPoint.ExtendedValue);

      stream.Position = 0;
      StreamReader reader = new StreamReader(stream);

      return reader.ReadToEnd();
  }
}

This method will convert any object into XML. Look how easy it is to read this: “source serialize to xml”.

var source = new List<string>();
source.Add("Test1");
source.Add("Test2");
source.Add("Test3");
var xml = source.Serialize().ToXml();

You will get:

<?xml version="1.0" ?>
<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<string>Test1</string>
<string>Test2</string>
<string>Test3</string>
</ArrayOfString>

Like any API development, one good practice to follow is to start by writing how you will use it. For example, in the last sample we could have started by writing “source.Serialize().ToXml();” and then start implementing the whole thing to make it work. The result of this is improved readability and reusability. Two things that helps a lot when someone else (or even you) have to modify your code later.

No comments: