1

Im new in design pattern and c#,i know using interface means any class inherit the interface forces to implement the method signature,in the repository pattern i see,for example we should create a IStudent repository first,like :

  public interface IStudents
{
   IEnumerable<students> getAll();
}

and here i implement it in Students:

 public class Students:IStudents
{


    public IEnumerable<students> getAll()
    {


        //return something;
    }
}

and then when i want to use in the controller first i should instantiate the IStudent class and the use its method,but instead i IStantiate the Students class and use the method directly from there,whats the advantages of this interface class here?!

moris62
  • 983
  • 1
  • 14
  • 41
  • Interfaces are like blueprints, you can use them blueprints on many objects and they will all look the same. From there you can call methods on interface objects, and they will have to be implemented. – Sasha Jul 17 '18 at 07:44
  • https://stackoverflow.com/questions/11326786/advantage-of-using-interface-over-abstract-class-for-repository-pattern – SehaxX Jul 17 '18 at 07:46
  • @Jaxi i still have not got the point,i know what interface does,but in my example what would be the advantage of it? – moris62 Jul 17 '18 at 07:51
  • 2
    Assume that one day, the implementation of Student get changed. Now you create another class call `StudentNewDBStructure:IStudents`. What you need to do in the controller now is just to change the class `IStudents _students = new StudentNewDBStructure();` . The rest of the code in your controller would remain the same. – Ppp Jul 17 '18 at 07:56

0 Answers0