Skip to content

Latest commit

 

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DIKit

A tiny dependency injection container for Swift. No third-party dependencies, works on iOS and macOS.

Licensed under MIT.

Installation

.package(url: "https://github.com/PedroAugusto01/DIKit", from: "1.0.0")
.product(name: "DIKit", package: "DIKit"),

How it works

DIContainer maps a type to a factory closure. register stores the factory; resolve calls it (or returns the cached instance, for singletons):

let container = DIContainer()

container.register(Logging.self, scope: .singleton) { _ in ConsoleLogger() }
container.register(UserService.self) { resolver in
    UserService(logger: resolver.resolve(Logging.self))
}

let service: UserService = container.resolve(UserService.self)
  • scope: .transient (the default) builds a fresh instance on every resolve.
  • scope: .singleton builds the instance once, on first resolve, and reuses it after that.
  • register(_:instance:) registers an already-built value as a singleton directly.
  • Factories receive the container itself, so a service can resolve its own dependencies from it — nested resolution doesn't deadlock, since the container never holds its lock while running a factory.
  • resolve(_:) traps with a descriptive message if nothing was registered for that type — a missing registration is a wiring mistake, not something to recover from at runtime. Use tryResolve(_:) if you'd rather get nil.

@Injected

For call sites that would rather not thread a container through every initializer:

final class ProfileViewModel {
    @Injected private var userService: UserService
}

By default it resolves from DIContainer.shared, the process-wide default container. Pass a different one (@Injected(container: myContainer)) when you're managing your own — most tests should.

Demo

swift run DIKitDemo

Tests

swift test

About

A tiny dependency injection container for Swift, with transient/singleton scopes and an @injected property wrapper.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages