GithubHelp home page GithubHelp logo

ivanlisovyi / dep Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 0.0 15 KB

Framework that implements a dependency injection using the Service Locator pattern and provides a convenient way to use these dependencies using @Dependency property wrapper

License: MIT License

Swift 100.00%
dependency-injection service-locator property-wrapper swift-package swift-framework swift

dep's Introduction

Dep

Build Status

Dep is a micro framework that implements a dependency injection using the Service Locator pattern and provides a convenient way to use these dependencies using @Dependency property wrapper

Examples

Basic Usage Example

// Define a container and register your dependencies 
let container = Container()
       .register(AnyDecoder.self) { _ in JSONDecoder() }
       .register(AnyEncoder.self) { _ in JSONEncoder() }

// Resolve dependency using the previously defined container 
let dependency = container.resolve(AnyDecoder.self)

Advanced Usage Example

It is possible to register a dependency that expect its dependencies injected onto it in some way. For this purpose use the factory closure parameter

let container = Container()
       .register(AnyDecoder.self) { _ in JSONDecoder() }
       .register(AnyEncoder.self) { _ in JSONEncoder() }
       .register(ComplexObject.self) { resolver in 
          let decoder = resolver.resolve(AnyDecoder.self)
          let encoder = resolver.resolve(AnyEncoder.self)
          return ComplexObject(decoder: decoder, encoder: encoder)
       }

// Resolve dependency using the previously defined container 
let dependency = container.resolve(ComplexObject.self)

@Dependency Property Wrapper Usage

let defaultContainer = Container()
       .register(AnyDecoder.self) { _ in JSONDecoder() }
       .register(AnyEncoder.self) { _ in JSONEncoder() }


class ComplexObject {
  @Dependency(container: defaultContainer) var decoder: AnyDecoder 
  @Dependency(container: defaultContainer) var encoder: AnyEncoder
}

Unit Testing

To test the classes that use @Dependency property wrapper it is possible to overwrite the container used to define a dependency

let testContainer = Container()
    .register(AnyDecoder.self) { _ in JSONDecoder() }
    .register(AnyEncoder.self) { _ in JSONEncoder() }
    .register(ComplexObject.self) { resolver in
        var result = ComplexObject()
        result.$decoder.container = resolver // overwrites decoder container
        result.$encoder.container = resolver
        return result
    }

class ComplexObjectsTests: XCTestCase {
  @Dependency(container: testContainer) var complexObject: ComplexObject

  func testThatItWorks() {
    XCTAssertTrue(complexObject.doesSomething())
  }
}

Installation

Swift Package Manager

The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift compiler.

Once you have your Swift package set up, adding Dep as a dependency is as easy as adding it to the dependencies value of your Package.swift or since XCode 11 you can do the same through XCode UI

dependencies: [
    .package(url: "https://github.com/kshin/Dep.git", from: "1.0.2")
]

Requirements

  • Xcode 11
  • Swift 5.1

Licence

The MIT License (MIT)

Copyright (c) 2019 Ivan Lisovyi

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

dep's People

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.