Skip to content

avreolko/VCDependencyContainer

Repository files navigation

Tests

VCDependencyContainer

Simple DI container

Installation

Install with SPM 📦

Features

  • Interface segregation. You can register one builder for multiple interfaces and then retrieve object by each on of these interfaces individually:
    protocol Foo { }
    protocol Bar { }
    class A: Foo & Bar { }

    let container = DependencyContainer()
    container.register { A() as Foo & Bar }

    let validObject = container.resolve() as Foo
    let anotherValidObject = container.resolve() as Bar
  • Lifecycle management. Manage your dependency lifecycle per container:
    class Foo { }
    class Bar { }
    
    let container = DependencyContainer()
    
    container.register(.single) { Foo() }
    let someFoo = container.resolve() as Foo
    let anotherFoo = container.resolve() as Foo
    print(someFoo === anotherFoo) // true
    
    container.register(.unique) { Bar() }
    let someBar = container.resolve() as Bar
    let anotherBar = container.resolve() as Bar
    print(someBar === anotherBar) // false
  • Optional types support:
    class Foo { }

    let container = DependencyContainer()
    container.register { Foo() }
    
    func bar(foo: Foo?) { ... }
    
    bar(container.resolve()) // valid

To do

  • Thread safety

License

This project is released under the MIT license.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages