Skip to content

Repository files navigation

cqrs: The Command and Query Responsibility Segregation

cqrs

LicenseSwift CompatibilityPlatform CompatibilityCICodeCov

Description

cqrs is an implementation of the command and query responsibility segregation in Swift.

Usage

  1. ICommand and IQuery contain data for use by an appropriate handler.

Create an instance of a command that conforms to ICommand or a query that conforms to IQuery, respectively:

import CQRS
finalclassExampleCommand:ICommand{
// MARK: Properties
letvalue:Int
// MARK: Initialization
init(value:Int){self.value = value
}}
import CQRS
finalclassExampleQuery:IQueue{typealiasResult=Int
// MARK: Properties
letvalue:Int
// MARK: Initialization
init(value:Int){self.value = value
}}
  1. A command handler or a query handler contains the execution logic for a command or a query.

Create an instance of a command handler that conforms to ICommandHandler or a query handler that conforms to IQueryHandler, respectively:

import CQRS
finalclassExampleCommandHandler:ICommandHandler{typealiasCommand=ExampleCommand
// MARK: ICommandHandler
func execute(command:Command)throws{
// write the execution logic here
}}
import CQRS
finalclassExampleQueryHandler:IQueryHandler{typealiasQuery=ExampleQuery
// MARK: IQueryHandler
func execute(query:Query)throws->Query.Result{
// write the execution logic here
}}
  1. Register your handler implementation in the container:
import CQRS
letcontainer=DependencyContainer()
container.register{ExampleCommandHandler()}
import CQRS
letcontainer=DependencyContainer()
container.register{ExampleQueryHandler()}
  1. Create an instance of a CommandDispatcher or a QueryDispatcher with the created container, like this:
import CQRS
letcommandDispatcher=CommandDispatcher(container: container)
import CQRS
letcommandDispatcher=QueryDispatcher(container: container)
  1. Execute your command on commandDispatcher or queryDispatcher:
letcommand=ExampleCommand()do{try commandDispatcher.execute(command: command)}catch{
// Handle an error.
}
letquery=ExampleCommand()do{letresult=try queryDispatcher.execute(query: query)}catch{
// Handle an error.
}

Requirements

  • iOS 16.0+ / macOS 13+ / tvOS 16.0+ / watchOS 9.0+
  • Xcode 14.0
  • Swift 5.7

Installation

Swift Package Manager

The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift compiler. It is in early development, but cqrs does support its use on supported platforms.

Once you have your Swift package set up, adding cqrs as a dependency is as easy as adding it to the dependencies value of your Package.swift.

dependencies:[.package(url:"https://github.com/space-code/cqrs.git",.upToNextMajor(from:"1.0.1"))]

Communication

  • If you found a bug, open an issue.
  • If you have a feature request, open an issue.
  • If you want to contribute, submit a pull request.

Contributing

Bootstrapping development environment

make bootstrap

Please feel free to help out with this project! If you see something that could be made better or want a new feature, open up an issue or send a Pull Request!

Author

Nikita Vasilev, nv3212@gmail.com

License

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

About

The Command and Query Responsibility Segregation

Topics

Resources

Stars

5 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages