Getting Started with Service¶
Service (vapor/service) is a dependency injection (inversion of control) framework. It allows you to register, configure, and create your application's dependencies in a maintainable way.
/// register a service during boot services.register(PrintLogger.self, as: Logger.self) /// you can then create that service later let logger = try someContainer.make(Logger.self) print(logger is PrintLogger) // true
This package is included with Vapor and exported by default. You will have access to all
Service APIs when you import
The Service package is lightweight, pure-Swift, and has very few dependencies. This means it can be used as a dependency injection framework for any Swift project—even one not using Vapor.
To include it in your package, add the following to your
// swift-tools-version:4.0 import PackageDescription let package = Package( name: "Project", dependencies: [ ... .package(url: "https://github.com/vapor/service.git", from: "1.0.0"), ], targets: [ .target(name: "Project", dependencies: ["Service", ... ]) ] )
import Service to access the APIs.
Some of this guide may contain Vapor-specific APIs, however most of it should be applicable to the Services package in general. Visit the API Docs for Service-specific API info.