cqrs_mediator 0.1.1 copy "cqrs_mediator: ^0.1.1" to clipboard
cqrs_mediator: ^0.1.1 copied to clipboard

Package that merge between microsoft cqrs and mediator patterns

CQRS-removebg-preview

N|cqrs_mediator

GitHub forks GitHub Repo stars GitHub watchers N|cqrs_mediator

[x] What is CQRS

As described by microsoft CQRS stands for Command and Query Responsibility Segregation, a pattern that separates read and update operations for a data store. Implementing CQRS in > your application can maximize its performance, scalability, and security. The flexibility created by migrating to CQRS allows a system to better evolve > over time and prevents update commands from causing merge conflicts at the domain level.

[x] What is Mediator

Mediator is a behavioral design pattern that lets you reduce chaotic dependencies between objects. The pattern restricts direct communications between the objects and forces them to collaborate only via a mediator object

[x] the idea is to merge between them and make simple way to code

if you are comming from .net there is library called MediatR this one is similar to it

How to use it #

There are 2 types of commands , ICommand & IQuery

ICommand #

Sync #

  • First create your command & CommandHandler
   class MyCommand extends ICommand {}

   class CommandHandler extends ICommandHandler<MyCommand> {
     @override
     void call(MyCommand command){
         // your code here
     }
    }
  • Then use it

void main(){
     Mediator.instance.registerHandler(()=> CommandHandler());
}

/// later in your code

TextButton(
    onPressed:(){
        Mediator.instance.run(MyCommand());
    }
)

Async #

  • First create your command & AsyncCommandHandler
   class MyAsyncCommand extends IAsyncCommand {}

   class MyAsyncCommandHandler extends IAsyncCommandHandler<MyAsyncCommand> {
     @override
     void call(MyCommand command){
         // your code here
     }
    }
  • Then use it

void main(){
     Mediator.instance.registerHandler(()=> MyAsyncCommandHandler());
}

/// later in your code

TextButton(
    onPressed:(){
        Mediator.instance.run(MyAsyncCommand());
    }
)

[x] Note when register the same instance twice then one only be registered

  /// will called only one time
  Mediator.instance.registerHandler(()=> CommandHandler());
  Mediator.instance.registerHandler(()=> CommandHandler());

IQuery #

Sync #

  • First create your Query & QueryHandler
   class MyQuery extends IQuery<String> {}

   class QueryHandler extends IQueryHandler<String,MyQuery> {
     @override
     string call(MyQuery query){
        return 'Your result';
     }
    }
  • Then use it

void main(){
     Mediator.instance.registerHandler(()=> QueryHandler());
}

/// later in your code

TextButton(
    onPressed:(){
      /// get result from the first handler
      var result=  Mediator.instance.run(MyQuery());
      print(result);

      /// get result from all handlers
      var result=  Mediator.instance.runAll(MyQuery());
      print(result);
    }
)

Async #

  • First create your AsyncQuery & AsyncQueryHandler
   class MyAsyncQuery extends IAsyncQuery<String> {}

   class AsyncQueryHandler extends IAsyncQueryHandler<String,MyAsyncQuery> {
     @override
     Future<string> call(MyAsyncQuery query){
        return 'Your result';
     }
    }
  • Then use it

void main(){
     Mediator.instance.registerHandler(()=> AsyncQueryHandler());
}

/// later in your code

TextButton(
    onPressed:() async{
      /// get result from the first handler
      var result=await  Mediator.instance.run(MyQuery());
      print(result);

      /// get result from all handlers
      var result=await Future.wait( Mediator.instance.runAll(MyQuery()));
      print(result);
    }
)

10
likes
160
pub points
50%
popularity

Publisher

verified publishernew-step-apps.com

Package that merge between microsoft cqrs and mediator patterns

Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD-3-Clause (license)

More

Packages that depend on cqrs_mediator