JKServiceManager is a lightweight manager for registering and invoking custom services(app modules), written in Swift.
In Jike app, due to rapid growth of modules for various business, we need a modulized architecture for several causes:
- Faster compilation speed, which matters a lot to Swift developers :)
- Decoupled business logic, each module can be maintained and tested by different people
- Unified service invocation style, take full advantage of Swift language features for simplest code
// Define a service protocol
public protocol TestServiceProtocol: ServiceProtocol {
func doTestJob()
}
// Extend Services class with typed static member for convenience
// Otherwise each caller needs to call (ServiceManager.getInstance(for: "testService") as? TestServiceProtocol)
extension Services {
public static var test: TestServiceProtocol? {
return ServiceManager.getInstance(for: "testService")
}
}
// Implement of a service protocol. Can be defined in another framework/module for decoupling.
class TestService: TestServiceProtocol {
static var isSingleton: Bool = true
static var sharedInstance: ServiceProtocol = TestService()
public required init() {}
func doTestJob() {
print("Test service instance is doing job")
}
}
{
"Services": {
"testService": "TestService"
}
}
// Register and start all services at app launch
ServiceManager.registerAndStartAllServices(in: [Bundle.main])
Services.test?.doTestJob()
Simple enough?
We have a 3 level architecture in Jike app. From top to bottom:
- Services: Implement services, which conform to corresponding service protocols
- Common: Define service protocols, meant to be imported by all business modules
- Core: Infrastructure of app, including JKServiceManager
See example folder.
- iOS 8.0+
- Swift 3.0+
JKServiceManager is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "JKServiceManager"
JKServiceManager is available under the MIT license. See the LICENSE file for more info.