CarbonGraph - A Swift dependency injection / lookup framework for iOS

Overview

Logo Banner Light Logo Banner Dark

Swift Version  Platforms  License 

CarbonGraph is a Swift dependency injection / lookup framework for iOS. You can use it to build loose coupling between modules.

Framework Description
CarbonCore The main implementation of CarbonGraph
CarbonObjC CarbonCore's ObjC adaptation framework

Features

  • Complete dependency injection capabilities
  • Complete Objective-C support
  • Convenient object definition DSL
  • High-performance thread-safe solution
  • Support resolving native Swift types
  • Support resolving with external parameters
  • Support resolving with circular dependencies
  • Automatic scanning of configuration
  • Additional module life cycle management capabilities

Requirements

CarbonGraph Stable Version Required iOS Version Required Swift Version
1.2.2 9.0 5.2

Compatibility

Xcode Version Swift Version MacOS Version Build for distribution
11.4 5.2 Catalina 10.15.7 passing
12.1 5.3 Catalina 10.15.7 passing
12.4 5.3.2 Catalina 10.15.7 passing
12.5 5.4 Big Sur 11.6 error
12.5.1 5.4.2 Big Sur 11.6 error
13.0 5.5 Big Sur 11.6 passing

Installation

CocoaPods is a dependency manager for Cocoa projects. For usage and installation instructions, visit their website. To integrate CarbonGraph into your Xcode project using CocoaPods, specify it in your Podfile:

pod 'CarbonCore', '~> 1.2.2'

Basic Usage

  • Basic object registration and resolving
let context = ApplicationContext()
context.register(builder: Definition().object(FilePath()))
appContext[FilePath.self]
  • Use configuration batch registration
class MyConfiguration: ScannableObject, Configuration {
    static func definitions() -> Definitions {
        Definition()
            .object(FilePath())
        Definition()
            .constructor(FileModel.init(filePath:))
        Definition()
          .factory { _ in FileModel(path: “/“) }
    }
}

let context = ApplicationContext()
context.register(configuration: MyConfiguration.self)
  • Multiple object definition methods
Definition().object(FilePath())
Definition().constructor(FilePath.init)
Definition().factory { _ in FilePath() }
Definition().factory(createFilePath())

static func createFilePath(context: ObjectContext) -> FilePath { FilePath() }
  • Define protocol alias for object
Definition()
    .object(FileModel(path: "/") as FileModelProtocol)
  • Define multiple protocol aliases for the object
Definition()
    .protocol(FileManagerProtocol.self)
    .alias(ImageManagerProtocol.self)
    .alias(DirectoryManagerProtocol.self)
    .object(FileManager())
  • Use constructor for dependency injection
Definition()
    .constructor(FileModel.init(filePath:))
  • Use property for dependency injection
Definition()
    .protocol(FileViewControllerProtocol.self)
    .object(FileViewController())
    .property(\.avatarFactory)
  • Use setter for dependency injection
Definition()
    .protocol(FileViewControllerProtocol.self)
    .object(FileViewController())
    .setter(FileViewController.setAvatarFactory)
  • Use static factory for dependency injection
Definition()
    .factory(fileModel(context:filePath:))
appContext[FileModelProtocol.self]

static func fileModel2(context: ObjectContext, filePath: FilePath) -> FileModelProtocol {
    FileModel(path: filePath.path, name: filePath.name)
}
  • Use a static factory for manual dependency injection
Definition()
    .factory { ctx in FileModel(filePath: ctx[FilePath.self]) as FileModelProtocol }
appContext[FileModelProtocol.self]
  • Create objects with external parameters
Definition()
    .factory(fileModel(context:arg1:arg2:))
appContext[FileModelProtocol.self, "/china/beijing", "family.png"]

static func fileModel(context: ObjectContext, arg1: String, arg2: String) -> FileModelProtocol {
    FileModel(path: arg1, name: arg2)
}

For more usage scenarios, please refer to Netdisk Demo or related unit test cases, or contact us for help.

Unit Tests

  1. Open Carbon.xcworkspace with Xcode
  2. Execute Command + U in Xcode

Discussion

Tool Address Description
Infoflow 5346856 CarbonGraph users discussion group
Email [email protected] CarbonGraph core contributors email group

Credits

The idea of using dependency injection to build loosely coupled projects is from Spring. The implementation of using generics to obtain method parameter types is from Swinject.

Thanks for the excellent ideas and implementation provided by the above framework.

License

CarbonGraph is released under the MIT license. See LICENSE for details.

You might also like...
Injector - A Swift package for simple dependency injection that also supports Swift UI previews

