Hi Kmcnet, please allow me to share a common scenario with you to help you understand how interface helps us.
Normally, we have interfaces and corresponding services, and we could use dependency injection to inject it in Program.cs then we can use the methods defined in services. By the way, we always create service for CRUD operations.
Let's assume we have a user table in the MySql database, and we used EF core to do CRUD operations on this user table, so that we create a UserService and IUserService for manage this table. And one day we have a requirement that we need to use Sql Server database instead, let's assume that the queries in MySql and Sql Server are different so that we need to write different EF core codes to do the query. Then we only need to create a new UserServiceForSqlServer class, and write the same method name as what we have in the origin UserService. The rest are changing the DI injection in Program.cs and other Controllers or Services use this UserService, we don't need to change any other codes.
It's a common business scenario. For example, we have an application for company A which only uses MySql and if we want to make our application to be capable for company B which only uses Sql Server, it's good idea to write 2 version CRUD operations. Since the rest part of the codes are the same, we can get benefit from defining an interface but with 2 implementations, we only need to change the DI configuration.
This is what interface helps us. Interface only define abstract method name but doesn't provide implementation. Therefore, in Controllers we only need to know what kind of method we need to call and call the corresponding Interface methods. We can recognize interface methods are defined for different requirements, such as operate table A, collect data B. The implementation might be changed in future, but the requirements might not be changed. Through the design for Interface, we can get benefit from loose coupling.
It's a good habit for us to write an interface for services which services themselves might be commonly used but shall be high probably changed, interface seems to play the role as entrance, no matter how the services change, when the result type doesn't change, we can use interface so that the part calling the service doesn't need to be changed. Certainly there're some other benefits interface bringing to us, I just show you a common scenario.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Tiny