JITDispatcher 1.0.0
dotnet add package JITDispatcher --version 1.0.0
NuGet\Install-Package JITDispatcher -Version 1.0.0
<PackageReference Include="JITDispatcher" Version="1.0.0" />
<PackageVersion Include="JITDispatcher" Version="1.0.0" />
<PackageReference Include="JITDispatcher" />
paket add JITDispatcher --version 1.0.0
#r "nuget: JITDispatcher, 1.0.0"
#addin nuget:?package=JITDispatcher&version=1.0.0
#tool nuget:?package=JITDispatcher&version=1.0.0
JITDispatcher
Tiny framework for whom wants to taste CQRS
Table of Contents
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:
- 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; } }
- Define Queries
internal class GetTodoByIdQuery : IQuery<TodoDto>
{
public GetTodoByIdQuery(Guid id)
{
Id = id;
}
public Guid Id { get; }
}
- 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; }
}
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; } }
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); } }
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); } }
Register to Service collection in startup.cs:
services.AddJITDispatcher(Assembly.GetExecutingAssembly());
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:
- Fork the repository.
- Create a new branch (
git checkout -b feature/YourFeature
). - Commit your changes (
git commit -m 'Add YourFeature'
). - Push to the branch (
git push origin feature/YourFeature
). - 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 | Versions 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. |
-
net9.0
- Microsoft.Extensions.Caching.Memory (>= 9.0.3)
- Newtonsoft.Json (>= 13.0.3)
- Optional (>= 4.0.0)
- Optional.Async (>= 1.3.0)
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 |