JojoCore 框架(iOS)
我知道你是一个非常聪明的开发者,这一点毫无疑问。在我们的经验中,在许多情况下,我们不得不重新编写特定项目中的网络层、数据层和其他所有层。当我们需要将这些基于抽象层的代码复制到新项目中的时候。最糟糕的部分是,即使你试图在以前的项目中修正更改并将更改复制到所有使用上述代码的地方。你知道,这是疯狂且费时的。我们应该有更好的方式来处理事情,而不是让我们的生活像恶魔一样疯狂。我们不得不找到最佳的替代方案来解决这样的问题和冲突,例如使用一些现有的框架,如 Alamofire Moya,以及所有的数据库和日志层。我已经走过这条路,回顾我的代码和架构。这很棒,但每次看到它,我都会注意到我们可以让它更完美,这将使我们编写代码的方式更加专业,并且我们的初级开发者可以模仿编写代码中的决策。
<此处插入对比图 anterior & actual >
因此,JojoCore 抽象化了基于项目的开发(网络层、环境、日志、数据层、性能和扩展)。它应该简单、直观且足够全面,以便使常见事物变得容易。
JojoCore 的一些出色功能
- 为不同的环境栈(QA、开发、生产等)设置环境。
- 内置网络层。
- 您可以将您想要的任何 DB 驱动器注入到项目中。
- 有一个缓存辅助类。
- 附带扩展类属性和方法
- 让您定义一个具有关联枚举值的清晰端点使用。
- 日志应该在调试模式下完成,而不是在生产模式下。
Plist 文件配置 <ConfigurationName.plist>
注意:您可以将 配置 及其 Wiki:配置文件模板 进行复制并修改。
- 环境
This is where you setup the environmental domain for different stacks
e.g
<key>Environments</key>
<dict>
<key>Development</key>
<string>http://www.development.io/v2/</string>
<key>QA</key>
<string>http://www.qa.io/v2/</string>
</dict>
From above you can reference the environment in (3. Servers).
- 请求头
<key>Headers</key>
<dict>
<key>HeaderServer1</key>
<dict>
<dict/>
<dict/>
From above you can reference the request header in (3. Servers).
- 服务器
<key>Servers</key>
<dict>
<key>Server1</key>
<dict>
<key>header</key>
<string>HeaderServer1</string>
<key>environment</key>
<string>QA</string>
</dict>
</dict>
From above you can reference the servers in (4. Modules).
- 模块
<key>Modules</key>
<dict>
<key>Profile</key>
<dict>
<key>environmentServer</key>
<string>Server1</string>
<key>endpoints</key>
<dict>
<key>profileAuthentication</key>
<dict>
<key>mock</key>
<string>/login</string>
<key>http</key>
<string>POST</string>
<key>live</key>
<string>/login</string>
</dict>
<key>profileRegistration</key>
<dict>
<key>mock</key>
<string>/signup</string>
<key>http</key>
<string>POST</string>
<key>live</key>
<string>/signup</string>
</dict>
</dict>
</dict>
</dict>
At this point is where you setup your modules e.g Registration & Authentication (Profile Module), etc..
安装
注意:JojoCore 需要 Swift 4.1(以及 Xcode 9.3)。
CocoaPods
CocoaPods 是 Cocoa 项目的依赖管理器。要使用 CocoaPods 安装 JojoCore
-
请确保已经 安装了 CocoaPods。
# Using the default Ruby install will require you to use sudo when # installing and updating gems. [sudo] gem install cocoapods
-
更新您的 Podfile,包含以下内容
use_frameworks! target 'YourAppTargetName' do pod 'JojoCore', '~> 0.0.9' end
-
运行
pod install --repo-update
。
用法
- AppDelegate 设置
import JojoCore
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
Module.plistName = "ConfigurationName"
return true
}
- 模块服务实现
- 创建模块类型:模块类型应与模块键名中的一致(第 4 部分:模块)PLIST 文件配置 <ConfigurationName.plist>
enum ModuleType : String{
case Profile
}
- 创建端点类型:端点的案例应与端点键名中的一致(第 4 部分:模块)PLIST 文件配置 <ConfigurationName.plist>
enum EndpointType : String{
case profileAuthentication
case profileRegistration
}
- 创建服务
class ModuleService {
let remote = Remote.init()
func profileAuthentication(_ request: JSON) {
let response = { (response: Response) in
switch response {
case .error(let message):
Logger.log(.i, messages: message)
case .success(let json):
Logger.log(.i, messages: "\(json)")
}
}
let module: IModule = ModuleExcutor.execute(ModuleType.Profile.rawValue, endPoint: EndpointType.profileAuthentication.rawValue)!
let builder = RemoteBuilder.init(module: module,request: request, response: response)
remote.execute(builder)
}
func profileRegistration(_ request: JSON) {
let response = { (response: Response) in
switch response {
case .error(let message):
Logger.log(.i, messages: message)
case .success(let json):
Logger.log(.i, messages: "\(json)")
}
}
let module: IModule = ModuleExcutor.execute(ModuleType.Profile.rawValue, endPoint: EndpointType.profileRegistration.rawValue)!
let builder = RemoteBuilder.init(module: module,request: request, response: response)
remote.execute(builder)
}
}
- 本地数据库设置……更新前(14:04:2019)