将数据库连接用作静态是一种好习惯吗?
Is it a good practice to use database connection as static?
我正在为我的 iOS 应用程序使用 SQLite.swift 框架。我有 DatabaseService
class,我在其中创建数据库连接并执行所有 CURD
操作。我正在实例化这个 class 并在每个控制器上创建一个连接,但最近我将数据库变量更改为 static
并为所有控制器创建一次连接。我不确定这是否是一个好习惯。这是我执行此操作的方法:
static var db: Connection?
init() {
if DatabaseService.db != nil {
return
}
let path = NSSearchPathForDirectoriesInDomains(
.documentDirectory, .userDomainMask, true
).first!
do {
let fileManager = FileManager()
try fileManager.copyfileToUserDocumentDirectory(forResource: "db", ofType: "sqlite3")
// Empty database will be created if file does not exist
DatabaseService.db = try? Connection("\(path)/db.sqlite3")
print("Connection successful")
} catch let error {
print("Unable to connect with the database. \(error)")
}
}
我想真正的答案是:如果您知道自己在做什么,并且知道如何安全地进行操作,那么当然可以。
但是既然你在问:我的回答是否定的。
如果在任何地方都使用的话,这里显示的静态变量就是单例模式。 singleton pattern often feels like the right thing to do, but the moment you start adding threads and you want to use a different data provider, things start getting buggy, more complicated, and ugly. You're better off using dependency injection with the factory pattern 使用一个对象 你只实例化了一次。
这样做确实不需要更多的代码。当你想注入它时(编译时间或 运行 时间),选择就变成了,而 Swift 使这一切变得非常简单。如果您想要使用示例进行编辑,请告诉我。预先以这种方式执行的代码有点多,但在大多数情况下 运行 长的代码中,它会为您省去很多心痛。做一些关于单身人士的利弊的研究。大多数人认为在大多数情况下这是个坏主意。
我要添加来自 a book 的一段,我强烈推荐:
Major problems can be caused by global states, for example, the usage of Singletons or static members
in your unit under test. Not only is it that Singletons increase the coupling between software units. They also often hold a global state that circumvents unit test independence. For instance, if a certain global state is the precondition for a successful test, but the previous test has mutated that global state, it can cause serious trouble.
Especially in legacy systems, which are often littered with Singletons, this begs the question: how can I get rid of all those nasty dependencies to those Singletons and make my code better testable? Well, that’s an important question I discuss...
这本书还引用了对标志性书籍 Design Patterns 作者的采访,作者基本上说他们不介意放弃单例模式,因为它从未被正确使用过。
一本关于抽象设计模式的标志性且有影响力的书的作者开玩笑(我认为这是个玩笑)关于从他们的书的新修订版本中删除单例设计模式...
也许真正的答案总是是否定的。
我正在为我的 iOS 应用程序使用 SQLite.swift 框架。我有 DatabaseService
class,我在其中创建数据库连接并执行所有 CURD
操作。我正在实例化这个 class 并在每个控制器上创建一个连接,但最近我将数据库变量更改为 static
并为所有控制器创建一次连接。我不确定这是否是一个好习惯。这是我执行此操作的方法:
static var db: Connection?
init() {
if DatabaseService.db != nil {
return
}
let path = NSSearchPathForDirectoriesInDomains(
.documentDirectory, .userDomainMask, true
).first!
do {
let fileManager = FileManager()
try fileManager.copyfileToUserDocumentDirectory(forResource: "db", ofType: "sqlite3")
// Empty database will be created if file does not exist
DatabaseService.db = try? Connection("\(path)/db.sqlite3")
print("Connection successful")
} catch let error {
print("Unable to connect with the database. \(error)")
}
}
我想真正的答案是:如果您知道自己在做什么,并且知道如何安全地进行操作,那么当然可以。
但是既然你在问:我的回答是否定的。
如果在任何地方都使用的话,这里显示的静态变量就是单例模式。 singleton pattern often feels like the right thing to do, but the moment you start adding threads and you want to use a different data provider, things start getting buggy, more complicated, and ugly. You're better off using dependency injection with the factory pattern 使用一个对象 你只实例化了一次。
这样做确实不需要更多的代码。当你想注入它时(编译时间或 运行 时间),选择就变成了,而 Swift 使这一切变得非常简单。如果您想要使用示例进行编辑,请告诉我。预先以这种方式执行的代码有点多,但在大多数情况下 运行 长的代码中,它会为您省去很多心痛。做一些关于单身人士的利弊的研究。大多数人认为在大多数情况下这是个坏主意。
我要添加来自 a book 的一段,我强烈推荐:
Major problems can be caused by global states, for example, the usage of Singletons or static members in your unit under test. Not only is it that Singletons increase the coupling between software units. They also often hold a global state that circumvents unit test independence. For instance, if a certain global state is the precondition for a successful test, but the previous test has mutated that global state, it can cause serious trouble. Especially in legacy systems, which are often littered with Singletons, this begs the question: how can I get rid of all those nasty dependencies to those Singletons and make my code better testable? Well, that’s an important question I discuss...
这本书还引用了对标志性书籍 Design Patterns 作者的采访,作者基本上说他们不介意放弃单例模式,因为它从未被正确使用过。
一本关于抽象设计模式的标志性且有影响力的书的作者开玩笑(我认为这是个玩笑)关于从他们的书的新修订版本中删除单例设计模式...
也许真正的答案总是是否定的。