Skip to content

Instantly share code, notes, and snippets.

Avatar

vedon vedon

View GitHub Profile
View Feature2.swiftdeps
### Swift dependencies file v0 ###
provides-top-level:
- "Feature2"
provides-nominal:
- "8Feature2AAC"
provides-member:
- ["8Feature2AAC", ""]
provides-dynamic-lookup:
- "doSomething"
- "feature1"
View PrivateModel.sh
/usr/bin/swift
-frontend -c /Feature3.swift
-primary-file PrivateModel.swift
-emit-module-path /PrivateModel\~partial.swiftmodule
-emit-module-doc-path /PrivateModel\~partial.swiftdoc
View DITests.swift
func testFeature2() {
let dependencies: Feature2.Dependencies = AnyInitializer(Feature2.Dependencies.init).resolve(by: container)
let f2 = Feature2(dependencies: dependencies)
f2.doSomething()
}
output:
Test Case '-[DITests.DITests testFeature2]' started.
View Feature1.swift
import FeatureService
extension Feature1: Feature1Service {
public func doSomething() {
print("Feature1 do something")
}
}
View Feature2.swift
import Foundation
import FeatureService
public class Feature2 {
public struct Dependencies {
let feature1: Feature1Service
let feature3: Feature3Service
public init(feature1: Feature1Service, feature3: Feature3Service) {
self.feature1 = feature1
View AppContext.swift
class AppContext {
let container: DIContainer
init() {
self.container = DIContainer.init()
ServiceAssembly.assembled(in: container)
}
}
View DIContainer.swift
import Foundation
import Swinject
public typealias DIContainer = Container
public final class AnyInitializer {
private let resolve:(_ container: DIContainer) -> Any
public init<D1, D2, T>(_ function: @escaping (D1, D2) -> T) {
resolve = { container in
let d1: D1 = container.resolve(D1.self)!
View ConstructorInjection_defaultValue.swift
class LoginViewController {
private let userSession: UserSession
init(_ userSession: UserSession = UserSession()) {
self.userSession = userSession
}
}
let vc = LoginViewController()
let vc = LoginViewController(userSession)
View ServiceLocator.swift
protocol Service {}
Class ServiceLocator {
func register(_ service: Service, for type: ServiceType)
func resolve(_ type: ServiceType)
}
ServiceLocator.shared.register(UserSession(), type: UserService.self)
Struct UserService: Service {}
View AmbientContext.swift
Class UserSession {
static let shared: UserSession
}
Class LoginViewController {
func login(_ name: String, password: String) {
...
UserSession.shared.login(name, password)
...
You can’t perform that action at this time.