When an app becomes more and more complex, there is a lot of dependencies on business modules, like this:
With the DispatchCenter architecture, we hope to achieve clear module responsibilities and boundaries, improve code quality, reduce complex dependencies, improve development efficiency, and make it easy to identify problems. like this:
- Service Register With Generic Type or URL
- Support Resolve Service With Complex Parameter
- Support URL query parameter
- Support Reference and Value Type
- Support push/present/show/showdetail navigation
- Support Navigation With URL or ViewController
- iOS 9.0+ / tvOS 9.0+
- Xcode 12 +
- Swift 5.0 +
pod 'DispatchCenter', '~> 1.2.0'
// register your service provider
class CourseViewController: UIViewController {
var id: Int?
}
extension CourseViewController: ServiceProviderProtocol {
static func create(_ arguments: [String: Any]? = nil) -> Self {
let courseVC = CourseViewController.init(nibName: "CourseViewController", bundle: nil) as! Self
return courseVC
}
}
// register process with Type
let container = ServiceManager()
// without parameter
container.register(CourseViewController.self) { (_) -> CourseViewController in
let course = CourseViewController.create()
return course
}
// with parameter
container.register(CourseViewController.self) { (_, id: Int) -> CourseViewController in
let course = CourseViewController.create(["id": id])
return course
}
let container = ServiceManager()
// without parameter
let course = container.resolve(CourseViewController.self)!
self.showViewController(course, from: self)
// without parameter
let course = container.resolve(CourseViewController.self)!
self.showViewController(course, from: self)
// register process with URL
class LessonViewController: UIViewController {
var id: Int?
}
extension LessonViewController: ServiceProviderProtocol {
static func create(_ arguments: [String : Any]? = nil) -> Self {
let lesson = LessonViewController(nibName: "LessonViewController", bundle: nil) as! Self
guard let id = arguments?["id"] as? Int else {
return lesson
}
lesson.id = id
return lesson
}
}
// url with query parameters
let url= "dispatch://course/lesson"
container.register(url: url) { (_, parameter: [String: String]?) -> LessonViewController in
var arguments: [String: Any] = [:]
if let idstr = parameter?["id"], let id = Int(idstr) {
arguments["id"] = id
}
let course = LessonViewController.create(arguments)
return
}
let resolveURL = "dispatch://course/lesson?id=1"
let lesson = container.openURL(url: resolveURL)
self.showViewController(lesson, sender: nil)
DispatchCenter support navigate through conform NavigatorType protocol。see below code
// define a RouteManager and confirm NavigatorType protocol
// register url
// navigate with url
let url= "dispatch://course/lesson"
final class RouteManager: NavigatorType {
static let `default` = RouteManager()
private let container = ServiceManager()
private init() {}
func register() -> Void {
container.register(url: url) { (_, params) -> DepViewController in
let lesson = LessonViewController.create(params)
return lesson
}
}
}
extension RouteManager {
func presentController() -> Void {
let url = "dispatch://course/lesson?id=1"
self.presentURL(url, controllerType: LessonViewController.self, container: container, animated: true)
}
}
more usage in Example and test case
IF you want to contribute, the Contributing guide is the best place to start. If you have questions, feel free to ask.
DispatchCenter is released under the MIT license. See LICENSE for details.