fl_bustter 1.0.1
fl_bustter: ^1.0.1 copied to clipboard
A flexible CQRS-style message bus and validation package for Dart & Flutter.
fl_bustter #
A lightweight Dart command bus and validation framework inspired by CQRS (Command Query Responsibility Segregation) patterns. Designed for clean, testable, and scalable architecture in Dart or Flutter applications.
β¨ Features #
- π¨
Busster: A simple request/handler dispatcher. - β
IValidator: Built-in validation system with rules likenotEmpty,emailAddress,mustMatch,isIn, andnested. - π¦ Easy integration with custom request types and handlers.
- π§ͺ Fully testable.
π¦ Installation #
Add this to your pubspec.yaml:
dependencies:
fl_bustter: ^1.0.1
Then run:
dart pub get
π¦ Getting Started #
1. Create a Response #
class LoginResponse {
final String token;
LoginResponse(this.token);
}
2. Create a Request #
class LoginRequest extends IRequest<LoginResponse> {
final String email;
final String password;
LoginRequest({required this.email, required this.password});
}
3. Create a Validator #
class LoginValidator extends IValidator<LoginRequest> {
@override
void buildRules() {
ruleFor((x) => x.email, 'email')
.notEmpty()
.emailAddress()
.withMessage('Please enter a valid email');
ruleFor((x) => x.password, 'password')
.notEmpty()
.minLength(6)
.withMessage('Password must be at least 6 characters');
}
}
4. Create a Request #
class LoginRequest extends IRequest<String> {
final String email;
final String password;
LoginRequest({required this.email, required this.password});
}
5. Create a Handler #
class LoginHandler extends IHandler<LoginRequest, LoginResponse> {
LoginHandler() : super(LoginValidator());
@override
Future<LoginResponse> handle(LoginRequest request) async {
await Future.delayed(Duration(seconds: 5));
// Simulate login logic
if (request.email == 'test@example.com' &&
request.password == 'password123') {
return LoginResponse("token_string_example");
} else {
throw Exception('Invalid email or password');
}
}
}
5. Register in Busster #
void main() {
final busster = Busster();
// Register the handler
busster.registerHandler<LoginRequest, LoginResponse>(LoginHandler());
}
6. Use Busster #
void call() async {
try {
final request = LoginRequest(email: 'test@example.com', password: 'password123');
final result = await busster.send<LoginResponse>(request);
print(result.token); // Login successful: token_string_example
} on ValidationException catch (e) {
print('Validation failed:');
print(e.toString());
} catch (e) {
print('Error: $e');
}
}
π Example #
See example/main.dart for a working login use case.
π§ͺ Tests #
Run tests using:
dart test
π License #
This project is licensed under the MIT License.