What is Dependency Injection (DI)?
Dependency injection is a method to replace rigid dependencies in a codebase with its configurable counterpart, by implementing a design where a dependent resource is passed as a configurable parameter.
Dependency Injection Objectives:
The following is a summary of the different approaches to work around fixed dependencies in a codebase.
Challenge
In line with the other threads; I'm going to put out a DI related challenge for anyone who wants to submit a solution and/or use the opportunity to demonstrate an alternative approach and /or to simply provide some missing clarity.
Summary of DI related application:
We're going to build a simple app that connects to the Github repository API for a configurable user's account, and then populates an array structure containing the fields we need in our interface.
JSON API: https://api.github.com/users/google/repos
Required JSON fields: "archived", "description", "html_url", "name", "pushed_at".
Dependency / Mock Requirement(s):
Naturally this type of computation is directly tied to a network based transaction, which with a rigid design would make it impossible to work on this code:
A successful solution should ideally demonstrate:
At the conclusion of this thread's challenge (+- 2 weeks), I'll be submitting examples in Swift and C#:
Preview of Swift Playground UI with Google's repositories.

Over the next 2 weeks;
I'll be posting some very basic code examples in C# and/or Swift to try demonstrate / discuss the different approaches to DI.
Dependency injection is a method to replace rigid dependencies in a codebase with its configurable counterpart, by implementing a design where a dependent resource is passed as a configurable parameter.
Dependency Injection Objectives:
- Application / Classes are independent from the creation of their dependencies.
- Separate configuration of the dependencies.
- Ability to support different configurations
- Test mocking.
- Encapsulating objects directly within the class makes the class instances inflexible because it rigidly commits a class to particular object; making it impossible to change that instantiation later without affecting the code of that class.
- More so it limits the reusability of code e.g. when the only difference is a different another configuration of the dependency.
- This rigidity also makes the class difficult to test, because live processing is innately unpredictable and so too mutability i.e. with a rigid design there is simply no way to inject a mock pure object to simulate a fixed input to output scenario.
The following is a summary of the different approaches to work around fixed dependencies in a codebase.
- Interface / Protocol and Constructor Injection
- Interface / Protocol and Property Injection
- Singleton
- Partial Application: with referential (mutable) types.
- Paradigm shift: replacing mutable surface with functional purity; including partial application with immutable types.
- Reader Monad *
- Free Monad *
* Added for completeness
As for the Reader Monad and Free Monad, both of these functional approaches will only be briefly explained in this thread so as to not detract from referential (mutable) dependency injection (versus immutability). These anyway as generic algebras deserve a separate thread for a more comprehensive overview.
Ps. Please feel free to add details of any methods you think I've failed to include, or even to provide more clarity on anything I may have overlooked, oversimplified, etc.
Challenge
In line with the other threads; I'm going to put out a DI related challenge for anyone who wants to submit a solution and/or use the opportunity to demonstrate an alternative approach and /or to simply provide some missing clarity.
Summary of DI related application:
We're going to build a simple app that connects to the Github repository API for a configurable user's account, and then populates an array structure containing the fields we need in our interface.
JSON API: https://api.github.com/users/google/repos
Required JSON fields: "archived", "description", "html_url", "name", "pushed_at".
Dependency / Mock Requirement(s):
Naturally this type of computation is directly tied to a network based transaction, which with a rigid design would make it impossible to work on this code:
- When we're coding offline
- With a different API, for example to get the latest weather forecast
- For testing; because testing typically needs a fixed input to output relationship; which cannot be guaranteed with a live API.
A successful solution should ideally demonstrate:
- Injection of a live process : connecting to Github and retrieving the repos for a nominated user
- How to substitute in a Mock dependency for both success and failure scenarios : i.e. process purity which can be used for offline coding and/or unit testing.
At the conclusion of this thread's challenge (+- 2 weeks), I'll be submitting examples in Swift and C#:
- The Swift code will demonstrate both constructor and singleton injection methods together with a rudimentary Tableview interface in Xcode playgrounds.
- The C# code will focus only a similar design to Swift for singleton injection, and the interface will be a rudimentary console (terminal) output.
Preview of Swift Playground UI with Google's repositories.

Over the next 2 weeks;
I'll be posting some very basic code examples in C# and/or Swift to try demonstrate / discuss the different approaches to DI.
Last edited: