All Projects → aryaxt → SwiftInjection

aryaxt / SwiftInjection

Licence: other
Dependency Injection framework for Swift

Programming Languages

swift
15916 projects
ruby
36898 projects - #4 most used programming language
objective c
16641 projects - #2 most used programming language

Projects that are alternatives of or similar to SwiftInjection

Typhoon
Powerful dependency injection for Objective-C ✨✨ (https://PILGRIM.PH is the pure Swift successor to Typhoon!!)✨✨
Stars: ✭ 2,711 (+12809.52%)
Mutual labels:  ioc, dependency-injection, ioc-container
React Ioc
Hierarchical Dependency Injection with new React 16 Context API
Stars: ✭ 133 (+533.33%)
Mutual labels:  ioc, dependency-injection, ioc-container
Singularity
A extremely fast ioc container for high performance applications
Stars: ✭ 63 (+200%)
Mutual labels:  ioc, dependency-injection, ioc-container
Typescript Ioc
A Lightweight annotation-based dependency injection container for typescript.
Stars: ✭ 427 (+1933.33%)
Mutual labels:  ioc, dependency-injection, ioc-container
Container
A lightweight yet powerful IoC container for Go projects
Stars: ✭ 160 (+661.9%)
Mutual labels:  ioc, dependency-injection, ioc-container
Di
Dependency Injection and IoC framework for PHP
Stars: ✭ 5 (-76.19%)
Mutual labels:  ioc, dependency-injection, ioc-container
ashley
Ashley is a dependency injection container for JavaScript.
Stars: ✭ 23 (+9.52%)
Mutual labels:  ioc, dependency-injection, ioc-container
ThunderboltIoc
One of the very first IoC frameworks for .Net that has no reflection. An IoC that casts its services before thunder casts its bolts.
Stars: ✭ 40 (+90.48%)
Mutual labels:  ioc, dependency-injection, ioc-container
vesselize
⛵ A JavaScript IoC container that works seamlessly with Vue.js and React.
Stars: ✭ 22 (+4.76%)
Mutual labels:  ioc, dependency-injection, ioc-container
Node Dependency Injection
The NodeDependencyInjection component allows you to standarize and centralize the way objects are constructed in your application.
Stars: ✭ 140 (+566.67%)
Mutual labels:  ioc, dependency-injection, ioc-container
Kangaru
🦘 A dependency injection container for C++11, C++14 and later
Stars: ✭ 297 (+1314.29%)
Mutual labels:  ioc, dependency-injection, ioc-container
iocgo
A lightweight Inversion of Control (IoC) (Dependency Injection) container for Golang
Stars: ✭ 36 (+71.43%)
Mutual labels:  ioc, dependency-injection, ioc-container
brisk-ioc
fast light brisk ioc/di container on nodejs; Node下快速 轻量的IoC/DI容器,依赖注入,配合装饰器使用
Stars: ✭ 12 (-42.86%)
Mutual labels:  ioc, dependency-injection, ioc-container
Poodinis
A dependency injection framework for D with support for autowiring.
Stars: ✭ 57 (+171.43%)
Mutual labels:  ioc, dependency-injection, ioc-container
alpha-dic
Powerful dependency injection container for node.js
Stars: ✭ 27 (+28.57%)
Mutual labels:  ioc, dependency-injection, ioc-container
Python Dependency Injector
Dependency injection framework for Python
Stars: ✭ 1,203 (+5628.57%)
Mutual labels:  ioc, dependency-injection, ioc-container
di
🛠 A full-featured dependency injection container for go programming language.
Stars: ✭ 156 (+642.86%)
Mutual labels:  ioc, dependency-injection, ioc-container
DependencyInjector
Lightweight dependency injector
Stars: ✭ 30 (+42.86%)
Mutual labels:  ioc, dependency-injection, ioc-container
Disco
PSR-11 compatible Dependency Injection Container for PHP.
Stars: ✭ 135 (+542.86%)
Mutual labels:  ioc, dependency-injection, ioc-container
Ioc
🦄 lightweight (<1kb) inversion of control javascript library for dependency injection written in typescript
Stars: ✭ 171 (+714.29%)
Mutual labels:  ioc, dependency-injection, ioc-container

SwiftInjection

Build Status Version

A dependency container for Swift

Setting up Dependencies

A Module file is where you define your dependencies. The goal is to abstract out all your dependencies in this file. The only class in your project that should know about concrete implementations should be the module class, the rest of the classes in your application should be using these implementations through interfaces.

You could have multiple module classes in order to organize your dependencies

public class AppModule: DIModule {
	
	public func load(container: DIContainer) {
		container.bind() { UserDefaults.standard() }
		container.bind() { URLSession.shared() }
		container.bind(type: HttpService.self) { HttpClient(baseUrl: "https://api.github.com", urlSession: $0)) }
		container.bind(type: GithubService.self) { GithubHttpClient(httpService: $0) }
		container.bind(type: AnalyticsTracker.self, named: GoogleAnalyticsTracker.analyticsIdentifier()) { GoogleAnalyticsTracker() }
		container.bind(type: AnalyticsTracker.self, named: AmplitudeAnalyticsTracker.analyticsIdentifier()) { AmplitudeAnalyticsTracker() }
	}
	
}

class AppDelegate: UIResponder, UIApplicationDelegate {
	override init() {
		super.init()
		DIContainer.instance.addModule(AppModule())
	}
}

Binding Internal classes

avoid direct use of singletons to make your code more testable

container.bind() { URLSession.shared() }

Binding classes as singleton

Instead of adding singleton logic to your classes simply bind them as singleton Note: Structs are not compatible with singleton pattern

// Bind class as singleton
bind(asSingleton: true) { Session() }

// Bind protocol to an implementation as singleton
bind(AnalyticsTracker.self, asSingleton: true) { GoogleAnalyticsTracker() }

Bind Named Instances

In cases where you have multiple implementations for a single protocol you can use named binding to retrieve the correct instance

bind(AnalyticsTracker.self, named: "Google") { GoogleAnalyticsTraker() }
bind(AnalyticsTracker.self, named: "Amplitude") { AmplitudeAnalyticsTracker() }

// Inject expected instance
let googleAnalyticsTracker: AnalyticsTracker = inject(named: "Google")
let amplitudeAnalyticsTracker: AnalyticsTracker = inject(named: "Amplitude")

// Get all implementations for a given protocol (great for chain of responssibilities)
let trackers: [AnalyticsTracker] = injectAll()

Property Injection

Only use property injection on root level, for anything else below the viewController use constructor injection

class ViewController: UIViewController {
	let githubService: GithubService = inject() // Injects the implementation defined in module
	let session = inject(Session.self) // injects the singleton instance
	let analyticTrackers: [AnalyticsTracker] = injectAll() // Injects all implemetations of AnalyticsTracker
}

Constructor Injection

Simpy pass dependencies through the intiializer and define binding in the module file

protocol GithubService { }

protocol HttpService { }

class GithubHttpClient: GithubService {
	let httpService: HttpService
	// Constructor injection
	init(httpService: HttpService) {
		self.httpService = httpService
	}
}

class AppModule: DIModule {
	func load(container: DIContainer) {
		container.bind(type: URLSession.self) { URLSession.shared() }
		container.bind(type: HttpService.self) { HttpClient(baseUrl: "https://api.github.com", urlSession: $0)) }
		container.bind(type: GithubService.self) { GithubHttpClient(httpService: $0) }
	}
}

class ViewController: UIViewController {
	// Property Injection
	// This will return an instance of GithubHttpClient with all depndencies as defined in module
	let githubService: GithubService = inject()
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].