GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - LiteCode/SwiftDI: SwiftDI the new way to use your dependency in Swift 5.1

BranchesTags
Open more actions menu

Latest commit

 

History

51 Commits
51 Commits

Folders and files

NameName
Last commit message
Last commit date
Sources/SwiftDI
Sources/SwiftDI
 
 
SwiftDIDemo
SwiftDIDemo
 
 
Tests
Tests
 
 
fastlane
fastlane
 
 
.gitignore
.gitignore
 
 
Gemfile
Gemfile
 
 
Gemfile.lock
Gemfile.lock
 
 
LICENSE
LICENSE
 
 
Package.swift
Package.swift
 
 
README.md
README.md
 
 
SwiftDI.podspec
SwiftDI.podspec
 
 

Repository files navigation

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.

About

SwiftDI the new way to use your dependency in Swift 5.1

Topics

combinedependency-injectiondslinjectionioslinterreactiveswiftswift-libraryswift5swiftuiswinject

Resources

Readme
MIT license
Activity
Custom properties

Stars

114 stars

Watchers

3 watching

Forks

6 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.