RouterService 开源项目教程

RouterService 开源项目教程

RouterService💉Type-safe Navigation/Dependency Injection Framework for Swift项目地址:https://gitcode.com/gh_mirrors/ro/RouterService

项目介绍

RouterService 是一个专注于提高模块化 Swift 应用构建速度的类型安全导航和依赖注入框架。该框架基于 AirBnB 在 BA:Swiftable 2019 上展示的系统,旨在作为模块化应用的依赖注入器。每个模块包含一个额外的 "interface" 模块,遵循一个特性模块不应直接依赖于具体模块的原则,以提高构建性能。特性模块仅能访问其他特性的接口,接口包含协议和其他不太可能改变的内容。RouterService 负责在引用这些协议时注入必要的具体依赖。

项目快速启动

安装

Swift Package Manager
package(url: "https://github.com/rockbruno/RouterService", upToNextMinor(from: "1.1.0"))
CocoaPods
pod 'RouterService'

初始化

import HTTPClient
import Profile
import Login

class AppDelegate: UIResponder, UIApplicationDelegate {
    let routerService = RouterService()

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // 注册依赖
        routerService.register(dependencyFactory: { return HTTPClient() }, forType: HTTPClientProtocol.self)

        // 注册路由处理程序
        routerService.register(routeHandler: ProfileRouteHandler())
        routerService.register(routeHandler: LoginRouteHandler())

        // 设置窗口
        let window = UIWindow()
        window.makeKeyAndVisible()

        // 启动 RouterService
        window.rootViewController = routerService.navigationController(withInitialFeature: ProfileFeature.self)
        return true
    }
}

应用案例和最佳实践

创建用户配置文件特性

import HTTPClient
import Profile

struct UserProfileFeature: Feature {
    @Dependency var client: HTTPClientProtocol
    @Dependency var routerService: RouterServiceProtocol

    func build(fromRoute route: Route) -> UIViewController {
        return UserProfileViewController(client: client, routerService: routerService)
    }
}

class UserProfileViewController: UIViewController {
    let client: HTTPClientProtocol
    let routerService: RouterServiceProtocol

    init(client: HTTPClientProtocol, routerService: RouterServiceProtocol) {
        self.client = client
        self.routerService = routerService
        super.init(nibName: nil, bundle: nil)
    }

    func goToLogin() {
        let loginRoute = SomeLoginRouteFromTheLoginFeatureInterface()
        routerService.navigate(toRoute: loginRoute, fromView: self, presentationStyle: Push(), animated: true)
    }
}

典型生态项目

RouterService 适用于需要模块化和快速构建的 Swift 应用。它通过特性(Feature)的概念,使得每个特性可以独立创建视图控制器,并通过 RouterService 管理依赖和导航。这种架构使得应用具有水平依赖图(快速构建时间)和动态导航(任何屏幕可以从任何地方推送)。

相关资源

RouterService💉Type-safe Navigation/Dependency Injection Framework for Swift项目地址:https://gitcode.com/gh_mirrors/ro/RouterService

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

童福沛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值