https://github.com/bkase/swift-di-explorations
Functional DI explorations in Swift
https://github.com/bkase/swift-di-explorations
dependency-injection di functional swift typesafe
Last synced: 6 months ago
JSON representation
Functional DI explorations in Swift
- Host: GitHub
- URL: https://github.com/bkase/swift-di-explorations
- Owner: bkase
- Created: 2017-05-31T05:57:31.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-10-25T02:44:33.000Z (almost 8 years ago)
- Last Synced: 2025-03-29T04:23:23.432Z (6 months ago)
- Topics: dependency-injection, di, functional, swift, typesafe
- Language: Swift
- Size: 728 KB
- Stars: 28
- Watchers: 2
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Functional DI Solutions in Swift
Our code has dependencies. We would like to not directly invoke code that uses side-effecting singleton-esque services because our code becomes harder to reason about, harder to test, etc. It would be nice to decouple the effects.
Dependency injection is about decoupling your code from your effects. In traditional OO languages, it's tough to get a compile-time-checked type-safe DI solution working, but Swift is functional(ish).
This repository explores the "functional" solutions to DI that are used in other functional languages (Scala, Haskell, etc)
## Understanding the solutions
In this repo, every implemenation must declare a dependency on some datastore that has the capability to get a string key-value pair given a string key, and set arbitrary key-value pairs given a key and a value.
In all cases, we define a simple program that sets a key-value pair, and then gets that key and uses it to say "hello".
The programs are declared here in literate Swift:
* [Cake Pattern](Sources/di-playground/Cake.swift)
[](Sources/di-playground/Cake.swift)
* [Reader Monad](Sources/di-playground/Reader.swift)
[](Sources/di-playground/Reader.swift)
* [Free Monad](Sources/di-playground/Free.swift)
[](Sources/di-playground/Free.swift)
The programs are then all evaluated in this
* [Test file](Tests/di-playgroundTests/di_playgroundTests.swift)
[](Tests/di-playgroundTests/di_playgroundTests.swift)