Extending ASP.NET Using C# extension methods01/19/2011
This article explains in depth how to minimize the use of a static utilities class by using extension methods. Extension methods allow you to literally extend the implementation of standard ASP.NET types (classes, server controls, etc). If you are like me you will want to skip to the good stuff (code).
As an application developer and computer science afficianado I am always concerned with the pattern of implentation. For instance, how useable or extendable is the code that you write? Good code should, of course, be optmized as well as highly portable and adaptable. But one of the most important aspects is how intuitive is the pattern or implementation. Let's take a step back and look at your code library implementations. The parts that we are concerned with are naming convention and object hierarchy. Does this name properly identify this object, is it in the right namespace, etc.?
For the most part code libraries tend to follow a similar hierarchy and naming convention. Most likely your code library has these namespaces:
These namespaces follow a similar pattern to ASP.NET which is great because we don't want to have to remember different structures for every assembly. Likewise we want to try to minimize the caveats that inherently arise in the heat of development to keep our assemblies as homogeneous as possible. Oftentimes to solve a problem it is recommended that you implement a static Utilities class to stuff methods that do not necessarily fit anywhere else in your hierarchy. This is a commonly accepted practice, however I have seen Utility classes with thousands of lines of code; many of these methods are also responsible for different things. In my opinion, this is poor pattern design.
An example of an out of control Utilities class would be a class that makes database connections, sends e-mail, modifies web controls, and so forth. The database connections should be in a Data namespace and e-mail should be in an Email namespace. Onto extending built in controls!
var tmpImage = new Image(); tmpImage.src = imgs[i].src.replace('_off', '_over');
Hard Code it:
ImgButton1.ImageUrl = "/images/path_to_file/ImgButton1_active.jpg";
ImgButton1.ImageUrl = ImgButton1.ImageUrl.Replace("_off", "_active");
public static class Utilities
public static void ImgMakeActive(ref Image img)
img.ImageUrl = img.ImageUrl.Replace("_off", "_over");
These are all acceptable solutions, however using extension methods will allow us to cut through lots of syntactic fluff. Here is what the syntax looks like using extension methods:
Pretty simple right? Also very cool because we have added a new method named "MakeActive()" to the standard System.Web.UI.Webcontrols.Image class. Ok, let's look at how we implement this. First you will want to create a new namespace. This namespace will have to be included wherever you want to use your extension methods. So let's assume that this namespace is ProjectName.Extensions. Next make a new class file for your "MakeActive" method. The class name should be "MyExtensions" or whatever you want to call it. Here is what the implementation of an extension method looks like:
public static partial class MyExtensions
public static void MakeActive(this Image img)
img.ImageUrl = img.ImageUrl.Replace("_off", "_active");
You can see that we have more or less the same code replacing "_off" with "_active"; however this puts the code in a single, central place. This is important because if we ever have to update it, we only have to go to one place. Include ProjectName.Extensions and all of your System.Web.UI.Webcontrols.Image classes will have the method "MakeActive()". You can see that the method itself takes an argument of "this Image img" with 'this' being the keyword, which turns this almost regular method into an extension method that extends the Image class.
Also it would be good to note that I have used a public static partial class. By making this class partial we are able to define this across different files in the same assembly, instead of piling all your code into one file called MyExtensions.cs you can now break it up into method files (e.g. one called MakeActive.cs and other files that define methods that belong to the class "MyExtensions").