深入解析依赖注入:单容器与容器层级方法应用
1. 单容器方法应用
为了将 KooberObjectFactories 转换为依赖容器,需要将其从无状态转变为有状态。容器用于持有长期存在的依赖项,如 UserSessionRepository 。下面我们将从头开始构建 KooberAppDependencyContainer 。
首先,创建并存储共享的 UserSessionRepository :
class KooberAppDependencyContainer {
// MARK: - Properties
// 1
let sharedUserSessionRepository: UserSessionRepository
// MARK: - Methods
init() {
// 2
func makeUserSessionRepository() -> UserSessionRepository {
let dataStore = makeUserSessionDataStore()
let remoteAPI = makeAuthRemoteAPI()
return KooberUserSessionRepository(dataStore: dataStore,
remoteAPI: remoteAPI
超级会员免费看
订阅专栏 解锁全文
11

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



