cqrs
is an implementation of the command and query responsibility segregation in Swift.
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
final class ExampleCommand: ICommand {
// MARK: Properties
let value: Int
// MARK: Initialization
init(value: Int) {
self.value = value
}
}
import CQRS
final class ExampleQuery: IQueue {
typealias Result = Int
// MARK: Properties
let value: Int
// MARK: Initialization
init(value: Int) {
self.value = value
}
}
Create an instance of a command handler that conforms to ICommandHandler
or a query handler that conforms to IQueryHandler
, respectively:
import CQRS
final class ExampleCommandHandler: ICommandHandler {
typealias Command = ExampleCommand
// MARK: ICommandHandler
func execute(command: Command) throws {
// write the execution logic here
}
}
import CQRS
final class ExampleQueryHandler: IQueryHandler {
typealias Query = ExampleQuery
// MARK: IQueryHandler
func execute(query: Query) throws -> Query.Result {
// write the execution logic here
}
}
import CQRS
let container = DependencyContainer()
container.register { ExampleCommandHandler() }
import CQRS
let container = DependencyContainer()
container.register { ExampleQueryHandler() }
CommandDispatcher
or a QueryDispatcher
with the created container, like this:import CQRS
let commandDispatcher = CommandDispatcher(container: container)
import CQRS
let commandDispatcher = QueryDispatcher(container: container)
commandDispatcher
or queryDispatcher
:let command = ExampleCommand()
do {
try commandDispatcher.execute(command: command)
} catch {
// Handle an error.
}
let query = ExampleCommand()
do {
let result = try queryDispatcher.execute(query: query)
} catch {
// Handle an error.
}
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"))
]
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!
Nikita Vasilev, [email protected]
cqrs is available under the MIT license. See the LICENSE file for more info.
link |
Stars: 0 |
Last commit: 2 weeks ago |
Released on 2023-09-15
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics