Container is a lightweight dependency injection framework for Swift.

Overview

Container

Container is a lightweight dependency injection framework for Swift.

Installation

is available in the Swift Package Manager.

Swift Package Manager

https://github.com/bartleby/Container.git

Basic Usage

Firstly, you must defined your containers

extension Container {
    static let services = Container(ContainerConfigurator.services)
    static let assemblies = Container(ContainerConfigurator.assemblies)
    static let appearance = Container(ContainerConfigurator.appearance)
}

struct ContainerConfigurator {
    static var services: DependencyResolver {
        let container = DependencyContainer()
        
        container.apply(Service() as ServiceProtocol)
        container.apply(ServiceTwo(), label: .two)
        
        return container
    }
    
    static var assemblies: DependencyResolver {
        let container = DependencyContainer()
        
        container.apply(MainAssembly())
        container.apply(AuthorizationAssembly())
        container.apply(RegistrationAssembly())
        container.apply(OnboardingAssembly())
        container.apply(SettingsAssembly())
        
        return container
    }
    
    static var appearance: DependencyResolver {
        let container = DependencyContainer()
        
        container.apply(Color.red, label: .redColor)
        
        return container
    }
}

Then get an instance of a service from the container.

let resolver = Container.services.resolver
let service = resolver.resolve(Service.self, scope: .unowned)
service.run()

You can also use Propertywrapers

class ViewModel {

    @Dependency(.services, scope: .strong) var config: AppConfigServiceProtocol
    
    //...
    
    func setup() {
        let token = config.obtain(for: .token)
        //...
    }
    
}

Scope

You can specify the scope for your dependencies, .weak .strong and .unowned

.weak scope is used by default

@Dependency(.services) var service: ServiceProtocol // .weak scope is used by default

How it's work

  • weak Keeps the object in memory while at least one object refers to it

  • strong An analogue of Singletone, after creating the object, it always remains in memory even if no one refers to it

  • unowned The object is not stored in memory and each time create a new object

Labels

For the same types, you can specify Label For exemple:

container.apply(Color.red, label: .redColor)
container.apply(Color.green, label: .greenColor)
container.apply(Color.orange, label: .orangeColor)

In order to create your own Label, you need to extension the structure of DependencyLabel

extension DependencyLabel {
    static let redColor = ContainerLabel(value: "redColor")
    static let greenColor = ContainerLabel(value: "greenColor")
    static let orangeColor = ContainerLabel(value: "orangeColor")
}

Then you can specify a label when using

@Dependency(.appearance, scope: .strong, label: .redColor) var redColor: Color

Assembly module

You'r also can use Assembly for assembly your Modules with Container

First, register a Assembly to a Container

extension Container {
    static let assembly = Container(ContainerConfigurator.assemblies)
    //...
}

struct ContainerConfigurator {
    
    static var assemblies: DependencyResolver {
        let container = DependencyContainer()
        
        container.apply(MainAssembly())
        container.apply(AuthorizationAssembly())
        container.apply(RegistrationAssembly())
        container.apply(OnboardingAssembly())
        container.apply(SettingsAssembly())
        
        return container
    }
    
    //...
}

Then implement the MainAssembly class

protocol Assembly {
    associatedtype C: CoordinatorType
    associatedtype M
    
    func build(coordinator: C) -> M
}

typealias MainModule = Module<MainModuleInput, MainModuleOutput>

struct MainAssembly: Assembly {
    @Dependency(.services, scope: .strong) var config: AppConfigServiceProtocol
    
    func build(coordinator: MainCoordinator) -> MainModule {
        
        // View
        let view = MainViewController()
        
        // Router
        let router = MainRouter(coordinator: coordinator)
        
        // ViewModel
        let viewModel = MainViewModel(router: router, config: config)
        
        // Configure
        viewModel.view = view
        view.output = viewModel
        
        return Module(view: view, input: viewModel, output: viewModel)
    }
}

How to usage

class MainCoordinator: BaseCoordinator {
    
    // MARK: - Dependencies
    @Dependency(.assemblies) var mainAssembly: MainAssembly
    
    override func start() {
        let module = mainAssembly.build(coordinator: self)
        router.setRootModule(module)
    }
}

Example Apps

Coming soon

License

MIT license. See the LICENSE file for details.

