Search code examples
dependency-injectionloose-coupling

Loose coupling in c#


Is dependency injection is the only way to achieve loose coupling in c# ?? I have been going through injection (DI), to solve the tight coupling, I found DI as one solution for that ! Buy I'm looking for other solutions even


Solution

  • your question is a bit abstract. you first need to define your problem. But, in order to write a loosely coupled, you'll need to use abstractions and not implementations (so you can inherit and override whatever you want) and inject the abstractions with dependency injection to your class/methods.

    that way you are not coupled to the specific implementation of the code and whoever wants can just implement the interface and pass it to the class/method.