Latest 2.0.0
Homepage https://github.com/bynelus/EasyDependency
License MIT
Platforms ios 8.0
Authors

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

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

Usage

Create dependency container

import EasyDependency

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

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

Register a dependency

This way you register an implementation on a protocol.

let appContainer = AppContainer()
try appContainer.register(Storage.self) { _ in StorageAImpl() }
try appContainer.register(Storage.self) { _ in StorageBImpl() }
try appContainer.register(Storage.self, .lazySingleton) { _ 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()
try appContainer.register(Storage.self) { _ in StorageAImpl() }
try appContainer.register(String.self) { _ in "StringExample" }

let featureContainer = FeatureContainer(container: appContainer)
try 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.

Latest podspec

{
    "name": "EasyDependency",
    "version": "2.0.0",
    "summary": "EasyDependency is a very lightweight dependency injection framework, without magic.",
    "description": "EasyDependency is a very lightweight dependency injection framework, without magic. Just a container to register and resolve dependencies.nThere is no focus on adding support for circular dependencies or automatic injection of dependencies. Simplicity is key.",
    "homepage": "https://github.com/bynelus/EasyDependency",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "NielsKoole": "[email protected]"
    },
    "source": {
        "git": "https://github.com/bynelus/EasyDependency.git",
        "tag": "2.0.0"
    },
    "social_media_url": "https://twitter.com/NielsKoole",
    "platforms": {
        "ios": "8.0"
    },
    "source_files": "EasyDependency/Classes/**/*"
}

Pin It on Pinterest

Share This