Quick Coding Tip: One Use for Extension Methods
Okay, I’m probably out of the loop on the whole coding thing, and this is probably something that’s been discussed many times, but I “discovered” this yesterday and felt I just had to share it with the programming community. As an FYI, this is dealing with C# 3.0 stuff — so you’ll need an appropriate compiler to work with it (i.e. VC# Express 2008).
First off, let me introduce the problem this is dealing with. Personally, I love working with interfaces. I probably overdo it, but it’s great to be able to write a unit test using interfaces (and mock objects), and know that your code will still work (in theory) regardless of how that interface is actually implemented. This concept works great if you break your UI up into controls, and have the controls implement your interfaces, but that’s a story for another time.
One of the main problems with this though, is that a lot of your really simple methods — copying data, equals methods, etc. — can’t be implemented in an interface, of course. Here’s how I used to get around this (and whether this is good design or not is definitely debateable):
First, a simple interface:
[codesyntax lang="csharp"]public interface INoteData {
string Title { get; set; }
string Text { get; set; }
}
[/codesyntax]
To implement a simple copy method, one that you know will work just on the interface and not on any particular implementation, you need to put that method in a different class:
[codesyntax lang="csharp"]public static class NoteManager {
public static void Copy(INoteData src, INoteData dest)
{
dest.Title = src.Title;
dest.Text = src.Text;
}
}
[/codesyntax]
And when you’re using your notes, and you want to copy it, you have to involve this extra class:
[codesyntax lang="csharp"]INoteData src = NoteFactory.CreateNote();
// do stuff with the src note
INoteData dest = NoteFactory.CreateNote();
NoteManager.Copy(src, dest);
[/codesyntax]
Wouldn’t it be cleaner to just do something like:
[codesyntax lang="csharp"]src.CopyTo(dest);[/codesyntax]
Of course, though, you can’t do that with interfaces. At least, not without forcing every different implementation to re-write the code for that particular method.
Here’s where extension methods can help. The code is really simple:
[codesyntax lang="csharp"]public static class NoteExtensions {
public static void CopyTo(this INoteData src, INoteData dest)
{
dest.Title = src.Title;
dest.Text = src.Text;
}
}
[/codesyntax]
Then, when you’re ready to use it, you just call it like above:
[codesyntax lang="csharp"]INoteData src = NoteFactory.CreateNote();
// do stuff with src.
INoteData dest = NoteFactory.CreateNote();
src.CopyTo(dest);
[/codesyntax]
And voila! Instant copy method, that applies across the board to all implementations of the INoteData interface.
Now, this example was assuredly contrived, but I’m sure you can find uses for the overall concept in your project.

![Reblog this post [with Zemanta]](http://img.zemanta.com/reblog_e.png?x-id=8d0f380b-8065-4535-8e4b-859f3a579d04)
