medartor 1.0.0 copy "medartor: ^1.0.0" to clipboard
medartor: ^1.0.0 copied to clipboard

Simple, unambitious CQRS mediator implementation, .NET MediatR inspired.

MeDARTor #

Simple, unambitious CQRS mediator implementation, .NET MediatR inspired.

// declares the message class (command/query) that is constructed with a username and returns a string
class GreetUserRequest implements IRequest<String> {
  final String userName;

  GreetUserRequest({
    required this.userName,
  });
}

// declare a class to handle the 'GreetUserRequest' message, which returns a string
class GreetUserHandler extends IRequestHandler<GreetUserRequest, String> {
  @override
  Future<String> handle(GreetUserRequest request) async {
    return "Hello ${request.userName}!";
  }
}

// Create a Mediator instance
final mediator = Medartor();

// Register the handler for the 'GreetUserRequest' message
mediator.register(GreetUserHandler());

// Create a 'GreetUserRequest' object
final request = GreetUserRequest(userName: "John");

// Send the message and get the response
final response = await mediator.send(request);

// print the answer
print(response); // print "Hello John!"

Multiple handlers can also be used to attend to the same Request, for this case a Key associated with each handler must be registered to identify it and pass it as a parameter to the send method.

class HelloUserHandler extends IRequestHandler<GreetUserRequest, String> {
  @override
  Future<String> handle(GreetUserRequest request) async {
    return "Hello ${request.userName}!";
  }
}

class GoodbyeUserHandler extends IRequestHandler<GreetUserRequest, String> {
  @override
  Future<String> handle(GreetUserRequest request) async {
    return "Goodbye ${request.userName}!";
  }
}

// Create a Mediator instance
final mediator = Medartor();

// Register the handler for the 'GreetUserRequest' message with different keys
mediator.register(HelloUserHandler(), key: "helloHandler");
mediator.register(GoodbyeUserHandler(), key: "goodbyeHandler");

// Create a 'GreetUserRequest' object
final request = GreetUserRequest(userName: "John");

// Send the message and get the response
final response = await mediator.send(request, key: "helloHandler");
// print the answer
print(response); // print "Hello John!"

// Send the message and get the response
final response = await mediator.send(request, key: "goodbyeHandler");

// print the answer
print(response); // print "Goodbye John!"

a good way to implement it in an architecture is to instantiate it and register its handlers at startup in a dependency injector.

The send function also supports a default handler function, which is executed in case the requested handler is not registered.

...
// Send the message and get the response
final response = await mediator.send(request, key: "goodbyeHandlerNotRegistered", 
  defaultHandler: (request) {
    return Future.value('Hello default handler');
  }
);

// print the answer
print(response); // print "Hello default handler"
3
likes
140
points
31
downloads

Publisher

verified publisherindimin.com

Weekly Downloads

Simple, unambitious CQRS mediator implementation, .NET MediatR inspired.

Homepage
Repository (GitHub)

Documentation

API reference

License

MIT (license)

Dependencies

flutter

More

Packages that depend on medartor