Created
April 15, 2021 01:53
-
-
Save bricker/b3798fb24017ff4ed22f2c25dabeb4c9 to your computer and use it in GitHub Desktop.
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
// Define Provider Protocols | |
protocol NetworkProvider { | |
var networkingService: NetworkServiceProtocol | |
} | |
protocol LoggingProvider { | |
var loggerService: LoggerServiceProtocol | |
} | |
protocol ConfigurationProvider { | |
var configService: ConfigServiceProtocol | |
} | |
// Create a Provider container for use in the app | |
class DefaultProvider: NetworkProvider, LoggingProvider, ConfigurationProvider { | |
// singleton setup | |
static var shared = ... | |
let networkingService: NetworkServiceProtocol = { | |
return NetworkService() | |
}() | |
let loggerService: LoggerServiceProtocol = { | |
return LoggerService() | |
}() | |
let configService: ConfigServiceProtocol = { | |
return ConfigService() | |
}() | |
} | |
// Define a module's dependencies with protocol composition | |
class ExploreViewModel { | |
typealias Dependencies = NetworkingProvider & LoggingProvider & ConfigurationProvider | |
private let ctx: Dependencies | |
init(ctx: Dependencies) { | |
self.ctx = ctx | |
} | |
} | |
let viewModel = ExploreViewModel(ctx: DefaultProvider.shared) | |
// Use a Mock provider for tests | |
class ViewModelMockProvider: NetworkProvider, LoggingProvider, ConfigurationProvider { | |
let networkingService: NetworkServiceProtocol = { | |
return MockNetworkService() | |
} | |
// ... etc | |
} | |
// ExploreViewModelTests.swift | |
let viewModel = ExploreViewModel(ctx: MockProvider()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment