Dependency inversion and how it could be very useful
Eslam Mohamed
Experienced Senior iOS Developer | @X-ERA | Crafting High-Performance Mobile Solutions
in brief Dependency inversion means high level Modules shouldn't depend on low level modules
here the CoreDataController is a low level module, its easy to reuse in other projects, the problem is with high level module ConversationDataController, its tightly coupled with CoreDataController.
we cannot use the ConversationDataContoller into another app easily because it is tightly coupled with CoreDataController or in other words it has a dependecy in low level module CoreDataController.
what is loosely coupled and tightly coupled anyway ?
-Tight coupling refers to a situation where two or more software components are closely connected and depend on each other to function properly.
-Loose coupling, on the other hand, means that the components are less dependent on each other and can operate more independently
We can solve this dependency using a Database Protocol. In this way ConversationDataController can use abstract protocol without caring for type of database used, lets see how the ConversationDataController will look after applying this.
now you can reuse conversationDataController without hazzle into another project with implementing new class which confirms to Database Abstraction or Protocol.