Introduction

Implementing an architecture within an application can be challenging. There are rules we can follow (SOLID, Clean Architecture) and patterns to guide us (MVVM, MVP, MVI, Redux, …) but sometimes, things we thought were well established deserve a step back.

Lately, I’ve been in this situation while developing an application that relied on the use of higher order free functions.

In this article, I’ll try to guide you from the sparkle that lit this desire for higher order free functions to its implementation regarding dependency injection.

Dependency Injection

Dependency injection is a technique at the crossroads of well known software engineering best practices: abstraction and decoupling, single responsibility, dependency inversion. It provides flexible and testable implementations.

This article assumes that the merits of dependency injection are accepted. We will see how dependency injection and higher order functions fit very well together.

Implementation in a traditional way

In the rest of this article we will focus on a UsersRepository object that fetches users from a Rest API and filters them out before returning them. For the sake of the demonstration all the endpoints of this API return users but with different flavours depending on the fetched route.

#dependency-injection #swift #functional-programming #function

Functions as dependencies in Swift
1.60 GEEK