I study application architecture on the example of asp.net mvc. I study on the site metanit. And there in the example about the onion architecture in the controller an instance of the repository interface is created. Here is the code from methanite:

IBookRepository repo; IOrder order; public HomeController(IBookRepository r, IOrder o) { repo = r; order = o; } public ActionResult Index() { var books = repo.GetBookList(); return View(); } 

But you can just as well create an instance of the repository (class instance) and the same result will be:

 BookRepository repo; Order order; public HomeController(BookRepository r, Order o) { repo = r; order = o; } public ActionResult Index() { var books = repo.GetBookList(); return View(); } 

What then is the difference ?? Create a class instance or interface instance. Explain, please.

  • The question boils down to β€œwhat are the interfaces for?”, See if one of the previously asked questions suits you. - AK ♦

1 answer 1

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); } 
  • AK thanks for the reply. Yes, I am familiar with unit testing a bit. Used library rinomok. I was just wondering how this mechanism works, in the sense that you are creating an instance of the interface, but in the end you still have an instance of the class. I understand that I can transfer an instance of any class inherited from this interface to this interface? And what if for example the class has the entire implementation of this interface with exactly the same method names, but there is no inheritance from the interface. In this case, this interface is an instance of this class? But most likely you tell me what it is to try) - Andrei
  • Not necessarily try. You can simply open any primer on c # and read the section on interfaces, explicit and implicit implementation, re-read the section on type casting - but after that be able to solve examples on online tests, interviews or in your head or decide on a number to consolidate the theory. Gave an example in the question try to guess what will happen when you run this.Test(b) ? )) Teach yourself to write such short sketches for each question that comes to your mind, and also to read books - and you will soon see a sharp increase in your knowledge of c #. - AK ♦
  • AK, the same as transferring a? Will there be an implicit implementation without inheritance? - Andrei
  • Although now I tried to do this example, I wrote that I could not give a type. It means all the same error. It will be necessary to bring - Andrew
  • Thanks for the answer - Andrew