update controller to thread-safe method

This commit is contained in:
Jimmy McDermott 2018-08-22 07:56:18 -05:00
parent 471e9c736a
commit 57e8cf28f6
1 changed files with 45 additions and 4 deletions

View File

@ -125,17 +125,17 @@ The routes.swift file is used to declare route registration for your application
```swift ```swift
import Vapor import Vapor
public func routes(_ router: Router) throws { public func routes(_ router: Router, _ container: Container) throws {
try router.register(collection: MyControllerHere()) try router.register(collection: MyControllerHere(db: container.connectionPool(to: .mysql)))
} }
``` ```
You should call this function from `configure.swift` like this: You should call this function from `configure.swift` like this:
```swift ```swift
services.register(Router.self) { _ -> EngineRouter in services.register(Router.self) { container -> EngineRouter in
let router = EngineRouter.default() let router = EngineRouter.default()
try routes(router) try routes(router, container)
return router return router
} }
``` ```
@ -613,6 +613,47 @@ func update(req: Request, content: User) throws -> Future<User> {
} }
``` ```
Controllers should follow the thread-safe architecture when possible. This means passing necessary `Service`s to the controller on initialization instead of making them in the routes.
**Bad:**
```swift
final class LoginViewController: RouteCollection {
func boot(router: Router) throws {
router.get("/login", use: login)
}
func login(req: Request) throws -> String {
let userRepository = try req.make(UserRepository.self)
//do something with it
return ""
}
}
```
**Good:**
```swift
final class LoginViewController: RouteCollection {
private let userRepository: UserRepository
init(userRepository: UserRepository) {
self.userRepository = userRepository
}
func boot(router: Router) throws {
router.get("/login", use: login)
}
func login(req: Request) throws -> String {
//use `self.userRepository`
return ""
}
}
```
Controllers should only cover one idea/feature at a time. If a feature grows to encapsulate a large amount of functionality, routes should be split up into multiple controllers and organized under one common feature folder in the `Controllers` folder. For example, an app that handles generating a lot of analytical/reporting views should break up the logic by specific report to avoid cluttering a generic `ReportsViewController.swift` Controllers should only cover one idea/feature at a time. If a feature grows to encapsulate a large amount of functionality, routes should be split up into multiple controllers and organized under one common feature folder in the `Controllers` folder. For example, an app that handles generating a lot of analytical/reporting views should break up the logic by specific report to avoid cluttering a generic `ReportsViewController.swift`
## Async ## Async