Giter Site home page Giter Site logo

flutter_mediator's Introduction

Dart Mediator Package

The Dart Mediator package provides an implementation of the Mediator pattern for Dart inspired by MediatR ASP.net Core, including dynamic handler registration via code generation. This pattern is useful for organizing your application's commands and queries by centralizing their processing through a mediator.

What is the Mediator Pattern?

The Mediator pattern is a behavioral design pattern that centralizes request handling and execution. It promotes loose coupling between components by removing direct dependencies between them. Instead of components communicating directly with each other, they communicate through a mediator object. This allows for more flexible and maintainable code, as components can be easily added, removed, or modified without affecting other components.

Why use the Mediator Pattern?

The Mediator pattern is useful for organizing your application's commands and queries by centralizing their processing

What is the CQRS?

CQRS stands for Command Query Responsibility Segregation. It is a design pattern that separates the read and write operations of an application. Commands are used to perform state changes, while queries are used to retrieve data. This separation allows for better scalability and maintainability of the application.

Why use the Dart Mediator Package?

The Dart Mediator package provides an implementation of the Mediator pattern for Dart inspired by MediatR ASP.net Core,

Features

  • Dynamic Handler Registration: Automatically registers handlers using code generation.
  • CQRS Support: Facilitates separation of commands (state changes) and queries (data retrieval).
  • Command and Query Handlers: Supports both command and query handlers.
  • Handler Annotations: Use annotations CommandHandler, QueryHandler and RequestHandler to auto-register handlers using code generation.
  • Handler Registration: Manually register handlers using the registerCommandHandler and registerQueryHandler methods.

Getting Started

Installation

To use the Dart Mediator package, add it to your pubspec.yaml:

dependencies:
  dart_mediatr: ^1.0.3

Then, run dart pub get to install the package.

Usage

To use the Mediator package, you need to create a Mediator instance and register your handlers. The Mediator class is the central component that processes requests and routes them to the appropriate handler.

you can register handlers using the registerCommandHandler and registerQueryHandler methods. or using the CommandHandler and QueryHandler annotation to auto register handlers using code generation.

automatically register handlers.

to automatically register handlers use the build_runner package. Add the package to your dev_dependencies in pubspec.yaml:

dev_dependencies:
  build_runner: ^2.0.0

Example

Create a command to create a user:

import 'package:dart_mediatr/dart_mediatr.dart';

class CreateUserCommand extends ICommand<CreateUserCommandResponse> {
  final String name;
  final String email;

  CreateUserCommand(this.name, this.email);
}
import 'package:dart_mediatr/dart_mediatr.dart';
import 'package:example/createUserCommand/create_user_command.dart';
import 'package:example/createUserCommand/create_user_command_response.dart';

@CommandHandler()
class CreateUserCommandHandler
    extends ICommandHandler<CreateUserCommand, CreateUserCommandResponse> {
  @override
  CreateUserCommandResponse handle(CreateUserCommand command) {
    return CreateUserCommandResponse(
        id: "", email: command.email, name: command.name);
  }
}
class CreateUserCommandResponse {
  final String id;
  final String name;
  final String email;

  CreateUserCommandResponse({
    required this.id,
    required this.name,
    required this.email,
  });
}

then add the @MediatorInit() annotation to your main function to generate the registration code:

import 'package:dart_mediatr/dart_mediatr.dart';

@MediatorInit()
void main() async {

  runApp(const MyApp());
}

Then, run the build_runner to generate the registration code:

dart run build_runner build

then import the generated file in your main file and invoke the registerAllHandlers function:

import 'package:dart_mediatr/dart_mediatr.dart';
import 'main.mediator.dart';

@MediatorInit()
Future<void> main() async {
  registerAllHandlers();

  runApp(const MyApp());
}

Finally, you can use the Mediator class to send commands and queries:

void main() async {
  Mediator mediator = Mediator();

  CreateUserCommand command = CreateUserCommand('faraj shuaib', '[email protected]');

  CreateUserCommandResponse response = mediator.sendCommand<CreateUserCommand, CreateUserCommandResponse>(command);

Manual Handler Registration

if you hate generated code and you want to manually register handlers, use the registerCommandHandler and registerQueryHandler methods:

import 'package:dart_mediatr/dart_mediatr.dart';

void main() async {
  Mediator mediator = Mediator();

  mediator.registerCommandHandler(CreateUserCommandHandler());

  runApp(const MyApp());
}

then you can use the Mediator class to send commands and queries as shown above.

Contributing

Contributions are welcome! Please follow these steps to contribute:

  1. Fork the repository.
  2. Create a new branch (git checkout -b feature-branch).
  3. Make your changes.
  4. Run the tests (dart test).
  5. Commit your changes (git commit -am 'Add new feature').
  6. Push to the branch (git push origin feature-branch).
  7. Create a new Pull Request.
  8. Get your changes reviewed.

License

This package is licensed under the MIT License. See the LICENSE file for details.

flutter_mediator's People

Contributors

farajshuaib avatar badeesabood avatar

Stargazers

Omar Almgerbie avatar Abdulalim Elmozogi avatar

Watchers

 avatar

Forkers

badeesabood

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.