我正在熟悉 Vapor 2.0 服务器端 Swift 框架,令我困惑的是字符串文字的广泛使用。例如,要实现 Model
协议(protocol),您必须像这样解析和序列化行(取自自动生成的示例项目):
// Initializes the Post from the database row
init(row: Row) throws {
content = try row.get("content")
}
// Serializes the Post to the database
func makeRow() throws -> Row {
var row = Row()
try row.set("content", content)
return row
}
如您所见,对于每个属性,您都将其数据库名称用作此特定协议(protocol)的字符串文字两次。在其他情况下还有更多 — 例如,Database
协议(protocol)、您自己的方法等。
这里使用文字字符串作为参数有明显的缺点,静态分析器不检查它们(就像 Objective-C 中的键值参数),使得这种方法很容易出错。我是否缺少任何最佳实践?
最佳答案
通过将字符串存储为模型上的静态属性并引用它,您可以轻松地减少重复字符串的次数。
final class Post: Model {
// Keys
static let contentKey = "content"
// Properties
var content: String
// Initializes the Post from the database row
init(row: Row) throws {
content = try row.get(Post.contentKey)
}
// Serializes the Post to the database
func makeRow() throws -> Row {
var row = Row()
try row.set(Post.contentKey, content)
return row
}
...
}
extension Post: Preparation {
static func prepare(_ database: Database) throws {
try database.create(self) { builder in
builder.id()
builder.string(Post.contentKey)
}
}
...
}
不幸的是,这可能是您可以做的所有有意义的事情,以使事情更加类型安全。目前没有办法向编译器提供有关数据库实际结构的信息(或任何其他字符串类型的东西,如 JSON)。不过,如果我们能做到这一点,那就太棒了!也许有一天。
(我将研究更新 API 和 Web 模板以包含此内容,在此处跟踪问题:https://github.com/vapor/api-template/issues/35)
关于swift - Vapor 中的数据库列名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44500421/