Template: Clean Architecture Feature
The canonical end-to-end feature: all three layers, DI, error handling, and tests. This bundle ships representative .swift files you can copy and rename. See architecture/clean_architecture.md.
Folder Structure
text
clean_architecture_feature/
├── Domain.swift // entity + repository protocol + use case + DomainError
├── Data.swift // DTO + mapper + repository implementation
├── Presentation.swift // ViewState + ViewModel + View
├── CompositionRoot.swift // wires the graph (prod + preview/test)
└── Tests.swift // use case + ViewModel + mapper testsThe files use a sample
Articlefeature. ReplaceArticle/articlewith your entity, and split each file into per-type files when you copy it into a real module.
How the layers connect
text
ArticlesView → ArticlesViewModel → FetchArticlesUseCase → ArticleRepository (protocol)
▲
RemoteArticleRepository (Data) ── APIClientConventions Demonstrated
- Domain is framework-free; repository protocol + use case live here.
- Data maps
ArticleDTO→ArticleandNetworkError→DomainError; DTOs never leak. - Presentation uses a single
ViewStateenum and an@Observable @MainActorViewModel. - DI through a composition root with a
#if DEBUGpreview/test factory. - Tests are deterministic with simple stubs (no real network).
Usage
- Copy the bundle into your feature module.
- Rename
Article/article→ your entity. - Split combined files into one-type-per-file.
- Point
RemoteArticleRepositoryat your endpoint. - Run the tests; extend coverage for your business rules.
See also: feature_module, repository_layer, swiftui_screen, unit_test_template.