You might also like...
Dependency Injection framework for Swift (iOS/macOS/Linux)
Dependency Injection framework for Swift (iOS/macOS/Linux)

Declarative, easy-to-use and safe Dependency Injection framework for Swift (iOS/macOS/Linux) Features Dependency declaration via property wrappers or

Swift Ultralight Dependency Injection / Service Locator framework
Swift Ultralight Dependency Injection / Service Locator framework

Swift Ultralight Dependency Injection / Service Locator framework

Needle - Compile-time safe Swift dependency injection framework
Needle - Compile-time safe Swift dependency injection framework

Needle is a dependency injection (DI) system for Swift. Unlike other DI frameworks, such as Cleanse, Swinject, Needle encourages hierarchical DI struc

CarbonGraph - A Swift dependency injection / lookup framework for iOS

CarbonGraph is a Swift dependency injection / lookup framework for iOS. You can

Pilgrim - Dependency injection for Swift (iOS, OSX, Linux). Strongly typed, pure Swift successor to Typhoon.

pilgrim.ph Pilgrim is a dependency injection library for Swift with the following features: Minimal runtime-only library that works with pure Swift (s

Injector - A Swift package for simple dependency injection that also supports Swift UI previews

A Swift package for simple dependency injection that also supports Swift UI prev

Toledo - a dependency injection library for Swift that statically generates resolvers at compile-time.

Toledo Toledo is a dependency injection library for Swift that statically generates resolvers at compile-time. Index Features Installation Usage Licen

Effortless modular dependency injection for Swift.

Inject Effortless modular dependency injection for Swift. Sometimes during the app development process we need to replace instances of classes or acto

Corridor  A Coreader-like Dependency Injection μFramework
Corridor A Coreader-like Dependency Injection μFramework

Corridor A Coreader-like Dependency Injection μFramework Table of Contents Why | Examples | Usage | Installation | Credits & License | Why In order to

Owner
Aleksei Artemev
iOS, Mac Developer & Designer. I code and design great simple things. It is a passion of my life.
Aleksei Artemev
Kraken - Simple Dependency Injection container for Swift. Use protocols to resolve dependencies with easy-to-use syntax!

Kraken Photo courtesy of www.krakenstudios.blogspot.com Introduction Kraken is a simple Dependency Injection Container. It's aimed to be as simple as

Syed Sabir Salman-Al-Musawi 1 Oct 9, 2020
ViperServices - Simple dependency injection container for services written for iOS in swift supporting boot order

ViperServices Introduction ViperServices is dependency injection container for iOS applications written in Swift. It is more lightweight and simple in

Siarhei Ladzeika 5 Dec 8, 2022
A new approach to Container-Based Dependency Injection for Swift and SwiftUI.

A new approach to Container-Based Dependency Injection for Swift and SwiftUI. Why do something new? Resolver was my first Dependency Injection system.

Michael Long 573 Jan 2, 2023
Deli is an easy-to-use Dependency Injection Container that creates DI containers

Deli is an easy-to-use Dependency Injection Container that creates DI containers with all required registrations and corresponding factories.

Jungwon An 134 Aug 10, 2022
Dip is a simple Dependency Injection Container.

Dip is a simple Dependency Injection Container. It's aimed to be as simple as possible yet p

Olivier Halligon 949 Jan 3, 2023
Injection - Dependency injection using property wrappers

Dependency injection using property wrappers. Registering types: // injecting a

Alejandro Ramirez 3 Mar 14, 2022
Swinject is a lightweight dependency injection framework for Swift.

Swinject Swinject is a lightweight dependency injection framework for Swift. Dependency injection (DI) is a software design pattern that implements In

null 5.6k Dec 31, 2022
Tranquillity is a lightweight but powerful dependency injection library for swift.

DITranquillity Tranquillity is a lightweight but powerful dependency injection library for swift. The name "Tranquillity" laid the foundation in the b

Ivlev Alexander 393 Dec 24, 2022
Cleanse is a dependency injection framework for Swift.

Cleanse - Swift Dependency Injection Cleanse is a dependency injection framework for Swift. It is designed from the ground-up with developer experienc

Square 1.7k Dec 16, 2022
DIKit Dependency Injection Framework for Swift, inspired by KOIN.

DIKit Dependency Injection Framework for Swift, inspired by KOIN. Basically an implementation of service-locator pattern, living within the applicatio

null 95 Dec 22, 2022