watermelon_mediator 0.0.1 watermelon_mediator: ^0.0.1 copied to clipboard
A pure dart package to use the Mediator design pattern in all dart projects, specially flutter mobile apps.
Intro #
This is a 'Make to it easy' package that makes your code style easy and more readable and make your code more clean.
It combines the Mediator design pattern, Single Responsibility principle, and CQRS pattern.
How to make it easy? #
final query = ExampleQuery();
final response = await _mediator.pushAsync<ExampleQuery, ExampleResponse>(query);
// Or ExampleCommand, this naming back to you
// about how do you want to use Command and
// Query pattern
Assume you have a considerable amount of logic, and you should create an instance from all of them one by one and import and use. The above line sits instead of all of those imports and names. You make a 'Query' that can pass your data and let the Mediator handle the rest.
How config it? #
@MediatSRequest(requestName: 'AddCarRequest')
class AddExampleCommand implements RequestModel<void> {
AddExampleCommand({required this.data});
final Data data;
}
RequestModel
model is part of a package that lets the package know
that the example class is a 'Request' type.
Request types are like event parmeters that are able to carry data.
@MediatSHandler(requestName: 'AddExampleRequest')
class AddExampleHandler implements RequestHandler<AddExampleCommand, void> {
final mockCars = MockUpData();
@override
void handle(AddExampleCommand request) {
// You don't need to write here if you don't
// need a synchronous methods.
throw UnimplementedError();
}
@override
Future<void> handleAsync(AddExampleCommand request) {
// You don't need to write here if you don't
// need a asynchronous methods.
throw UnimplementedError();
}
}
RequestHandler
accept a generic type, one for RequestModel
and
another one for ResponseModel.
You are free to create a custom ResponseModel to
have more clean code.
Your handler class offers two 'handle' methods, one
for synchronous and one for asynchronous. Logically, if
you use one, you should leave the other.
At the end you should add
RequestsRegister();
at top of the runApp(...);
method in your main.dart
class, and then
run this command
dart run watermelon_mediator -path [WRITE THE SAVE LOCATION OF RequestsRegister() PATH]
If can remove -path argument, it generate in lib.
How to use? #
For example, in your state manager or, any part of your
architect that you want to call a service or logic, and
you can import watermelon_mediator
package. By passing
the request model to it, it finds the handler automatically
and pass result to it to you.
class ExampleSateManager {
final _mediator = Mediator();
Future<List<Example>> getExampleList() async {
final query = GetExampleListQuery(pageIndex: 0, orderNum: 2);
final exampleResponse =
await _mediator.pushAsync<GetExampleListQuery, GetExampleListResponse>(query);
return exampleResponse.examples;
}
void addCar(String name) {
final example = Example(name: name);
final command = AddExampleCommand(example: example);
_mediator.push(command);
}
}