cmdr is a Dart package that provides a lightweight implementation of the Mediator pattern tailored for Command Query Responsibility Segregation (CQRS) applications.
Installation Add cmdr to your pubspec.yaml file:
To use cmdr
in your Dart project, add it to your pubspec.yaml
file:
dependencies:
cmdr: ^1.0.0
then run:
dart pub get
import 'package:cmdr/cmdr.dart';
var mediator = Mediator();
class MyCommandHandler CommandHandler<MyQuery,dynamic> {
Future<void> handle(MyCommand command) async {
// Command handling logic
}
}
class MyQueryHandler extends QueryHandler<MyQuery,dynamic> {
Future<Result> handle(MyQuery query) async {
// Query handling logic
}
}
var commandHandler = MyCommandHandler();
var queryHandler = MyQueryHandler();
mediator.registerHandler(MyCommand, commandHandler);
mediator.registerHandler(MyQuery, queryHandler);
var command = MyCommand();
var query = MyQuery();
await mediator.send(command);
var result = await mediator.send(query);
Registers a handler for a specific type of command or query.
void registerHandler(Type type, dynamic handler)
type: The type of command or query.
handler: The handler responsible for handling commands or queries of the specified type.
Sends a command or query to the registered handler.
Future<dynamic> send(dynamic request)
request: The command or query to be sent to the handler.
For more detailed examples, please refer to the example directory.
If you encounter any issues or have suggestions for improvements, feel free to open an issue or submit a pull request in the GitHub repository.