views:

49

answers:

2

I like the technique of creating extension methods against interfaces to provide default functionality. One of the places it is very handy is in putting some meat on dummy classes for unit tests.

For example,

public interface IEmployee
{
    IAddress Address { get; set; }

    IAddress GetAddress();
    void SetAddress(IAddress address);
}

public class Employee : IEmployee
{
    // Complex production stuff here (causes DB access)
}

public class TestEmployee : IEmployee
{
    // Simple unit testing stuff goes here (in memory only)
}

public static class EmployeeExtensions
{
    public static IAddress GetAddress(this IEmployee employee)
    {
        // Some sort of magic (maybe using IOC)
        // 'employee' can be either of type 'Employee' or of type 'TestEmployee'
    }

    public static void SetAddress(this IEmployee employee, IAddress address)
    {
        // Again with the magic
    }
}

Ok, maybe not a great example but you get the idea...

The key is that GetAddress and SetAddress can be called against both Employee and TestEmployee without code support from either of them. Unfortunately, this feels a little more like Java than C#. What I would really love to do for both is to work directly with the Address property and have it do all the same magic that the methods do. What I want to avoid is having to write code in the concrete classes to do it (especially in TestEmployee).

Is there any way to provide default implementations for the Address property of IEmployee?

I know that C# properties are actually methods under the hood. Anyway of providing implementations for those somehow?

Could it be done in a different .NET language?

A: 

As you already found out, you cannot add properties through extension methods. If it's a matter of easing the tests, you could use a mocking framework like Moq to avoid having to implement those properties on each class that inherits from the interface.

Anero
Thanks Anero. Moq is truly great but not not really what I was hoping for. Unit test are just a simple (fits in a question) example of when this would be useful. Properties are really just getter/setter methods with a nice consumption syntax. I was hoping there might be a way to pull it off. Hey, no harm in asking I guess. There are much greater geniuses than I lurking around here.
Justin
A: 

There are a variety of ways to do this, I suppose. As suggested you may look at just creating a hierarchy so that these types of functions are handled by the base class.

You may, however, be interested in a code-generation solution using T4 Templates (from Microsoft).

Noon Silk
Silky, are you suggesting I add default implementations to an abstract class? I want to be able to do it on **interfaces**. See my answer to LukeH in the question comments. If I misunderstand you, do you have any more information? I do not follow.Regardless, I have been aware of T4 for a while but have never really taken a look at it. You have given me a nudge. Thank you.PS. I believe they ship T4 with MonoDevelop these days as well.
Justin