Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Advantage of using Interface over abstract class for repository pattern? [duplicate]

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

like image 445
Ryan Avatar asked Jul 04 '12 10:07

Ryan


People also ask

What are the advantages of interface over abstract classes?

The main advantages of interface over abstract class is to overcome the occurrence of diamond problem and achieve multiple inheritance. In java there is no solution provided for diamond problem using classes. For this reason multiple inheritance is block using classes in java.

Why do we need interface for repository pattern?

A repository provides an interface to access data stored in the database or external resources. Data is returned in the form of objects. Repositories act as a bridge between the models and the controller. By using Repository Pattern we can decouple the hard dependencies of models from the controllers.

Why we use interface instead of abstract?

Abstract classes should be used primarily for objects that are closely related, whereas interfaces are best suited for providing a common functionality to unrelated classes. Interfaces are a good choice when we think that the API will not change for a while.

What is the benefit of repository pattern?

The Repository pattern makes it easier to test your application logic. The Repository pattern allows you to easily test your application with unit tests. Remember that unit tests only test your code, not infrastructure, so the repository abstractions make it easier to achieve that goal.


1 Answers

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.

like image 100
Stu Pegg Avatar answered Sep 17 '22 18:09

Stu Pegg