DIContainer Swift is an ultra-light dependency injection container made to help developers to handle dependencies easily. It works with Swift 5.1 or above.

Overview

Swift

🏺 DIContainer Swift

It is an ultra-light dependency injection container made to help developers to handle dependencies easily. We know that handle with dependency injection is hard and generates boilerplate code a lot. The main idea of this lib is to keep it simples and light, it should be direct and intuitive, even using the property wrapper.

What is Dependency Injection?

The intent behind dependency injection is to achieve separation of concerns of construction and use of objects. This can increase readability and code reuse. Dependency injection is one form of the broader technique of inversion of control. A client who wants to call some services should not have to know how to construct those services. Instead, the client delegates to external code (the injector). The client is not aware of the injector.[2] The injector passes the services, which might exist or be constructed by the injector itself, to the client. The client then uses the services. Font - Wikipedia

What is DI Container?

IoC Container (a.k.a. DI Container) is a framework for implementing automatic dependency injection. It manages object creation and it's life-time, and also injects dependencies to the class. The IoC container creates an object of the specified class and also injects all the dependency objects through a constructor, a property or a method at run time and disposes it at the appropriate time. This is done so that we don't have to create and manage objects manually. Font - tutorialsteacher

Registering dependencies

To register something on Container, you can use the InjectIdentifier directly or make an extension to create helpers to identify it.

Using key directly

Container.standard.register(key: "some_key") { _ in SomeObjc() }

Using type directly

Container.standard.register(type: FetchService.self) { _ in GitHubService() }

Using type with key variants

Container.standard.register(type: FetchService.self, key: "github") { _ in GitHubService() }

Container.standard.register(type: FetchService.self, key: "gitlab") { _ in GitlabService() }

Creating shortcuts to identify by extension

extension InjectIdentifier {

    static var githubService: InjectIdentifier<FetchService> { .by(type: FetchService.self, key: "github") }
    static var gitlabService: InjectIdentifier<FetchService> { .by(type: FetchService.self, key: "gitlab") }
    static var externalService: InjectIdentifier<ExternalSingletonService> { .by(type: ExternalSingletonService.self) }
}

Registering with shortcuts

Container.standard.register(.githubService) { _ in GitHubService() }

Container.standard.register(.gitlabService) { _ in GitlabService() }

Container.standard.register(.externalService) { _ in ExternalSingletonService.shared }

Register using other dependencies

Container.standard.register(.gitlabService) { resolver in

    let externalService = try resolver.resolve(.externalService)
    return GitlabService(externalService)
}

Resolving dependencies

To resolve your dependencies, you have two ways. You can access the Container directly or use @Injected to do it automatically.

Using resolve from Container

let githubService = try? Container.standard.resolve(.githubService)
let gitlabService = try? Container.standard.resolve(.by(type: FetchService.self, key: "gitlab"))
let externalService = try? Container.standard.resolve(.by(type: ExternalSingletonService.self))

Using injection with @propertyWrapper

If you use @Injected and have not injected yet, it will call fatalError with an error message. If you do not want this behavior, you should use @InjectedSafe, but using @InjectedSafe, you will get an optional result.

@Injected(.githubService)
var githubService: FetchService

@InjectedSafe(.by(type: FetchService.self, key: "gitlab"))
var gitlabService: FetchService?

@Injected
var externalService: ExternalSingletonService

Instalation

Swift Package Manager

in Package.swift add the following:

dependencies: [
    // Dependencies declare other packages that this package depends on.
    // .package(url: /* package url */, from: "1.0.0"),
    .package(url: "https://github.com/Tavernari/DIContainer", from: "0.2.0")
],
targets: [
    .target(
        name: "MyProject",
        dependencies: [..., "DIContainer"]
    )
    ...
]

Cocoapds

pod 'DIContainer-swift', '~> 0.2.0'
You might also like...
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

Tranquillity is a lightweight but powerful dependency injection library for swift.
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

Swinject is a lightweight dependency injection framework for Swift.
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

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

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

Owner
Victor Carvalho Tavernari
iOS Mobile Engineer at @Farfetch
Victor Carvalho Tavernari
A simple way to handle dependency injection using property wrappers

Injektion Introduction A simple way to handle dependency injection using propert

Andrew McGee 2 May 31, 2022
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
Container is a lightweight dependency injection framework for Swift.

Container Container is a lightweight dependency injection framework for Swift. Installation is available in the Swift Package Manager. Swift Package M

Aleksei Artemev 17 Oct 13, 2022
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
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

AppsQuick.ly 60 Oct 24, 2022
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

null 6 Aug 9, 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