Slide 18
Slide 18 text
02 The Composable ArchitectureでのDIの例
©NewsPicks Inc. All Rights Reserved.
// MARK: - Domainモジュール
public enum SelfLogicAdapterKey: TestDependencyKey {
public static let testValue: SelfLogicAdapter = MockSelfLogic()
public static let previewValue: SelfLogicAdapter = MockSelfLogic()
}
public extension DependencyValues {
var selfLogic: SelfLogicAdapter {
get { self[SelfLogicAdapterKey.self] }
set { self[SelfLogicAdapterKey.self] = newValue }
}
}
// MARK: - Legacyモジュール
extension SelfLogicAdapterKey: DependencyKey {
public static let liveValue: SelfLogicAdapter = SelfLogic.shared
}
// MARK: - Presentationモジュール
struct NewsFeedReducer: ReducerProtocol {
@Dependency(\.selfLogic) var selfLogic
func reduce(into state: inout State, action: Action) -> EffectTask {
...
selfLogic.someLogic()
}
}
● DependencyValuesを使ってReducerに
SelfLogicをインジェクトしている