Dependency Injection in Swift (Mini Cake solution)

I already talked about the subject of Dependency Inversion here and of course the way to implement Dependency Inversion is to use Dependency Injection.

First let’s go back to what is Dependency Injection. Dependency injection is used to inject real implementations into a class that uses abstract interfaces instead. So let’s say a Presenter Class uses of View Class. If you are using SOLID, then your Presenter Class will “see” the View Class thru an abstract interface (a contract) and thus the dependency to the View Class will be reversed as you can see in the Fig-1.

Fig-1 : Presenter-Interface-View

When this is done, you can “insert” whatever View in the Presenter as long as it respects the View Protocol contract.

To do the insertion, you will use Dependency Injection. There are 2 types of Dependency Injection :

  1. Dependency Injection at instanciation
  2. Dependency Injection by modification

Continue reading “Dependency Injection in Swift (Mini Cake solution)”

Protect yourself from models !

You probably use models in your code every day and so do I. The major problem with a model is that it will surely evolve. It will evolve because the business will require a change or because a new feature will be needed and thus will require the model to change or because the api will have new fields or different informations. The best case is that this evolution will not touch your code, the worst case is that this evolution will destroy your code. But in any case you depend on this model and this is generally bad… Continue reading “Protect yourself from models !”