David Hayden [MVP C#]

Sponsors

The Lounge

News

  • CodeBetter.Com Home

Other Links

Teas

Patterns & Practices

Florida .NET Developer

Book Reviews

Tampa ASP.NET MVC Developer Group

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Extension Methods in C# 3.0 - Shiny New Hammer Looking for a Nail :)

Scott Guthrie wrote a post on Extension Methods, called New "Orcas" Language Feature: Extension Methods, which used a similar example of Extension Methods I wrote in a post last year, called Extension Methods in C# 3.0 - C# Tutorials and Examples.

I like what Scott says at the end: "Just because you have a shiny new hammer doesn't mean that everything in the world has suddenly become a nail!" Wiser words have never been spoken, but I have never been one to miss an opportunity to goof off and abuse language enhancements Big Smile.

I have a fondness for data access and O/R Mappers and thought I would port some of the functionality in my own homegrown O/R Mapper as Extension Methods. I mentioned my ActiveRecord O/R Mapper in a couple of posts which was inspired by Castle Project's ActiveRecord:

 

Let's take a simple class as follows ( note I am using Automatically Implemented Properties in C# 3.0 ):

 

[Table("Customers")]
public class Customer : IEntity
{
    [Column]
    public string CustomerId { get; set; }
    
    [Column]
    public string Name { get; set; }
    
    [Column]
    public string EmailAddress { get; set; }
}

 

that defines a class that will be persisted to a Customers Table.

Using Extension Methods in C# 3.0 we can give this class instance a number of new methods that deal with persistence and what have ya via the IEntity Interface

 

public static class ActiveRecordExtensions
{
    public static int Save(this IEntity entity)
    {
        // Persist...
    }
}

 

As long as these Extension Methods are in scope, we can get a nice set of methods ( in this case I am only showing 1 ) to help with persistence and selection from our persistence store:

 

Extension Methods

 

In this case my IEntity Interface is nothing but a "filter" so that only a select few of classes have the ActiveRecord Extension Methods. Nothing is defined in the IEntity Interface:

 

public interface IEntity {}

 

I explicity said the Save Method will work with classes that implement IEntity:

 

public static class ActiveRecordExtensions
{
    public static int Save(this IEntity entity)
    {
        // Persist...
    }
}

 

This is definitely a shiny new hammer looking for a nail, but it is rather cool to attach behavior to objects via Extension Methods. It seems we are all suggesting not to abuse the language enhancement, but the tinkerer in me suggests otherwise Stick out tongue.

 As an aside, I love the graphical image that denotes Extension Method versus regular methods:

 

 

 

Download the Visual Studio Orcas March 2007 CTP to play with this stuff.

by David Hayden


Posted 03-28-2007 5:33 PM by David Hayden

[Advertisement]

Comments

Peter Ritchie wrote re: Extension Methods in C# 3.0 - Shiny New Hammer Looking for a Nail :)
on 03-28-2007 6:07 PM

Scott's comments hit the nail on the head.  It doesn't help that all the Microsoft Orcas demos overuse extension methods either...

Marco De Sanctis wrote re: Extension Methods in C# 3.0 - Shiny New Hammer Looking for a Nail :)
on 03-28-2007 6:35 PM

Sorry, maybe i'm missing something, but... unless you are using an ORM or something, which is able to persist an entity without explicitly specifying its type, how do you write the Save method without knowing the entity's concrete type?

David Hayden wrote re: Extension Methods in C# 3.0 - Shiny New Hammer Looking for a Nail :)
on 03-28-2007 6:45 PM

I know its concrete type because it is part of the Extension Method:

public static int Save(this IEntity entity)

{

       // Persist...

}

A simple entity.GetType() gives you the type.

A little reflection on the properties:

PropertyInfo[] propInfos = entity.GetType().GetProperties();

can give you the properties.

In my case I look for certain attributes.

Regards,

Dave

Srinivasa wrote re: Extension Methods in C# 3.0 - Shiny New Hammer Looking for a Nail :)
on 03-30-2007 10:37 AM

Automatically Implemented Properties in C# 3.0

------------------------------------------------------

Even if they had to implement Automatic Properties as part of Compiler functionality I would have preferred the following syntax

[Property]

public string CustomerId;

instead of

public string CustomerId { get; set; }

joeyDotNet wrote Introducing ActiveSupport.NET
on 02-07-2008 3:18 PM

Introducing ActiveSupport.NET