Skip to content

Instantly share code, notes, and snippets.

@RuiAAPeres

RuiAAPeres/Injection.md

Last active Mar 3, 2021
Embed
What would you like to do?
/// |                    World                 |
/// |------------------------------------------|
/// | Module A | Module B | Module C | Module D|
  1. World is a module
  2. World is aware of all modules.
  3. Modules aren't aware of World.

Each Module has this structure:

ModuleA/World.swift
ModuleA/Sources/*

In ModuleA World.swift looks like this:

public protocol World {
  var dependencyX: DependencyX { get }
  var dependencyY: DependencyY { get }
  var dependencyZ: DependencyZ { get }
}

public var Current: World!

In ModuleB:

public protocol World {
  var dependencyJ: DependencyJ { get }
  var dependencyG: DependencyG { get }
}

public var Current: World!

In the World module:

struct World {
  let dependencyX: DependencyX
  let dependencyY: DependencyY
  let dependencyZ: DependencyZ
  let dependencyJ: DependencyJ
  let dependencyG: DependencyG
  .
  .
  .
  
  init (
  ...
  ) {
     // set dependencies
     
     ModuleA.Current = self
     ModuleB.Current = self
  }
}

extension World: ModuleA.World {}
extension World: ModuleB.World {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment