For the needs of your application at the current level - no difference. "If it quacks like a duck and swims like a duck, then it is a duck."
This will be important when you need to implement a different behavior. Then you make a new class that implements the same interface - and in all the places where you laid on the use of a class with some interface, you can use any class that implements the interface.
One of the most typical applications in applications is for unit testing purposes.
In the test, you create a controller, but you donβt transfer a real repository that knows how to access the real database - but some class that can "pretend" for your controller repository.
For example, you can consider a book repository something that can render a list of books:
public interface IBookRepository { IEnumerable<Book> List(); }
And in the project with tests create a simple implementation:
public class TestBookRepository : IBookRepository { public TestBookRepository() { this.Books = new List<Book>() { new Book { Id = 1, Title = "ΠΊΠ½ΠΈΠ³Π° 1" }, new Book { Id = 2, Title = "ΠΊΠ½ΠΈΠ³Π° 2" }, } } private List<Book> Books { get; set; } public IEnumerable<Book> List() { return this.Books; } }
There are testing libraries that can automatically create such stubs, but they also need to know which interface they are mocking.
And in tests, check the methods of your controller using a test class.
[TestFixture] public class BooksControllerTests { [Test] public void Index_Always_ReturnsAllRecords() { // Arrange var booksRepository = new TestBookRepository(); var controller = new BooksController(booksRepository); // Act var result = controller.Index(); // Assert // Π½Π°ΠΏΡΠΈΠΌΠ΅Ρ ΠΏΡΠΎΠ²Π΅ΡΠΈΡΡ ΡΡΠΎ ΡΠΈΡΠ»ΠΎ Π·Π°ΠΏΠΈΡΠ΅ΠΉ ΡΠ°Π²Π½ΠΎ Π΄Π²ΡΠΌ ΠΈ ΠΈΡ
ΠΊΠΎΠ½ΠΊΡΠ΅ΡΠ½ΠΎΠ΅ ΡΠΎΠ΄Π΅ΡΠΆΠΈΠΌΠΎΠ΅ } }
To do this if you use the name of a particular class is either impossible or entails an excessive and unnecessary complication of this class. Also, with the help of specialized frameworks, you can be sure that a particular method is running and guarantee it in a test, etc.
An example for a question asked in the comments:
void Main() { var a = new A(); var b = new B(); //this.Test(a); //this.Test(b); } // Define other methods and classes here public class A : IBookRepository { public void GetById(int id) {} } public class B { public void GetById(int id) {} } public void Test(IBookRepository my) {} public interface IBookRepository { void GetById(int id); }