深入理解依赖注入:从单容器到容器层次结构
1. 引言
在开发过程中,管理对象依赖是实现可测试性和可维护性等目标的关键。依赖注入(DI)作为一种重要的设计模式,能够从对象外部提供依赖,帮助我们构建良好架构的软件。本文将详细介绍如何应用 DI 模式,包括单容器和容器层次结构的实现。
2. 单容器方法的应用
为了解决 KooberObjectFactories 中长生命周期依赖需要创建全局常量的问题,我们将其升级为 KooberAppDependencyContainer 。以下是构建该容器的详细步骤:
2.1 创建并存储共享的 UserSessionRepository
class KooberAppDependencyContainer {
// MARK: - Properties
// 1
let sharedUserSessionRepository: UserSessionRepository
// MARK: - Methods
init() {
// 2
func makeUserSessionRepository() -> UserSessionRepository {
let dataStore = makeUserSessionDataStore()
let remoteAPI = makeAuthRemoteAPI()
return KooberUserSession
超级会员免费看
订阅专栏 解锁全文
1675

被折叠的 条评论
为什么被折叠?



