diff --git a/Cherrish-iOS/Cherrish-iOS/App/Cherrish_iOSApp.swift b/Cherrish-iOS/Cherrish-iOS/App/Cherrish_iOSApp.swift index 1fbe2f90..c585493b 100644 --- a/Cherrish-iOS/Cherrish-iOS/App/Cherrish_iOSApp.swift +++ b/Cherrish-iOS/Cherrish-iOS/App/Cherrish_iOSApp.swift @@ -9,9 +9,15 @@ import SwiftUI @main struct Cherrish_iOSApp: App { + + init() { + // TODO: 코디네이터에서 실행하기 + DIContainer.shared.dependencyInjection() + } + var body: some Scene { WindowGroup { - ContentView() + ViewFactory.makeTestView() } } } diff --git a/Cherrish-iOS/Cherrish-iOS/App/DIContainer+.swift b/Cherrish-iOS/Cherrish-iOS/App/DIContainer+.swift new file mode 100644 index 00000000..3c218d73 --- /dev/null +++ b/Cherrish-iOS/Cherrish-iOS/App/DIContainer+.swift @@ -0,0 +1,18 @@ +// +// AppDIContainer+.swift +// Cherrish-iOS +// +// Created by 이나연 on 1/5/26. +// + +import Foundation + +extension DIContainer { + func dependencyInjection() { + let dataDependencyAssembler = DataDependencyAssembler() + let domainDependencyAssembler = DomainDependencyAssembler(preAssembler: dataDependencyAssembler) + let presentationDependencyAssembler = PresentationDependencyAssembler(preAssembler: domainDependencyAssembler) + + presentationDependencyAssembler.assemble() + } +} diff --git a/Cherrish-iOS/Cherrish-iOS/Core/DIContainer.swift b/Cherrish-iOS/Cherrish-iOS/Core/DIContainer.swift index b254fa01..6a280de1 100644 --- a/Cherrish-iOS/Cherrish-iOS/Core/DIContainer.swift +++ b/Cherrish-iOS/Cherrish-iOS/Core/DIContainer.swift @@ -1,7 +1,35 @@ // -// DIContainer.swift +// AppDIContainer.swift // Cherrish-iOS // // Created by 이나연 on 12/31/25. // +import Foundation + +protocol DependencyAssembler { + func assemble() +} + +final class DIContainer { + + static let shared = DIContainer() + + private init() { } + + private var dependencies: [String: () -> Any] = [:] + + func register(type: T.Type, closure: @escaping () -> T) { + let key = String(describing: T.self) + dependencies[key] = closure + } + + func resolve(type: T.Type) -> T? { + let key = String(describing: T.self) + guard let closure = dependencies[key] else { + return nil + } + + return closure() as? T + } +} diff --git a/Cherrish-iOS/Cherrish-iOS/Data/DataDependencyAssembler.swift b/Cherrish-iOS/Cherrish-iOS/Data/DataDependencyAssembler.swift new file mode 100644 index 00000000..bfbb6dcd --- /dev/null +++ b/Cherrish-iOS/Cherrish-iOS/Data/DataDependencyAssembler.swift @@ -0,0 +1,16 @@ +// +// DataDependencyAssembler.swift +// Cherrish-iOS +// +// Created by 이나연 on 1/5/26. +// + +import Foundation + +final class DataDependencyAssembler: DependencyAssembler { + func assemble() { + DIContainer.shared.register(type: TestInterface.self) { + return DefaultTestRepository() + } + } +} diff --git a/Cherrish-iOS/Cherrish-iOS/Data/Repository/Repository.swift b/Cherrish-iOS/Cherrish-iOS/Data/Repository/Repository.swift deleted file mode 100644 index 0c1a6353..00000000 --- a/Cherrish-iOS/Cherrish-iOS/Data/Repository/Repository.swift +++ /dev/null @@ -1,8 +0,0 @@ -// -// Repository.swift -// Cherrish-iOS -// -// Created by 이나연 on 1/3/26. -// - -import Foundation diff --git a/Cherrish-iOS/Cherrish-iOS/Data/Repository/TestRepository.swift b/Cherrish-iOS/Cherrish-iOS/Data/Repository/TestRepository.swift new file mode 100644 index 00000000..2f5a0414 --- /dev/null +++ b/Cherrish-iOS/Cherrish-iOS/Data/Repository/TestRepository.swift @@ -0,0 +1,14 @@ +// +// Repository.swift +// Cherrish-iOS +// +// Created by 이나연 on 1/3/26. +// + +import Foundation + +struct DefaultTestRepository: TestInterface { + func test() { + print("Repository Test!") + } +} diff --git a/Cherrish-iOS/Cherrish-iOS/Domain/DomainDependencyAssembler.swift b/Cherrish-iOS/Cherrish-iOS/Domain/DomainDependencyAssembler.swift new file mode 100644 index 00000000..fae9b612 --- /dev/null +++ b/Cherrish-iOS/Cherrish-iOS/Domain/DomainDependencyAssembler.swift @@ -0,0 +1,28 @@ +// +// DomainDependencyAssembler.swift +// Cherrish-iOS +// +// Created by 이나연 on 1/5/26. +// + +import Foundation + +final class DomainDependencyAssembler: DependencyAssembler { + private let preAssembler: DependencyAssembler + + init(preAssembler: DependencyAssembler) { + self.preAssembler = preAssembler + } + + func assemble() { + preAssembler.assemble() + + guard let testRepository = DIContainer.shared.resolve(type: TestInterface.self) else { + return + } + + DIContainer.shared.register(type: TestUseCase.self) { + return DefaultTestUseCase(repository: testRepository) + } + } +} diff --git a/Cherrish-iOS/Cherrish-iOS/Domain/Interface/Interface.swift b/Cherrish-iOS/Cherrish-iOS/Domain/Interface/TestInterface.swift similarity index 69% rename from Cherrish-iOS/Cherrish-iOS/Domain/Interface/Interface.swift rename to Cherrish-iOS/Cherrish-iOS/Domain/Interface/TestInterface.swift index 19bf475a..1393ed47 100644 --- a/Cherrish-iOS/Cherrish-iOS/Domain/Interface/Interface.swift +++ b/Cherrish-iOS/Cherrish-iOS/Domain/Interface/TestInterface.swift @@ -6,3 +6,7 @@ // import Foundation + +protocol TestInterface { + func test() +} diff --git a/Cherrish-iOS/Cherrish-iOS/Domain/UseCase/TestUseCase.swift b/Cherrish-iOS/Cherrish-iOS/Domain/UseCase/TestUseCase.swift new file mode 100644 index 00000000..47ee15a4 --- /dev/null +++ b/Cherrish-iOS/Cherrish-iOS/Domain/UseCase/TestUseCase.swift @@ -0,0 +1,25 @@ +// +// Usecase.swift +// Cherrish-iOS +// +// Created by 이나연 on 1/3/26. +// + +import Foundation + +protocol TestUseCase { + func execute() +} + +struct DefaultTestUseCase: TestUseCase { + private let repository: TestInterface + + init(repository: TestInterface) { + self.repository = repository + } + + func execute() { + self.repository.test() + print("UseCase execute") + } +} diff --git a/Cherrish-iOS/Cherrish-iOS/Domain/UseCase/Usecase.swift b/Cherrish-iOS/Cherrish-iOS/Domain/UseCase/Usecase.swift deleted file mode 100644 index 5a40044d..00000000 --- a/Cherrish-iOS/Cherrish-iOS/Domain/UseCase/Usecase.swift +++ /dev/null @@ -1,8 +0,0 @@ -// -// Usecase.swift -// Cherrish-iOS -// -// Created by 이나연 on 1/3/26. -// - -import Foundation diff --git a/Cherrish-iOS/Cherrish-iOS/Presentation/Feature/ContentView.swift b/Cherrish-iOS/Cherrish-iOS/Presentation/Feature/ContentView.swift deleted file mode 100644 index 02607b0b..00000000 --- a/Cherrish-iOS/Cherrish-iOS/Presentation/Feature/ContentView.swift +++ /dev/null @@ -1,17 +0,0 @@ -// -// ContentView.swift -// Cherrish-iOS -// -// Created by 이나연 on 12/31/25. -// - -import SwiftUI - -struct ContentView: View { - var body: some View { - VStack { - Text("Hello, world!") - } - .padding() - } -} diff --git a/Cherrish-iOS/Cherrish-iOS/Presentation/Feature/TestView.swift b/Cherrish-iOS/Cherrish-iOS/Presentation/Feature/TestView.swift new file mode 100644 index 00000000..d4db72d5 --- /dev/null +++ b/Cherrish-iOS/Cherrish-iOS/Presentation/Feature/TestView.swift @@ -0,0 +1,23 @@ +// +// ContentView.swift +// Cherrish-iOS +// +// Created by 이나연 on 12/31/25. +// + +import SwiftUI + +struct TestView: View { + @ObservedObject var viewModel: TestViewModel + + var body: some View { + VStack { + Button(action: { + viewModel.test() + }) { + Text("\(viewModel.text)") + } + .padding() + } + } +} diff --git a/Cherrish-iOS/Cherrish-iOS/Presentation/Feature/TestViewModel.swift b/Cherrish-iOS/Cherrish-iOS/Presentation/Feature/TestViewModel.swift new file mode 100644 index 00000000..c3d4bd43 --- /dev/null +++ b/Cherrish-iOS/Cherrish-iOS/Presentation/Feature/TestViewModel.swift @@ -0,0 +1,23 @@ +// +// TestViewModel.swift +// Cherrish-iOS +// +// Created by 이나연 on 1/5/26. +// + +import Foundation + +final class TestViewModel: ObservableObject { + @Published var text: String = "터치해보세여" + private let testUseCase: TestUseCase + + init(testUseCase: TestUseCase) { + self.testUseCase = testUseCase + } + + func test() { + testUseCase.execute() + text = "버튼 터치했음!" + print("view model execute") + } +} diff --git a/Cherrish-iOS/Cherrish-iOS/Presentation/PresentationDependencyAssembler.swift b/Cherrish-iOS/Cherrish-iOS/Presentation/PresentationDependencyAssembler.swift new file mode 100644 index 00000000..6c535173 --- /dev/null +++ b/Cherrish-iOS/Cherrish-iOS/Presentation/PresentationDependencyAssembler.swift @@ -0,0 +1,31 @@ +// +// PresentationDependencyAssembler.swift +// Cherrish-iOS +// +// Created by 이나연 on 1/5/26. +// + +import Foundation + +final class PresentationDependencyAssembler: DependencyAssembler { + private let preAssembler: DependencyAssembler + + init(preAssembler: DependencyAssembler) { + self.preAssembler = preAssembler + } + + func assemble() { + preAssembler.assemble() + + guard let testUseCase = DIContainer.shared.resolve(type: TestUseCase.self) else { + return + } + + DIContainer.shared.register(type: TestViewModel.self) { + print("뷰모델 등록") + return TestViewModel(testUseCase: testUseCase) + } + } + + +} diff --git a/Cherrish-iOS/Cherrish-iOS/Presentation/ViewFactory.swift b/Cherrish-iOS/Cherrish-iOS/Presentation/ViewFactory.swift new file mode 100644 index 00000000..e94af7f7 --- /dev/null +++ b/Cherrish-iOS/Cherrish-iOS/Presentation/ViewFactory.swift @@ -0,0 +1,22 @@ +// +// ViewFactory.swift +// Cherrish-iOS +// +// Created by 이나연 on 1/5/26. +// + +import Foundation + +protocol ViewFactoryProtocol { + static func makeTestView() -> TestView +} + +final class ViewFactory: ViewFactoryProtocol { + static func makeTestView() -> TestView { + guard let viewModel = DIContainer.shared.resolve(type: TestViewModel.self) else { + // TODO: DI 실패 시 기본으로 갈 곳 지정 + fatalError() + } + return TestView(viewModel: viewModel) + } +}