Peter-John Welcome
Oct 18, 2017 · 2 min read

In the beginning of this year, I was looking at different ways to achieve dependency injection in Swift. A colleague of mine mentioned this concept of the Cake Pattern.

I did some research, trying to find out what this was all about and how I could use it within The Swift code base. I found that it was a concept that originates from Scala, which is based on the concept that “one should only inject via an interface (protocol), not concrete class”.

So, if this is a Scala pattern, how do we achieve this in Swift? We do so with Protocol Extensions.

Lets get to the CODE

We will start by creating a struct which will be our model. This will hold our data that we will be receiving.

We then create a protocol that will define our repository. This protocol will have a method that will fetch all of our products for us.

Now we will need some kind of implementation for our repository. We will create a ProductRepositoryImplementation struct, that will conform to our ProductRepository protocol. In this implementation we will just return some dummy products.

This is where the magic happens

Lets create another protocol and name it ProductsRepositoryInjectable. On this protocol, lets add a property of type ProductsRepository and make this a read only property. We will now create a protocol extension, that will provide us with a default implementation for this property. This being the ProductRepositoryImplementation we created earlier.

At this point, we have just achieved dependency injection and we have not used any third party libraries. Just pure Swift. What makes this so powerful, is that we can now swap out the implementation at anytime and where we are injecting this protocol will not need to change.

So, how would we use this might be the next question? Well lets create a viewmodel where we can inject the ProductsRepositoryInjectable. Within the init, we will use our injected property and loop through each product and printing the name and cost.

There you go. Dependency Injection with the awesome Cake Pattern.

Why is this useful? It makes your code more loosely coupled, testable and reusable. These are all concepts we want when writing clean code.

Go try it and let me know what you think. If you have any questions, please leave them in the comments below.

Get in Touch!

Peter John Welcome — Google+

Thanks to Ashton Welcome, Justin Guedes and Keegan Rush for reviewing this post.

Swift Programming

The Swift Programming Language

Thanks to The Coded Self

 by the author.

Peter-John Welcome

Written by

Mobile Engineering Lead, Technology Explorer, Photographer, Co-Founder, GDG Organizer

Swift Programming

The Swift Programming Language

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade