none
in repository design pattern why people prefer to use interface instead of abstract class RRS feed

  • Question

  • i have seen people use interface to design their repository design pattern area which interact with EF to perform CRUD operation. just like to refer a url https://www.asp.net/mvc/overview/older-versions/getting-started-with-ef-5-using-mvc-4/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application

    i like know why people use interface to design their repository design pattern area instead of abstract class.

    say for example of interface

    public interface IStudentRepository 
        {
            IEnumerable<Student> GetStudents();
            Student GetStudentByID(int studentId);
            void InsertStudent(Student student);
            void DeleteStudent(int studentID);
            void UpdateStudent(Student student);
            void Save();
        }

    instead of above interface developer can write a abstract class and define 6 abstract function inside it and as a result when concrete class will extend it then they will implement 6 function like the way they want.

    so please help me to clear my concept why develop prefer to use interface instead of abstract class when they design repository classes.

    thanks

    Wednesday, October 5, 2016 11:25 AM

Answers

  • Hi Mou_inn,

    As far as I know, the main difference between them is that a class can implement more than one interface but can only inherit from one abstract class. Since C# doesn’t support multiple inheritance, interfaces are used to implement multiple inheritance. If you does not need to implement multiple inheritance, you could also use abstract class.

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Sudip_inn Thursday, October 6, 2016 10:05 AM
    Thursday, October 6, 2016 1:10 AM
    Moderator
  • i like know why people use interface to design their repository design pattern area instead of abstract class.

    An IoC can't instantiate an abstract class,  which you have been posting about lately. An IoC uses an Interface to instantiate a class/object as defined by the IoC container. IoC is used in unit testing.

    • Marked as answer by Sudip_inn Thursday, October 13, 2016 11:14 AM
    Saturday, October 8, 2016 2:23 PM

All replies

  • Hi Mou_inn,

    As far as I know, the main difference between them is that a class can implement more than one interface but can only inherit from one abstract class. Since C# doesn’t support multiple inheritance, interfaces are used to implement multiple inheritance. If you does not need to implement multiple inheritance, you could also use abstract class.

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Sudip_inn Thursday, October 6, 2016 10:05 AM
    Thursday, October 6, 2016 1:10 AM
    Moderator
  • i like know why people use interface to design their repository design pattern area instead of abstract class.

    An IoC can't instantiate an abstract class,  which you have been posting about lately. An IoC uses an Interface to instantiate a class/object as defined by the IoC container. IoC is used in unit testing.

    • Marked as answer by Sudip_inn Thursday, October 13, 2016 11:14 AM
    Saturday, October 8, 2016 2:23 PM