| name | dependency-injection |
| description | Creates Features for dependency injection. Use when creating features, exposing public entry points, or wiring up dependencies. |
Skill: Dependency Injection
Guide for creating dependency injection with Composition Root pattern.
References
Architecture Overview
ChallengeApp
โ
โโโ AppContainer (Composition Root)
โ
โโโ httpClient: HTTPClientContract
โโโ tracker: TrackerContract
โโโ imageLoader: ImageLoaderContract โ SwiftUI Environment
โ
โโโ features: [Feature]
โโโ CharacterFeature (navigation + deep links)
โ โโโ CharacterContainer (DI composition)
โ โโโ repository
โ โโโ makeCharacterListView()
โ โโโ makeCharacterDetailView()
โ
โโโ HomeFeature (navigation)
โโโ HomeContainer (DI composition)
โโโ makeHomeView()
Key Concepts
- AppContainer: Composition Root โ creates shared dependencies (HTTPClient, Tracker) and all features
- {Feature}Container: Handles dependency composition (repositories, factories)
- {Feature}Feature: Handles navigation and deep links, delegates DI to Container
- Container factory methods create both ViewModel and View โ callers receive a ready-to-use
some View
- Navigation is handled by App using
NavigationCoordinator
File Structure
App/
โโโ Sources/
โ โโโ {AppName}App.swift
AppKit/
โโโ Sources/
โ โโโ AppContainer.swift
โ โโโ Presentation/
โ โ โโโ Navigation/
โ โ โ โโโ AppNavigationRedirect.swift
โ โ โโโ Views/
โ โ โโโ NavigationContainerView.swift
โ โ โโโ RootContainerView.swift
โ โ โโโ ModalContainerView.swift
Features/{Feature}/
โโโ Sources/
โ โโโ {Feature}Feature.swift
โ โโโ {Feature}Container.swift
โ โโโ Navigation/
โ โ โโโ {Feature}IncomingNavigation.swift
โ โ โโโ {Feature}OutgoingNavigation.swift
โ โ โโโ {Feature}DeepLinkHandler.swift
โ โโโ Presentation/
โ โโโ {Screen}/
โ โโโ Navigator/
โ โโโ Tracker/
โ โโโ Views/
โ โโโ ViewModels/
โโโ Tests/
โโโ Feature/
โโโ {Feature}FeatureTests.swift
Dependency Patterns
| Type | Pattern | Reason |
|---|
| HTTPClient | Required init parameter | Injected by AppContainer |
| Tracker | Required init parameter | Injected by AppContainer |
| ImageLoader | SwiftUI Environment | Injected by RootContainerView from AppContainer |
| Container | Created in Feature init | Owns dependency composition |
| DataSource | Local variable in Container init | Only needed to build repositories |
| Repository | Stored property in Container (let) | Built in init, used by factory methods |
| Navigator | Created inline inside Container factory method | New instance per View |
| Screen Tracker | Created inline inside Container factory method | New instance per View |
| ViewModel | Created inline inside Container factory method | New instance per View |
| View | Container factory method (make{Name}View) | Wraps ViewModel; returned as some View |
| UseCase | Created inline | Stateless |
Visibility Summary
| Component | Visibility | Reason |
|---|
| Contract (Protocol) | public | API for consumers, enables DI |
| {Feature}Feature | public | Entry point struct |
| {Feature}Container | internal | Created by Feature within same module |
| Feature.makeMainView() | public | Creates the feature's default entry point |
| Feature.resolve() | public | Returns view for navigation or nil |
| AppContainer.init | public | Only public member, created by ChallengeApp |
| AppContainer methods | internal/private | resolveView, handle, makeRootView are internal; rest private |
Container factory methods (make{Name}View) | internal | Called by Feature; return some View |
| {Feature}IncomingNavigation | public | Used by AppNavigationRedirect |
| {Feature}OutgoingNavigation | public | Used by AppNavigationRedirect |
| {Feature}DeepLinkHandler | internal | Accessed via Feature.deepLinkHandler |
| Navigator / Tracker / Views | internal | Internal to feature |
Checklist