As tough as the name “Dependency Injection” sounds, it’s a really simple and straightforward concept, and hence the quote from James Shore:
“Dependency Injection” is a 25-dollar term for a 5-cent concept
While I was reading about Dependency Injection in swift, I came across somewhat complex explanations for a topic that is pretty straight forward. I will keep this article really short on purpose because there's no complex thing about Dependency Injection that needs a lot of explanation or examples.
Consider the code that we see a lot on the daily basis:
… this is pretty much what Dependency Injection really is !
In case you did not understand the above piece of code, let me explain:
Without Dependency Injection: This is a situation where we do not provide a way for our object (employee1) to provide data for our properties; they are all provided internally
Initialiser based Dependency Injection: This is a situation where we can provide data to our properties through initialiser of the object (employee2)
Property based Dependency Injection: This is a situation where we can provide data to our properties by accessing the properties of the object (employee3) directly
Advantages
Ease of writing unit test cases. Real implementations can easily be substituted with mock data
It provides an improved level of flexibility and decoupling between components
Better maintainability, readability and reusability
That’s it, folks! Happy Coding!