![](https://img.shields.io/badge/Swift 2-compatible-4BC51D.svg?style=flat-square)
A light weight Command Bus implementation written in Swift
The idea of a command bus is that you create command objects that represent what you want your application to do. Then, you toss it into the bus and the bus makes sure that the command object gets to where it needs to go.
So, the command goes in -> the bus hands it off to a handler -> and then the handler actually does the job. The command essentially represents a method call to your application layer.
You can have more informations here.
CommandBus is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "CommandBus", '~> 0.0.7'
- First, you have to create a json mapping file in order to associate yours Commands with their handler:
{
"{CommandNameA}": "{CommandHandlerNameA}",
"{CommandNameB}": "{CommandHandlerNameB}",
"{CommandNameC}": "{CommandHandlerNameC}"
}
- Then you can create your Command and inject it to the bus:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
/*** Register to the Command event ***/
NSNotificationCenter.defaultCenter().addObserver(self, selector: "onCommandHandled:", name:"COMMAND_DONE", object: nil)
/*** Create the CommandBus ***/
let commandBus: CommandBus = CommandBus(configurationFileName: "configuration")!
/*** Create your own Command ***/
let customCommand: CustomCommand = CustomCommand()
/*** Send your command to the CommandBus with your event name ***/
commandBus.handle(command: customCommand, commandHandledEvent: "COMMAND_DONE")
}
func onCommandHandled(notification: NSNotification) {
/*** This method is called when the CommandHandler have done ***/
print("Command Handled: \(notification.object!)")
}
}
You can also see the Example project.
Ekhoo:
CommandBus is available under the MIT license. See the LICENSE file for more info.