Do you want to learn the Dependency Injection pattern using Swift? This tutorial will show you how to write loosely linked code using DI.
First and foremost, I like this little quote by James Shore:
Dependence Injection means giving an object its instance variables. Really. That's it.
In my opinion, the whole story is just a bit more complicated, but if you solve the problem to the roots, you will realize that implementation of the DI pattern can be as easy as providing an object instance variable. No nonsense, it's really a no-brainer, but many developers overcomplicating it and use injections in the wrong places. 💉
Learning DI is not about implementation details, it's about how to use the pattern. There are four small variations of addiction injections, let's review them by using examples that help you get an idea of when to use dependency injection. Now take your keypad! 💻
As I mentioned before DI is a fancy term for a single concept, you do not really need external libraries or frames to start using it. Let's imagine you have two separate objects. Object A will use object B. Hello to your first addiction.
If you hard-coded object B into object A that will not be good because from point A can not be used without B. Now, this scales up to a ~ 1
So the main goal is to create independent objects as much as possible or someone says loosely linked code to improve reusability and testability. Separation of concerns and decoupling is the right terms to use here too, because in most cases you should literally separate logical funcionalities into standalone objects. 🤐
So in theory, both objects will only do a specific thing, and the dependence between them is usually realized through a common descriptor (protocol), without the exact appearance of hardcoding. Use of addiction injection for this purpose will improve your code quality because addiction can be replaced without changing the second object's implementation. It's good for mocking, testing, reuse, etc. 😎
How do you do DI in Swift?
Swift is a wonderful programming language, with excellent support for both protocol and object-oriented principles. It also has good funcional capabilities, but let's ignore it for now. Dependence injection can be done in several ways, but in this tutorial I will focus on just a few basics without external addiction
injection . 😂
Well, let's start with a protocol, but it's just because Swift does not expose
Encoder to the public, but we need something like demos.
Property list and JSON encoder are already implementing this method, we only need to expand our objects to comply with our brand new protocol.
The most common form of addiction injection is constructor injection or initiator-based injection. The idea is that you transfer your addiction through initialization and save that object to an individual variable (private read / unchanged). The biggest advantage here is that your item will have all addiction – when it's created – in order to work properly. 🔨
You can also provide a defect value for the encoder in the constructor, but you should fear the bastard injection antimony ! This means that if the default value comes from another module, the code will be closely linked to it. So think twice! 🤔
Sometimes initialization injection is difficult to do because your class must inherit from a system class. makes the process very difficult if you have to work with views or inspectors. A good solution to this situation is to use a proprietary injection design pattern. Perhaps you do not have full control over initialization, but you can always check your properties. The only downside is that you have to check if the property is already presented (set) or not, before you do anything about it. 🤫
There are many real estate injection patterns in IOS frames, delegated patterns are often implemented like this. Another great advantage is that these properties can be mutable, so you can replace them on airplanes. ✈️
If you only need an addiction once, you do not really have to save it as an object variable. Instead of an initialization argument or an exposed mutable property, you can only pass your addiction as a method parameter, this technique is called method injection, or someone says parameter-based injection. 👍
Your addiction may vary each time this method is called, it is not necessary to keep a reference from addiction so that it is only to be used in a local method.
Our last pattern is quite dangerous one. It should only be used for universal addictions that are shared with multiple object inserts. Logging, analysis or a caching mechanism is a good example of this. 🚧
Ambient context has some disadvantages. It can fit well in the case of cross-border concerns, but it creates implicit addictions and represents a global mutable state. It is not highly recommended, you should consider the other addiction injectors first, but sometimes it may be a proper fit for you. ⚠️
It is about addiction patterns in a nutshell. If you are looking for more, read the following sources because they are absolutely amazing. Especially the first of Ilya Puchka which is highly recommended. 😉