クリーンアーキテクチャっぽいプログラムをSwiftで書いてみた

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.swift
import SwiftUI struct SampleViewController: View { var body: some View { SampleTextView() } }

View

SampleTextView.swift
import 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.swift
import Foundation struct SampleService { private var sampleRepository: SampleRepository init(sampleRepository: SampleRepository) { self.sampleRepository = sampleRepository } func getGreeting() -> GreetingText { return self.sampleRepository.getGreeting() } }

Model

GreetingText.swift
import Foundation struct GreetingText { private var value: String init(value: String) { self.value = value } func toString() -> String { return self.value } }

Infra

Repository

Interface

SampleRepository.swift
import Foundation protocol SampleRepository { func getGreeting() -> GreetingText }

Implement

SampleRepositoryImpl.swift
import Foundation struct SampleRepositoryImpl: SampleRepository { func getGreeting() -> GreetingText { return GreetingText(value: "hello") } }

ニッシー☆

@yukinissie

目次