0 purchases
cqrs
cqrs #
A library for convenient communication with CQRS-compatible backends, using queries and commands.
Usage example #
import 'package:cqrs/cqrs.dart';
// You may use json_serializable for cutting on boilerplate code for your queries...
class AllFlowers implements Query<List<Flower>> {
const AllFlowers({required this.page});
final int page;
@override
String getFullName() => 'AllFlowers';
@override
List<Flower> resultFactory(dynamic json) {
return List<Flower>.of(json as List).map(Flower.fromJson).toList();
}
@override
Map<String, dynamic> toJson() => {'page': page};
}
// ...as well as commands.
class AddFlower implements Command {
const AddFlower({
required this.name,
required this.pretty,
});
final String name;
final bool pretty;
@override
String getFullName() => 'AddFlower';
@override
Map<String, dynamic> toJson() => {'Name': name, 'Pretty': pretty};
}
// Firstly you need an Uri to which the requests will be sent.
// Remember about the trailing slash as otherwise resolved paths
// may be invalid.
final apiUri = Uri.parse('https://flowers.garden/api/');
// Then construct a CQRS instance using the just created Uri
// and an HTTP client which in most cases will be probably handling
// refreshment of the tokens and their rotation.
final cqrs = Cqrs(loginClient, apiUri);
// Fetch first page of the all flowers query from the CQRS server.
final flowers = await cqrs.get(AllFlowers(page: 1));
// Run a command called add flower which... adds a pretty Daisy.
final result = await cqrs.run(AddFlower(name: 'Daisy', pretty: true));
// You can check the command result for its status, whether it successfully ran.
if (result case CommandSuccess()) {
print('Added a daisy successfully!');
} else if (result case CommandFailure(isInvalid: true, :final validationErrors)) {
print('Validation errors occured!');
handleValidationErrors(validationErrors);
} else {
print('Error occured');
}
copied to clipboard
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.