Possible Duplicate:
Interface vs Base class
Its common to see the repository pattern implemented using Interfaces
public interface IFooRepository
{
Foo GetFoo(int ID);
}
public class SQLFooRepository : IFooRepository
{
// Call DB and get a foo
public Foo GetFoo(int ID) {}
}
public class TestFooRepository : IFooRepository
{
// Get foo from in-memory store for testing
public Foo GetFoo(int ID) {}
}
But you could equally do this using abstract classes.
public abstract class FooRepositoryBase
{
public abstract Foo GetFoo(int ID);
}
public class SQLFooRepository : FooRepositoryBase
{
// Call DB and get a foo
public override Foo GetFoo(int ID); {}
}
public class TestFooRepository : FooRepositoryBase
{
// Get foo from in-memory store for testing
public override Foo GetFoo(int ID); {}
}
What are the specific advantages of using an Interface over an Abstract Class in a repository scenario?
(i.e. don’t just tell me that you can implement multiple interfaces, I know this already – why would you do that in a repository implementation)
Edit to clarify – pages like “MSDN – Choosing Between Classes and Interfaces” can be paraphrased as “Choose classes over interfaces unless there is a good reason not to” – what are the good reasons in the specific case of a Repository pattern
The main advantage of using an interface over an abstract class in this instance is that an interface is entirely transparent: This is more of an issue where you don’t have access to the source of the class you’re inheriting from.
However, this transparency allows you to produce unit tests of a known scope: If you test a class that accepts an interface as a parameter (using the dependency injection method), you know you’re testing the class with a known quantity; the testing implementation of the interface will only contain your testing code.
Similarly, when testing your repository, you know you’re testing just your code in the repository. This helps to limit the number of possible variables/interactions in the test.