Protocol
Dependency
The base protocol for all dependency definitions in the napkin architecture.
protocol Dependency : AnyObject, Sendable
Mentioned In
Overview
A Dependency protocol defines the set of dependencies that a napkin requires from its parent. The parent’s Component must conform to this protocol to provide the required dependencies.
Overview
Dependencies flow down the napkin tree:
A child napkin defines a dependency protocol listing what it needs
The parent’s component conforms to that protocol
The parent’s component is passed to the child’s builder
The child’s builder uses the dependency to construct its napkin
Defining Dependencies
Create a protocol that extends Dependency and declares the required services:
protocol MyFeatureDependency: Dependency {
var userService: UserServiceProtocol { get }
var analyticsService: AnalyticsServiceProtocol { get }
var logger: LoggerProtocol { get }
}
Providing Dependencies
The parent component conforms to the child’s dependency protocol:
final class ParentComponent: Component<ParentDependency>, MyFeatureDependency {
var userService: UserServiceProtocol {
return dependency.userService // Pass through from grandparent
}
var analyticsService: AnalyticsServiceProtocol {
return shared { AnalyticsService() } // Create locally
}
var logger: LoggerProtocol {
return shared { Logger() }
}
}
See Also
Component
See Also
EmptyDependency
See Also
Builder
Relationships
Conforming Types
Inherited By