cqrs_mediator

Creator: coderz1093

Last updated:

0 purchases

cqrs_mediator Image
cqrs_mediator Images

Languages

Categories

Add to Cart

Description:

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
}
}
copied to clipboard

Then use it


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

/// later in your code

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

copied to clipboard
Async #

First create your command & AsyncCommandHandler

class MyAsyncCommand extends IAsyncCommand {}

class MyAsyncCommandHandler extends IAsyncCommandHandler<MyAsyncCommand> {
@override
void call(MyCommand command){
// your code here
}
}
copied to clipboard

Then use it


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

/// later in your code

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

copied to clipboard
[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());
copied to clipboard
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';
}
}
copied to clipboard

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);
}
)

copied to clipboard
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';
}
}
copied to clipboard

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);
}
)

copied to clipboard

License

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files In This Product:

Customer Reviews

There are no reviews.