Skip to content

denismurphy/swift-dependency-injection

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Dependency Injection

πŸš€ Dependency Injection

Dependency Injection (DI) solution for Swift projects. It is inspired by JSR-330 and uses a property wrapper named @Inject to inject dependencies into properties of classes, structs, and protocols.

✨ Features

  • πŸ”„ Support for both prototype and singleton scopes

  • πŸ—οΈ Initialisable protocol for classes with an init method

  • πŸ’‰ Injectable protocol for objects conforming to Initialisable

  • πŸ”— WeakObjectWrapper class for wrapping reference types as weak references

  • 🎁 Inject struct for property wrapper, enabling dependency injection into properties

  • 🧠 Context class for managing dependency registration and resolution

  • πŸ› οΈ AssemblyBuilder class for creating and configuring dependency assemblies

πŸš€ Usage

  1. Import the DependencyInjection module in your project.

  2. Use the @Inject property wrapper on properties in your classes, structs, and protocols:

class MyClass {
    @Inject var dependency: MyDependency
}
  1. Utilize the AssemblyBuilder to register dependencies and configure their scope:
let assembly = AssemblyBuilder.instance
    .add(factory: { MyDependency() }, bindingName: "MyDependency", scope: .singleton)
    .build()
  1. Use the Context class to register the assembly and resolve dependencies (e.g., in AppDelegate):
Context.instance.register(assembly)

πŸ”§ Compatibility

This library requires Swift 5.3 or later.

πŸ“¦ Installation

Install DependencyInjection using the Swift Package Manager by adding it as a dependency in your Package.swift file or directly in your Xcode project.

πŸ“„ License

This code is licensed under the MIT License.

About

Dependency Injection (DI) for Swift inspired by JSR-330

Topics

Resources

License

Stars

Watchers

Forks

Languages