Is an interface + extension methods (mixin) preferable to an abstract class?
If your answer is \"it depends\", what does it depend upon?
I see t
Downside of extension methods: clients pre-C#3/VB9 won't be able to use it as easily.
That's about it as far as I'm concerned - I think the interface-based approach is significantly nicer. You can then mock out your dependencies nicely, and everything is basically less tightly coupled. I'm not a huge fan of class inheritance unless it's really about specialization :)
EDIT: I've just thought of one other benefit which might be relevant. It's possible that some of the concrete implementations could provide more optimized versions of some of the general methods.
Enumerable.Count
is a good example of this - it explicitly checks whether the sequence implements IList
or not, because if it does it can call Count
on the list instead of iterating through the whole sequence. If IEnumerable<T>
had been an abstract class with a virtual Count()
method, it could have been overridden in List<T>
rather than there being a single implementation which knows about IList
explicitly. I'm not saying this is always relevant, nor that IEnumerable<T>
should have been an abstract class (definitely not!) - just pointing it out as a small possible disadvantage. That's where polymorphism really would be appropriate, by specializing existing behaviour (admittedly in a way which only affects performance instead of the result).
Interfaces tend to make code a little cleaner I feel it's a little easier to test. When you add Extensions your adding even more flexibility while keeping clean testable code.
For me abstract classes have always seemed clunky, using interfaces I can have an object factory that returns an object that is specific to what I'm trying to accomplish (separation of concerns).
Just making something up A have the interface called Math that has add, subtract, divide and multiply and then I have a class called IntMAth that implements Math that is optimized for integer math, and I have another class called FloatMath the implements Math that is optimized for Floating Math, and I have a generalMath that implements Math that handles everything else.
When I need to Add some floats I could call my factory MathFactory.getMath(typeof(float)) and it has some logic to know that if the type I'm passing in is a float then it returns the FloatMath class.
This way all of my classes are smaller and more maintainable, the code that calls the classes is smaller, etc.
IMHO, this is the wrong question.
You should use everything for what it is designed for.
They are static methods and can not be overridden and not be mocked in a unit test. Is is a non-OO language feature and the caller is statically bound to it.
Abstract base classes are really often misused to "reuse code" (instead of a real inheritance). This applies to inheritance in general.
The question should be: "When should I use Interfaces, extension methods or base classes?"
Edit:
Or the question should be: "How do I write reusable functionality that does not belong to a base class?"
In general I would say, extension methods are the wrong place for business logic, except of special cases or special design decisions.
Base classes are only in rare cases the right decision. In doubt, it is not. In no-doubt, you should think again about it.