Giter Site home page Giter Site logo

minsone / dicontainer Goto Github PK

View Code? Open in Web Editor NEW
28.0 2.0 0.0 1.16 MB

DIContainer is lightweight dependency injection container framework for Swift

License: MIT License

Swift 100.00%
dependency-injection di-container inversion-of-control ioc-container ios swift

dicontainer's Introduction

Dependency Injection Container

English | 한국어

Overview

The Swift Dependency Injection Container is a lightweight and flexible library designed to facilitate dependency management in Swift applications. It provides a structured and type-safe approach to resolve dependencies throughout your codebase, promoting code reusability, testability, and maintainability.

Features

  • Type-safe dependency resolution.
  • Lazy instantiation of dependencies.
  • Property wrappers for convenient dependency injection.
  • Dynamic module registration and management.
  • Result builder syntax for declarative module registration.
  • Debug utilities for module and injection key scanning.

Requirements

  • Swift 5.9+

Installation

DIContainer is available Swift Package Manager.

Swift Package Manager

in Package.swift add the following:

dependencies: [
    .package(url: "https://github.com/minsOne/DIContainer.git", from: "1.0.0")
]

Usage

Basic Usage

First, register a key and module pair to a Container, where the module has concreate type. Key has protocol type, and concreate type is inheriting protocol type

Container {
    Module(AnimalKey.self) { Cat() }
}
.build()

Then get an instance from the container.

@Inject(AnimalKey.self)
var cat: Meow
cat.doSomething() // prints "Meow.."

Where definitions of the protocols and struct are

class AnimalKey: InjectionKey {
    var type: Meow?
}

protocol Meow {
    func doSomething()
}

struct Cat: Meow {
    func doSomething() {
        print("Meow..")
    }
}

Test

$ (cd MockData/Sources/MockData && for i in {1..20}; do cp MockClass.swift MockClass$i.swift; done)
$ swift test
$ swift test -Xswiftc -O

Contributing

Contributions to the Swift Dependency Injection Container are welcome. Here are ways you can contribute:

  • Reporting issues
  • Suggesting enhancements
  • Submitting pull requests with bug fixes or new features

Please ensure to follow the coding standards and write tests for new functionality.

License

This project is licensed under the MIT License.

Post

Credits

The DIContainer are inspired by:

dicontainer's People

Contributors

minsone avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  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.