JITDispatcher 1.0.0

dotnet add package JITDispatcher --version 1.0.0
                    
NuGet\Install-Package JITDispatcher -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="JITDispatcher" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="JITDispatcher" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="JITDispatcher" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add JITDispatcher --version 1.0.0
                    
#r "nuget: JITDispatcher, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=JITDispatcher&version=1.0.0
                    
Install JITDispatcher as a Cake Addin
#tool nuget:?package=JITDispatcher&version=1.0.0
                    
Install JITDispatcher as a Cake Tool

JITDispatcher

Tiny framework for whom wants to taste CQRS

License Language

Table of Contents

  1. Introduction
  2. Features
  3. Getting Started
  4. Installation
  5. Usage
  6. Contributing
  7. License

Introduction

JITDispatcher is a lightweight framework for developers who are interested in exploring the Command Query Responsibility Segregation (CQRS) pattern. It provides a simple and efficient way to implement CQRS in your applications, making it ideal for learning or small projects.


Features

  • Lightweight and Simple: Easy to integrate into your projects.
  • CQRS Pattern: Implements the separation of commands and queries for better scalability and maintainability.
  • C# Language: Built entirely in C# for .NET developers.

Getting Started

These instructions will help you get started with JITDispatcher in your project.

Prerequisites

  • .NET SDK installed on your machine.
  • Basic knowledge of C# and CQRS.

Installation

To install JITDispatcher, you can use the NuGet Package Manager.

dotnet add package JITDispatcher

Alternatively, you can clone the repository and include it in your project manually:

git clone https://github.com/khaledov/JITDispatcher.git

Usage

Below is an example of how to use JITDispatcher in your project:

  1. Define Commands and its Validator:
    internal class AddTodoCommand : ICommand
     {
         public AddTodoCommand(string title)
         {
            ........
         }
         .............
     }
     internal class AddTodoCommandValidator : ICommandValidator<AddTodoCommand>
     {
    
    
    
         public ValidationResult Validate(AddTodoCommand command)
         {
             var result = new ValidationResult();
             if (string.IsNullOrWhiteSpace(command.Title))
             {
                 result.ErrorMessages.Add("Title is required");
             }
    
             return result;
         }
    
    
     }
    
    
    
  2. Define Queries
    internal class GetTodoByIdQuery : IQuery<TodoDto>
    {
        public GetTodoByIdQuery(Guid id)
        {
            Id = id;
        }
        public Guid Id { get; }
    }

  1. Define Events
    internal class TodoAddedEvent : IEvent
    {
        public readonly string Title;
        public TodoAddedEvent(Guid id,string title)
        {
            Id = id;
            Title = title;
        }
        public Guid Id { get; set; }
        public int Version { get; set; }
        public DateTimeOffset TimeStamp { get; set; }
    }


  1. Create CommandHandlers:

     internal class AddTodoHandler : ICommandHandler<AddTodoCommand>
     {
    
    
         public Task Execute(AddTodoCommand command, CancellationToken cancellationToken)
         {
             var todo = new TodoDto(command.Id, command.Title, command.Description, command.IsCompleted);
    
             InMemoryDatabase.DataBase.Add(todo);
             return Task.CompletedTask;
         }
     }
    
    
  2. Create Query Handler

     internal class GetTodoByIdQueryHandler : IQueryHandler<GetTodoByIdQuery, TodoDto>
     {
    
         public async Task<TodoDto> Execute(GetTodoByIdQuery query, CancellationToken cancellationToken)
         {
             var todo = InMemoryDatabase.DataBase.FirstOrDefault(t => t.Id == query.Id);
             return await Task.FromResult(todo);
         }
     }
    
  3. Create Event Handler

    internal class TodoAddedEventHandler : IEventHandler<TodoAddedEvent>
    {
        public async Task Handle(TodoAddedEvent @event, CancellationToken cancellationToken)
        {
            await Task.Run(() =>
            {
                Console.WriteLine($"Todo added: {@event.Id} - {@event.Title}");
            }, cancellationToken);
        }
    }
    
    
    
  4. Register to Service collection in startup.cs:

    services.AddJITDispatcher(Assembly.GetExecutingAssembly());
    
  5. Resolve Dispatcher wherever you want to invoke it

    MyConstructor(IDispatcher dispatcher)
    {
        _dispatcher=dispatcher;
    }
    
    ....
    ....
    ....
    //Command
    var cmd =new AddTodoCommand(title: title);
    var result=dispatcher.SendAsync(cmd, CancellationToken.None).GetAwaiter().GetResult(); 
      if(result.IsValid)
      {
          //Events
          var @event=new TodoAddedEvent(cmd.Id,cmd.Title);
         await dispatcher.PublishAsync(@event, CancellationToken.None);
      }else
          Console.WriteLine(result.ErrorMessages.ToList().Select(e=>e));
      Console.WriteLine("*************************************");
     
     //Query 
    var query = new GetTodoByIdQuery(id);
    var todo = await dispatcher.QueryAsync<GetTodoByIdQuery, TodoDto>(query, CancellationToken.None);
   

Contributing

Contributions are welcome! If you would like to contribute to this project, please follow these steps:

  1. Fork the repository.
  2. Create a new branch (git checkout -b feature/YourFeature).
  3. Commit your changes (git commit -m 'Add YourFeature').
  4. Push to the branch (git push origin feature/YourFeature).
  5. Open a pull request.

License

This project is licensed under the Apache2.0.


Acknowledgments

  • Inspired by the principles of CQRS.
  • Special thanks to the open-source community for their contributions.

Feel free to reach out if you have any questions or suggestions!

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 154 4/21/2025