Member-only story
Seven (or So) Flavors of Dependency Injection in Swift
Dependency Injection is one of the first techniques that every programmer should learn. Let’s look at how (and maybe why) it works in Swift
One of the programmer newbies that I’ve met said to me once (brooding deeply): I thought that the bigger the class, the better… You know, then it can do more!
Time goes by, newbies become experienced, and big mighty classes lose their attraction. Programmers start realizing that they need to return to the same code over and over again, and in order to keep code maintenance effective, the code should be testable and easy to understand. This is where dependency injection comes in handy.
Note: This post is not considered as advanced and is more suitable for upper junior-intermediate level programmers. However, it’s always useful to brush up on basics even if you’re a seasoned engineer. Especially since I like to go a little bit further the main focus in my blog posts and cover useful companion techniques too.
Motivation
The main principle of the technique is to recognize an extra responsibility of a class that can be extracted out and mounted on another class (separation of concerns).