Another Dependency Injection
Hello iOS Medium reader!
Today I would like to present to you all another way for Dependency Injection (D.I.) in Swift. I’m aware there is a lot of articles about D.I. such as:
https://www.swiftbysundell.com/articles/dependency-injection-using-factories-in-swift/
or libraries:
https://github.com/Swinject/Swinject.
So here comes the question: why I chose this topic. AGAIN…
Answer: I chosen this topic because it is highly extensive and its realization depends on knowledge of the creator’s solution.
What I would like to present is lightweight D.I. for passing protocols between View Controllers, or something else, depending on your architecture.
For this purpose, we need to declare protocol with a declaration of associated type and injection method which will inject the desired protocol.
Let’s call this protocol Injectable:
Now let’s create something where we will be able to inject the Injectable protocol. For a simple example, I will use view controller.
Let us go one step further and create BaseViewController which contains a method where we can inject our protocol. For that I will use the generics mechanism:
All right. Now we have base components so let’s do some work with them and then we will see the results.
First, we need to create a scene where we want to use the protocol. Let’s assume our FirstViewController is fetching some objects from Core Data. So the View Controller implementation will look like this:
And the CoreDataProvider implementation will look like this:
Now what we need to do is inject the CoreDataProvider into the FirstViewController. For that purpose, we create an object which will implement the Injectable protocol:
FirstViewInjector:
Ok, let’s connect these blocks!
FirstViewController:
Now inject our core data service and show FirstViewController.
AppDelegate:
If we run our code right now, we will see a yellow scene also Xcode will print:”[FirstViewController] FETCHING SOME NICE MODELS FROM CORE DATA”. At least it should be like this… I hope it is!
Let’s move on and add the second scene which will fetch our amazing model from Core Data and get something incredible from REST API from its own injector!
SecondViewInjector:
WebServiceProvider:
Second scene:
In viewDidAppear() method forFirstViewController add this code:
I think you have noticed that we have two protocols to inject into SecondViewController so in SecondViewInjector I’ve used Tuple for injecting two different types of protocol, thanks to this solutionwe can easily inject them.
Now, if we run entire code we should see:
- “[FirstViewController] FETCHING SOME NICE MODELS FROM CORE DATA”
- “[SecondViewController] FETCHING SOME NICE MODELS FROM CORE DATA”
- “[SecondViewController] GETTING REALLY AWESOME STUFF FROM REST API”
Conclusion:
With only a few lines of code, we achieved a real lightweight D.I. for protocols injection so we no need external libs. It can also be easily used in Unit Test for stubbing responses.
Please, take this as an incentive to take a moment to develop the solution by yourself instead of adding external dependencies immediately :)
Thanks for reading, and Good luck!