测试已测试 | ✓ |
语言语言 | SwiftSwift |
许可证 | MIT |
发布最后发布 | 2017年11月 |
SwiftSwift 版本 | 3.0 |
SPM支持 SPM | ✓ |
由Luciano Polit维护。
RepositoryKit是一个减轻您代码组织方式的框架。
它基于存储库模式,其主要目的是将检索存储模块(如网络或本地存储)数据的逻辑从您的代码中分离出来。它通常位于控制器中,迫使它执行比它应该做的更多的事情(并使其变得更大)。然而,现在,它们有了正确的干净的地方。
它由三个组件组成
此外,它使用承诺,这是一种管理异步代码的好方法。
要运行示例项目有两种方法
pod try RepositoryKit
要使用CocoaPods安装RepositoryKit,只需将以下行添加到您的Podfile
pod "RepositoryKit", "~> 4.1"
要使用Carthage安装RepositoryKit,只需将以下行添加到您的Cartfile
github "LucianoPolit/RepositoryKit" ~> 4.1
情况:需要与REST API交互来管理对话,简单的情况,'Message'作为唯一实体。对于这些消息,我们需要执行四种不同的操作,存储库将负责执行这些操作。这些操作是:创建、读取、更新、删除,通常称为CRUD。
首先,由于存储库需要与我们的API进行交互,它需要能够识别实体,因为它需要在同一实体上执行不同的操作。因此,我们必须遵守 'Identifiable',只需添加一个 'id' 属性。
struct Message: Identifiable {
// Entity identification.
var id: String
// Properties.
var text: String
}
然后,由于存储库需要使用公共数据表示与我们的API进行通信,实体必须遵守两个新的协议:'DictionaryInitializable' 和 'DictionaryRepresentable'。
extension Message: DictionaryInitializable {
// Initialize with a dictionary.
init?(dictionary: [String: Any]) {
// Here we will have the dictionary that should initialize the entity.
// We have to be careful that every information we need is inside the dictionary.
// If not, return nil, and we will have an error in the promise of the repository operation.
guard let id = dictionary["_id"] as? String, let text = dictionary["text"] as? String else { return nil }
// In case that we have the data needed, set it.
self.id = id
self.text = text
}
}
extension Message: DictionaryRepresentable {
// Dictionary representation.
var dictionary: [String: Any] {
return [
"_id": id,
"text": text
]
}
}
就这样!我们的实体准备好了,可以与我们的存储库和API交互了!
现在我们有了准备好的实体,我们需要做一些能够与API交互的事情,并且实体能够提供信息。
首先,我们的存储库需要知道我们的实体。符合 '存储库' 是容易的。
class MessageRepository: Repository {
// It is the entity that the repository operates.
typealias Entity = Message
}
然后,我们需要符合 '网络存储库'。所以,存储库需要
class MessageRepository: NetworkingRepository {
// It is the entity that the repository operates.
typealias Entity = Message
// It will make the requests.
var store: Networking
// The path that represents the repository.
var path: String = "messages"
// Initialize it with a networking store.
init(store: Networking) {
self.store = store
}
}
现在,我们能够实现实体-存储库-API 之间的通信了。那么,我们能执行什么操作呢?有人知道吗…
所以,根据实际需要,我们需要能够执行CRUD操作
如果我们使存储库符合'CRUDNetworkingRepository',它就能够执行这些操作。酷吗?多亏了Swift,这是由于默认协议实现成为可能的!
class MessageRepository: CRUDNetworkingRepository { ... }
如果CRUD的概念不足以满足你的需求,或者KIT包含的其他存储库,你可以扩展存储库并定义自己的方法,或者创建你自己的存储库类型。
现在,我们准备进行尝试了!
首先,我们使用Promise。如果你对这些没有了解,我邀请你先了解一点PromiseKit。
在开始执行CRUD操作之前,我们需要初始化网络会话和存储库。
let networkingSession = NetworkingSession(url: "https://:3000")
let messageRepository = MessageRepository(store: networkingSession)
现在我们准备好了!让我们创建一条消息。
// It will create a message on your API.
messageRepository.create(["text": "Here goes a message!!!"])
.then { message in
print(message)
// Observe that you should have at least the 'text' and 'id' properties initialized.
// In my case, it printed 'Message(id: "581a8e2da80614c82661b98d", text: "Here goes a message!!!")'.
// Here you can update the UI for example.
}
.catch { error in
// In case that an error occurs, do whatever you have to do here.
errorHandler(error)
}
让我给你演示一个额外的例子。然后你可以尝试任何你想要的东西!
messageRepository.search("581a8e2da80614c82661b98d")
.then(execute: messageRepository.delete)
.then {
print("The message has been deleted")
}
.catch { error in
// In case that an error occurs, do whatever you have to do here.
errorHandler(error)
}
在这种情况下,它会尝试根据指定的id在API上搜索实体。然后,如果成功,它会尝试删除它,如果一切正常,它会打印实体已被删除。请记住,不要忘记处理可能的错误。
简单吗?更整洁吗?很酷!现在轮到你了,来试试吧!
使用案例只是一个起点,是最简单的。有更多的存储库类型,因此实体可能需要更多的要求。要查看更复杂的示例,下载示例文件,并看看它如何与多个存储库(在这种情况下,网络存储和本地存储结合,列出用户)一起工作。
以下是目前可用的存储库列表(对于每个存储库,检查需要进行实现的协议,对于实体和存储库两种情况)
Luciano Polit,[email protected]
RepositoryKit遵循MIT许可。有关更多信息,请参阅LICENSE文件。