Skip to content

EasyDependency is a very lightweight dependency injection framework, without magic.

License

Notifications You must be signed in to change notification settings

pvroosendaal/EasyDependency

 
 

Repository files navigation

EasyDependency

CI Status Version License Platform Twitter

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

  • Swift 4.0

Installation

EasyDependency is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'EasyDependency', '~> 1.0'

Summary

EasyDependency is a very lightweight dependency injection framework, without magic. Just a container to register and resolve dependencies. There is no focus on adding support for circular dependencies or automatic injection of dependencies. Simplicity is key.

Features

  • Register & retrieve dependencies from a DI container.
  • Resolve list of implementations.
  • Register dependencies as singletons.

Usage

Create dependency container

import EasyDependency

class AppContainer: Container {
    var superContainer: Container?
    var registrations: [Any] = []

    required init(container: Container? = nil) {
        self.superContainer = container
    }
}

Register a dependency

This way you register an implementation on a protocol.

let appContainer = AppContainer()
appContainer.register(Storage.self) { _ in StorageAImpl() }
appContainer.register(Storage.self) { _ in StorageBImpl() }
appContainer.register(Storage.self, .singleton) { _ in StorageBImpl() }

Retrieve a dependency

You can retrieve the implementation by resolve the dependency by its interface.

let storageImplementation: Storage? = try? appContainer.resolve()
let storageList: [Storage] = appContainer.resolveList()

Feature containers

You can create feature containers including the super container. Registrations on the feature container are used in favor of the super container.

let appContainer = AppContainer()
appContainer.register(Storage.self) { _ in StorageAImpl() }
appContainer.register(String.self) { _ in "StringExample" }

let featureContainer = FeatureContainer(container: appContainer)
featureContainer.register(Storage.self) { container in StorageBImpl(string: try container.resolve()) }
let storageImplementation: Storage? = try? featureContainer.resolve() // StorageBImpl() will be returned.

Credits

This concept is created together with Jelle Heemskerk (Github).

Author

NielsKoole (Twitter)

License

EasyDependency is available under the MIT license. See the LICENSE file for more info.

About

EasyDependency is a very lightweight dependency injection framework, without magic.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 100.0%