I am reading Microsoft's documentation on dependency injection, and I keep seeing the same pattern. The author will define some kind of service, create an interface for the service, and then add the interface to the service collection as an injectable dependency.
public class MyService : IMyService {
}
_
public interface IMyService {
}
Then in startup, in your ConfigureServices method, you register your service like this
services.AddScoped<IMyService>();
Then in a controller or another class, you will inject the interface as a dependency
public class SampleDataController : Controller {
private readonly IMyService _service;
public SampleDataController(IMyService service) {
_service = service;
}
It seems to me like you could just as easily do away with the interface. What are the benefits of defining your dependencies in this way?
Aucun commentaire:
Enregistrer un commentaire