A Swift package for simple dependency injection that also supports Swift UI prev

Tranquillity is a lightweight but powerful dependency injection library for swift.
Tranquillity is a lightweight but powerful dependency injection library for swift.

DITranquillity Tranquillity is a lightweight but powerful dependency injection library for swift. The name "Tranquillity" laid the foundation in the b

Kraken - Simple Dependency Injection container for Swift. Use protocols to resolve dependencies with easy-to-use syntax!
Kraken - Simple Dependency Injection container for Swift. Use protocols to resolve dependencies with easy-to-use syntax!

Kraken Photo courtesy of www.krakenstudios.blogspot.com Introduction Kraken is a simple Dependency Injection Container. It's aimed to be as simple as

Toledo - a dependency injection library for Swift that statically generates resolvers at compile-time.

Toledo Toledo is a dependency injection library for Swift that statically generates resolvers at compile-time. Index Features Installation Usage Licen

A new approach to Container-Based Dependency Injection for Swift and SwiftUI.
A new approach to Container-Based Dependency Injection for Swift and SwiftUI.

A new approach to Container-Based Dependency Injection for Swift and SwiftUI. Why do something new? Resolver was my first Dependency Injection system.

Effortless modular dependency injection for Swift.

Inject Effortless modular dependency injection for Swift. Sometimes during the app development process we need to replace instances of classes or acto

Corridor  A Coreader-like Dependency Injection μFramework
Corridor A Coreader-like Dependency Injection μFramework

Corridor A Coreader-like Dependency Injection μFramework Table of Contents Why | Examples | Usage | Installation | Credits & License | Why In order to

Deli is an easy-to-use Dependency Injection Container that creates DI containers
Deli is an easy-to-use Dependency Injection Container that creates DI containers

Deli is an easy-to-use Dependency Injection Container that creates DI containers with all required registrations and corresponding factories.

Dip is a simple Dependency Injection Container.

Dip is a simple Dependency Injection Container. It's aimed to be as simple as possible yet p

Owner
Baidu
Baidu Open Source Projects
Baidu
Dependency Injection framework for Swift (iOS/macOS/Linux)

Declarative, easy-to-use and safe Dependency Injection framework for Swift (iOS/macOS/Linux) Features Dependency declaration via property wrappers or

Scribd 684 Dec 12, 2022
Cleanse is a dependency injection framework for Swift.

Cleanse - Swift Dependency Injection Cleanse is a dependency injection framework for Swift. It is designed from the ground-up with developer experienc

Square 1.7k Dec 16, 2022
DIKit Dependency Injection Framework for Swift, inspired by KOIN.

DIKit Dependency Injection Framework for Swift, inspired by KOIN. Basically an implementation of service-locator pattern, living within the applicatio

null 95 Dec 22, 2022
Swinject is a lightweight dependency injection framework for Swift.

Swinject Swinject is a lightweight dependency injection framework for Swift. Dependency injection (DI) is a software design pattern that implements In

null 5.6k Dec 31, 2022
Swift Ultralight Dependency Injection / Service Locator framework

Swift Ultralight Dependency Injection / Service Locator framework

Michael Long 1.9k Jan 6, 2023
Needle - Compile-time safe Swift dependency injection framework

Needle is a dependency injection (DI) system for Swift. Unlike other DI frameworks, such as Cleanse, Swinject, Needle encourages hierarchical DI struc

Uber Open Source 1.4k Jan 3, 2023
Container is a lightweight dependency injection framework for Swift.

Container Container is a lightweight dependency injection framework for Swift. Installation is available in the Swift Package Manager. Swift Package M

Aleksei Artemev 17 Oct 13, 2022
Pilgrim - Dependency injection for Swift (iOS, OSX, Linux). Strongly typed, pure Swift successor to Typhoon.

pilgrim.ph Pilgrim is a dependency injection library for Swift with the following features: Minimal runtime-only library that works with pure Swift (s

AppsQuick.ly 60 Oct 24, 2022
ViperServices - Simple dependency injection container for services written for iOS in swift supporting boot order

ViperServices Introduction ViperServices is dependency injection container for iOS applications written in Swift. It is more lightweight and simple in

Siarhei Ladzeika 5 Dec 8, 2022
DIContainer Swift is an ultra-light dependency injection container made to help developers to handle dependencies easily. It works with Swift 5.1 or above.

?? DIContainer Swift It is an ultra-light dependency injection container made to help developers to handle dependencies easily. We know that handle wi

Victor Carvalho Tavernari 10 Nov 23, 2022