Skip to content

iftech-engineering/JKServiceManager

Repository files navigation

JKServiceManager

Language Version License Platform Build Status

JKServiceManager is a lightweight manager for registering and invoking custom services(app modules), written in Swift.

Inspiration

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

Usage

1. Define Service Protocol

// 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")
    }
}

2. Implement Service

// 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")
    }
}

3. Add to Services.json

{
    "Services": {
        "testService": "TestService"
    }
}

4. Invoke Service

// Register and start all services at app launch
ServiceManager.registerAndStartAllServices(in: [Bundle.main])

Services.test?.doTestJob()

Simple enough?

Architecture

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

Example

See example folder.

Requirements

  • iOS 8.0+
  • Swift 3.0+

Installation

JKServiceManager is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "JKServiceManager"

License

JKServiceManager is available under the MIT license. See the LICENSE file for more info.