This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import FeatureService | |
| extension Feature1: Feature1Service { | |
| public func doSomething() { | |
| print("Feature1 do something") | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class AppContext { | |
| let container: DIContainer | |
| init() { | |
| self.container = DIContainer.init() | |
| ServiceAssembly.assembled(in: container) | |
| } | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class LoginViewController { | |
| private let userSession: UserSession | |
| init(_ userSession: UserSession = UserSession()) { | |
| self.userSession = userSession | |
| } | |
| } | |
| let vc = LoginViewController() | |
| let vc = LoginViewController(userSession) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Class UserSession { | |
| static let shared: UserSession | |
| } | |
| Class LoginViewController { | |
| func login(_ name: String, password: String) { | |
| ... | |
| UserSession.shared.login(name, password) | |
| ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| typealias Login = (_ name: String, _ password: String) -> Void | |
| class LoginViewController { | |
| func login(_ login: Login, name: String, password: String) { | |
| ... | |
| login(name, password) | |
| ... | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class LoginViewController { | |
| var userSession: UserSession? | |
| func login(_ userName: String, password: String) { | |
| userSesson?.login(userName, password) | |
| } | |
| } |