All Projects → LiteCode → Swiftdi

LiteCode / Swiftdi

Licence: mit
SwiftDI the new way to use your dependency in Swift 5.1

Programming Languages

swift
15916 projects
dsl
153 projects

Projects that are alternatives of or similar to Swiftdi

inject
A simple Kotlin multi-platform abstraction around the javax.inject annotations.
Stars: ✭ 42 (-60.75%)
Mutual labels:  dependency-injection, injection
MVVM-Sample
Swift MVVM Sample project. Made with ReactiveCocoa, Swinject and Routers
Stars: ✭ 21 (-80.37%)
Mutual labels:  reactive, dependency-injection
Zenject-2019
Dependency Injection Framework for Unity3D
Stars: ✭ 2,567 (+2299.07%)
Mutual labels:  dependency-injection, injection
PopKorn
DI can be simple. Forget about modules and components. Just use it!
Stars: ✭ 139 (+29.91%)
Mutual labels:  dependency-injection, injection
Koin
Koin - a pragmatic lightweight dependency injection framework for Kotlin
Stars: ✭ 7,142 (+6574.77%)
Mutual labels:  dependency-injection, injection
tsdi
Dependency Injection container (IoC) for TypeScript
Stars: ✭ 50 (-53.27%)
Mutual labels:  dependency-injection, injection
fusion
A simple automated dependency injection library for TypeScript, supporting React class and functional components.
Stars: ✭ 18 (-83.18%)
Mutual labels:  dependency-injection, injection
Tsyringe
Lightweight dependency injection container for JavaScript/TypeScript
Stars: ✭ 2,761 (+2480.37%)
Mutual labels:  dependency-injection, injection
Frint
Modular JavaScript framework for building scalable and reactive applications
Stars: ✭ 608 (+468.22%)
Mutual labels:  reactive, dependency-injection
Kangaru
🦘 A dependency injection container for C++11, C++14 and later
Stars: ✭ 297 (+177.57%)
Mutual labels:  dependency-injection, injection
Reflex
Minimal dependency injection framework for Unity
Stars: ✭ 263 (+145.79%)
Mutual labels:  dependency-injection, injection
Dikit
Dependency Injection Framework for Swift, inspired by KOIN.
Stars: ✭ 77 (-28.04%)
Mutual labels:  dependency-injection, injection
AnnotationInject
Compile-time Swift dependency injection annotations
Stars: ✭ 40 (-62.62%)
Mutual labels:  dependency-injection, injection
DependencyInjector
Lightweight dependency injector
Stars: ✭ 30 (-71.96%)
Mutual labels:  dependency-injection, injection
vesselize
⛵ A JavaScript IoC container that works seamlessly with Vue.js and React.
Stars: ✭ 22 (-79.44%)
Mutual labels:  dependency-injection, injection
refuel
Lightweight dependency injection engine and DI-driven tools.
Stars: ✭ 21 (-80.37%)
Mutual labels:  dependency-injection, injection
React Ioc
Hierarchical Dependency Injection with new React 16 Context API
Stars: ✭ 133 (+24.3%)
Mutual labels:  dependency-injection, injection
opyoid
Dependency injection library for Python
Stars: ✭ 34 (-68.22%)
Mutual labels:  dependency-injection, injection
Poodinis
A dependency injection framework for D with support for autowiring.
Stars: ✭ 57 (-46.73%)
Mutual labels:  dependency-injection, injection
Container Ioc
Inversion of Control container & Dependency Injection for Javascript and Node.js apps powered by Typescript.
Stars: ✭ 89 (-16.82%)
Mutual labels:  dependency-injection, injection

SwiftDI

SwiftDI it's a tool for Dependency Injection using @propertyWrapper. Right now SwiftDI is alpha version. Be careful!

SwiftDI works with Swift 5.1 only and SwiftUI.

Please looks at our demo SwiftDIDemo!

How it use?

  1. Create your container:
let container = DIContainer()
  1. Create your assemblies extended from DIPart:
class MyAssembly: DIPart {
    var body: some DIPart {
        // Some Assembly parts place here
    }
}
  1. Register your objects:
class MyAssembly: DIPart {
    var body: some DIPart {
        DIRegister(MyService.init)
    }
}

you can use as<T>(_ type: T.Type) for set a new injection identifier:

class MyAssembly: DIPart {
    var body: some DIPart {
        DIRegister(MyService.init)
            .as (MyServiceInput.self)
    }
}
  1. Load your DIPart to the container:
let container = DIContainer(part: MyAssembly())

or

let container = DIContainer()
container.appendPart(MyAssembly())

  1. Set your container to SwiftDI:
SwiftDI.useContainer(container)
  1. Use your DI
class MyController: UIViewController {
    @Injected var service: MyServiceInput
}

Does it! You're finish setup your DI container.

DSL

  1. DIGroup - Contains one or many DIParts
DIGroup {
    // Place you DIParts here
}
  1. DIRegister - Register some object
DIRegister(MyService.init)

also contains methods lifeCycle() and as()

SwiftDI ❤️ SwiftUI!

SwiftDI also supports SwiftUI framework. You can inject your BindableObject and property automatically connect to view state. For this magic just use @EnvironmentObservedInject

struct ContentView: View {
	
	@EnvironmentObservedInject var viewModel: ContentViewModel

	var body: some View {
		HStack {
			Text(viewModel.title)
		}.onAppear { self.viewModel.getData() }
	}
}

For non-mutating view object use @EnvironmentInject:

struct ContentView: View {
	
	@EnvironmentInject var authService: AuthorizationService

	var body: some View {
		HStack {
			Text("Waiting...")
		}.onAppear { self.authService.auth() }
	}
}

By default SwiftDI using shared container, but if you wanna pass custom container to view using method inject(container:) for view:

let container = DIContainer()

let view = HomeView().inject(container: container)

Or if you wanna add some method to container using method environmentInject:

// SceneDelegate.swift

let window = UIWindow(windowScene: windowScene)
let authService = AuthorizationService(window: window)

let view = HomeView().environmentInject(authService)

// etc

Scopes

SwiftDI supports object scopes, you can use method lifeCycle

DIRegister(MyService.init)
	.lifeCycle(.single)

By default life time for instance is objectGraph. objectGraph using Mirror for get info about nested injectables property and it can be slowly.

How it works?

SwiftDI using @propertyWrapper to use power of injection. @Inject it's struct uses SwiftDI.sharedContainer for resolve objects when value is call.

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].