Skip to content

A dependency injection library written with SwiftUI's reactivity in mind

Notifications You must be signed in to change notification settings

EbrahimTahernejad/DependencyFactory

Repository files navigation

DependencyFactory

A simple dependency factory library

Usage

Define a service

import DependencyFactory

protocol StorageServiceProtocol: BaseService {
    var token: String? { get set }
}

class StorageService: BaseService, StorageServiceProtocol {
    @Stored(key: "Token", in: .keychain) var token: String?
}

Add it to container

import DependencyFactory

extension Container {
    static var storage: Factory<StorageServiceProtocol> {
        Factory(self) { StorageService() }
    }
}

Inject

class ViewModel: BaseViewModel {
    @Injected(\.storage) var storage
}

Register another class

class MemoryStorageService: BaseService, StorageServiceProtocol {
    var token: String? 
}
Container.shared.manager.register { () -> StorageServiceProtocol in
    MemoryStorageService()
}

About

A dependency injection library written with SwiftUI's reactivity in mind

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages