I have read about IOC/DI for dependency injection. It says to create interfaces of every class that we wish to inject. Doesn’t it violates the definition of interface(implement where we want to enforce behavior in a class). Any help will be highly appreciated. Thanks.
I have read about IOC/DI for dependency injection. It says to create interfaces of
Share
I think your main point of confusion is that in a DI/IoC scenario, we don’t have a class we want to inject, we have a service we want to inject. Let’s say I have a
Loggerclass. It’s not theLoggerclass itself I want to inject, it’s the set of logging services described by theILoggerinterface. If I wasn’t using DI or IoC, I would have a place in each class where I would goILogger _logger = new Logger(), or maybeILogger _logger = Logger.Instance, both of which couple my code to a specific instance of theILoggerinterface. But with DI/IoC, I leave this step to the framework, and don’t bother with the specific implementation in my business logic class.