-
Notifications
You must be signed in to change notification settings - Fork 0
Getting Started
ferdi kurnaz edited this page Sep 1, 2026
·
1 revision
This page shows the basic steps to start using Mediatron in your project.
Mediatron can scan your assembly and register all your handlers automatically.
using Mediatron.Extensions;
var builder = WebApplication.CreateBuilder(args);
// Register Mediatron and scan the current assembly for handlers
builder.Services.AddMediatron(typeof(Program).Assembly);
var app = builder.Build();A "request" is a command or a query. Each request needs a handler that contains the logic.
using Mediatron.Abstractions;
// 1. Define the Command/Query
public record CreateProductCommand(string Name, decimal Price) : IRequest<int>;
// 2. Define the Handler
public class CreateProductCommandHandler : IRequestHandler<CreateProductCommand, int>
{
public async Task<int> Handle(CreateProductCommand request, CancellationToken cancellationToken)
{
// Your business logic here (e.g., save to database)
int generatedId = 42;
return await Task.FromResult(generatedId);
}
}Inject IMediator into your controller or service, then send the request.
[ApiController]
[Route("api/[controller]")]
public class ProductsController : ControllerBase
{
private readonly IMediator _mediator;
public ProductsController(IMediator mediator)
{
_mediator = mediator;
}
[HttpPost]
public async Task<IActionResult> Create([FromBody] CreateProductCommand command)
{
var productId = await _mediator.SendAsync<CreateProductCommand, int>(command);
return Ok(new { ProductId = productId });
}
}That's it! Your first command is now working end-to-end.
Next, read Requests and Handlers (CQRS) and Notifications (Pub/Sub) to learn more.