September 24 2024


public interface IQueryHandler<in TQuery, TQueryResult> { Task<TQueryResult> Handle(TQuery query, CancellationToken cancellation); }
public interface ICommandHandler<in TCommand, TCommandResult> { Task<TCommandResult> Handle(TCommand command, CancellationToken cancellation); }
public interface IQueryDispatcher { Task<TQueryResult> Dispatch<TQuery, TQueryResult>(TQuery query, CancellationToken cancellation); }
public interface ICommandDispatcher { Task<TCommandResult> Dispatch<TCommand, TCommandResult>(TCommand command, CancellationToken cancellation); }
public class QueryDispatcher(IServiceProvider serviceProvider) : IQueryDispatcher { private readonly IServiceProvider _serviceProvider = serviceProvider; public Task<TQueryResult> Dispatch<TQuery, TQueryResult>(TQuery query, CancellationToken cancellation) { var handler = _serviceProvider.GetRequiredService<IQueryHandler<TQuery, TQueryResult>>(); return handler.Handle(query, cancellation); } }
public class CommandDispatcher(IServiceProvider serviceProvider) : ICommandDispatcher { private readonly IServiceProvider _serviceProvider = serviceProvider; public Task<TCommandResult> Dispatch<TCommand, TCommandResult>(TCommand command, CancellationToken cancellation) { var handler = _serviceProvider.GetRequiredService<ICommandHandler<TCommand, TCommandResult>>(); return handler.Handle(command, cancellation); } }
[Route("api/[controller]")] [ApiController] public class UsersController(IQueryDispatcher queryDispatcher, ICommandDispatcher commandDispatcher) : ControllerBase { private readonly IQueryDispatcher _queryDispatcher = queryDispatcher; private readonly ICommandDispatcher _commandDispatcher = commandDispatcher; [HttpGet("{id}")] public async Task<IActionResult> GetById(Guid id, CancellationToken cancellationToken) { var query = new GetUserByIdQuery { UserId = id }; var user = await _queryDispatcher.Dispatch<GetUserByIdQuery, User>(query, cancellationToken); if (user == null) { return NotFound(); } return Ok(user); } }
public class GetUserByIdQueryHandler : IQueryHandler<GetUserByIdQuery, User> { public GetUserByIdQueryHandler() { } public async Task<User> Handle(GetUserByIdQuery query, CancellationToken cancellationToken) { //Call Repository return new User(); } }
1. Design Patterns that Deliver
This isn’t just another design patterns book. Dive into real-world examples and practical solutions to real problems in real applications.Check out it here.
Go-to resource for understanding the core concepts of design patterns without the overwhelming complexity. In this concise and affordable ebook, I've distilled the essence of design patterns into an easy-to-digest format. It is a Beginner level. Check out it here.
Every Monday morning, I share 1 actionable tip on C#, .NET & Arcitecture topic, that you can use right away.
Join 18,000+ subscribers to improve your .NET Knowledge.
Subscribe to the TheCodeMan.net and be among the 18,000+ subscribers gaining practical tips and resources to enhance your .NET expertise.