クリーンアーキテクチャっぽいプログラムをSwiftで書いてみた
DDDもクリーンアーキテクチャもまだ完全に理解すらしていませんが、それっぽいものをSwift(iOSアプリ)で書いてみたので公開します。
ディレクトリ構造
mySampleApp |-Presentation | |-ViewController | | |-SampleViewController.swift | | | |-View | | |-SampleTextView.swift | |-Domain | |-Service | | |-SampleService.swift | | | |-Model | | |-GreetingText.swift | |-Infra |-Repository |-SampleRepository.swift |-SampleRepositoryImpl.swift
Presentation
ViewController
SampleViewController.swiftimport SwiftUI struct SampleViewController: View { var body: some View { SampleTextView() } }
View
SampleTextView.swiftimport SwiftUI struct SampleTextView: View { var body: some View { echoGreeting() } // TODO: emitしたい private func echoGreeting() -> Text { let sampleService = SampleService(sampleRepository: SampleRepositoryImpl()) let greetingText = sampleService.getGreeting() return Text(greetingText.toString()) } }
Domain
Service
SampleService.swiftimport Foundation struct SampleService { private var sampleRepository: SampleRepository init(sampleRepository: SampleRepository) { self.sampleRepository = sampleRepository } func getGreeting() -> GreetingText { return self.sampleRepository.getGreeting() } }
Model
GreetingText.swiftimport Foundation struct GreetingText { private var value: String init(value: String) { self.value = value } func toString() -> String { return self.value } }
Infra
Repository
Interface
SampleRepository.swiftimport Foundation protocol SampleRepository { func getGreeting() -> GreetingText }
Implement
SampleRepositoryImpl.swiftimport Foundation struct SampleRepositoryImpl: SampleRepository { func getGreeting() -> GreetingText { return GreetingText(value: "hello") } }
目次