GithubHelp home page GithubHelp logo

injector's Introduction

Swift Injector with Xcode Macros

This project provides a lightweight dependency injection framework for Swift iOS projects built on Xcode macros. It simplifies dependency management by enabling you to mark classes with injectable properties and configure the Injector to resolve them during object creation.

Installation (SPM)

.package(url: "https://github.com/yurii-lysytsia/Injector.git", .upToNextMajor(from: "2.0.0"))

Usage

  1. Mark a class with injectable properties using the @Injectable macro. This macro informs the Injector about classes that participate in dependency injection.
@Injectable
class SomeInjectableMember { ... }
  1. Mark properties within the class as injectable using the @Injected macro. The Injector will resolve dependencies and inject them during object creation.
@Injected var int: Int
@Injected(name: "stringValue") var string: String // User `name: String` to define custom parameter's name
@Injected var intClosure: () -> Int
@Injected(escaping: true) var stringClosure: StringClosure // Use `escaping: true` to add `@escaping` modifier to init's parameter. Usually used for `typealias` closures
  1. Example
import Injector

@Injectable
public class SomeInjectableMember {
    public typealias StringClosure = () -> String

    // MARK: Injected
    
    @Injected var int: Int
    // Autogenerated code by `@Injected`
    //
    // {
    //     get {
    //         dependencies.int
    //     }
    // }
    @Injected(name: "stringValue") var string: String
    // Autogenerated code by `@Injected`
    //
    // {
    //     get {
    //         dependencies.stringValue
    //     }
    // }
    @Injected var intClosure: () -> Int
    @Injected(escaping: true) var stringClosure: StringClosure

    // MARK: Init
    
    init(dependencies: Dependencies) {
        self.dependencies = dependencies
    }
    
    // Autogenerated code by `@Injecable` macro
    //
    // public struct Dependencies {
    //     public let int: Int
    //     public let stringValue: String
    //     public let intClosure: () -> Int
    //     public let stringClosure: StringClosure
    //
    //     public init(
    //         int: Int,
    //         stringValue: String,
    //         intClosure: @escaping () -> Int,
    //         stringClosure: @escaping StringClosure
    //     ) {
    //         self.int = int
    //         self.stringValue = stringValue
    //         self.intClosure = intClosure
    //         self.stringClosure = stringClosure
    //      }
    // }
    //
    // private let dependencies: Dependencies
}

License

This project is distributed under the MIT License (see LICENSE file).

injector's People

Contributors

yurii-lysytsia avatar

Stargazers

 avatar  avatar

Watchers

 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.