Imposing Interfaces onto Classes with Extension Methods
Before you get excited, this post is just about floating an idea rather than explaining how to do something, but bear with me and see if you think it'd be handy.
Imagine you're given a class library with a Customer class in it. You want to make a nice little modal edit form so your user can edit a Customer, and the form should have an OK and Cancel button.
You put together your form, and it looks sweet. So then you start typing the code to bring up the form:
Customer cust = GetCustomer();
CustomerForm custForm = new CustomerForm(cust);
cust.BeginEdit();
if (custForm.ShowDialog() == DialogResult.OK)
{
cust.EndEdit();
}
else
{
cust.CancelEdit();
}
Looks great, huh? Except ... you compile, and you realise that the creator of the Customer class never bothered to implement the IEditableObject interface.
So now you have to write your own little helper methods to save off the properties of your Customer, and put them back when the user clicks Cancel. Ugh.
Wouldn't it be nice if you could "impose" an interface onto a class, and tell the compiler what to call when the interface methods/properties are called? So in the example above you could define the IEditableObject methods (BeginEdit, EndEdit and CancelEdit) for Customer, and tell the compiler that Customer does indeed implement IEditableObject - and to use those methods for its implementation.
I have no idea what the syntax might look like. Some take on the "this" keyword you use to define an extension method, I suppose. Perhaps something like this:
public static class CustomerStuff
{
public static void IEditableObject.BeginEdit(this Customer cust)
{
// save away the customer
}
public static void IEditableObject.CancelEdit(this Customer cust)
{
// roll the customer back
}
}
The compiler, of course, would have to complain if you didn't implement the entire interface (notice I've forgotten EndEdit in the example above).
Maybe my IEditableObject example is a bit weak, but imagine if you wanted to define your own implementation of IComparable<T> for a class that you didn't own. Or ISerializable! Would that be worth something to you?
# Trackback from Jason Haley on 13/03/2008 12:05 AM
Comments
# Nathan
14/03/2008 7:01 AM
I think this feature would be beyond useful. If you could impose an interface onto an object without modifying it's type, it would allow you to take any third party library or BCL object and use it polymorphically in your own framework without having to do something stupid, like wrap the third party object or extend it just to apply an interface. There have been many times i've been working with an object that comes with a library or exists in the BCL and noticed that it absolutely had the interface I desired, but of course it didn't specifically implement it - oh to be able to strap an interface on those without further ado...
# mabster
14/03/2008 7:23 AM
Hi Nathan,
Yeah, I find the 'partial classes' generated by service/web references to be really handy when it comes to bolting on my own interfaces and implementations. This 'interface imposition' idea would mean that I'd get that ability with *any* class, not just partial ones.
# mutzel
11/09/2009 9:34 PM
- and I thought that I am the only one thinking that this would be awesome when I asked about this on Stack Overflow (http://stackoverflow.com/questions/1409233/interface-injection).