Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repository pattern - Why exactly do we need Interfaces?

I have read from internet I got this points which says Interfaces is used for this

  • Use TDD methods
  • Replace persistance engine

But I'm not able to understand how interface will be usefull to this point Replace persistance engine. lets consider I'm creating a basic(without generics) repository for EmployeeRepository

public class EmployeeRepository {   public employee[] GetAll()   {      //here I'll return from dbContext or ObjectContex class   } } 

So how interfaces come into picture?

and if suppose i created an interface why upcasting is used ? for e.g

 IEmployee emp = new EmployeeRepository() ;  vs  EmployeeRepository emp = new EmployeeRepository(); 

Please explain me precisely and also other usefullness of Interface in regard to Repository Pattern.

like image 806
Meson Avatar asked May 16 '12 09:05

Meson


People also ask

Why repository class is an interface?

An interface defines the repository with all logical read and write operations for a specific entity. You can see an example of such a repository interface in the diagram. The interface gets implemented by one or more classes that provide data store specific implementations of each interface method.

Why do we need interface in spring?

Using Interfaces allows your classes to extend from some other classes if required. Your Interfaces can have multiple implementations and you can switch between any of them without changing the client code.

Why we use repository pattern in MVC?

The repository pattern is intended to create an abstraction layer between the data access layer and the business logic layer of an application. It is a data access pattern that prompts a more loosely coupled approach to data access.

How does repository pattern work?

Repository Design Pattern separates the data access logic and maps it to the entities in the business logic. It works with the domain entities and performs data access logic. In the Repository pattern, the domain entities, the data access logic, and the business logic talk to each other using interfaces.


1 Answers

So how interfaces come into picture ?

Like this:

public interface IEmployeeRepository {     Employee[] GetAll(); } 

and then you could have as many implementations as you like:

public class EmployeeRepositoryEF: IEmployeeRepository {     public Employee[] GetAll()     {         //here you will return employees after querying your EF DbContext     } }  public class EmployeeRepositoryXML: IEmployeeRepository {     public Employee[] GetAll()     {         //here you will return employees after querying an XML file     } }  public class EmployeeRepositoryWCF: IEmployeeRepository {     public Employee[] GetAll()     {         //here you will return employees after querying some remote WCF service     } }  and so on ... you could have as many implementation as you like 

As you can see it's not really important how we implement the repository. What's important is that all repositories and implementations respect the defined contract (interface) and all posses a GetAll method returning a list of employees.

And then you will have a controller which uses this interface.

public class EmployeesController: Controller {     private readonly IEmployeeRepository _repository;     public EmployeesController(IEmployeeRepository repository)     {         _repository = repository;     }      public ActionResult Index()     {         var employees = _repository.GetAll();         return View(employees);     }    } 

See how the controller no longer depends on a specific implementation of the repository? All it needs to know is that this implementation respects the contract. Now all that you need to do is to configure your favorite dependency injection framework to use the implementation you wish.

Here's an example of how this is done with Ninject:

  1. Install the Ninject.MVC3 NuGet
  2. In the generated ~/App_Start/NinjectWebCommon.cs code you simply decide to use the EF implementation with a single line of code:

    private static void RegisterServices(IKernel kernel) {     kernel.Bind<IEmployeeRepository>().To<EmployeeRepositoryEF>(); }         

This way you no longer need to do any manual instantiations of those repository classes and worry about upcasting or whatever. It is the dependency injection framework that manages them for you and will take care of injecting the defined implementation into the controller constructor.

And by simply modifying this configuration you could switch your data access technology without touching a single line of code in your controller. That's way unit testing in isolation also comes into play. Since your controller code is now weakly coupled to the repository (thanks to the interface we introduced) all you need to do in the unit test is to provide some mock implementation on the repository which allows you to define its behavior. This gives you the possibility to unit test the Index controller action without any dependency on a database or whatever. Complete isolation.

I also invite you to checkout the following articles about TDD and DI in ASP.NET MVC.

like image 58
Darin Dimitrov Avatar answered Sep 28 '22 21:09

Darin Dimitrov