27
loading...
This website collects cookies to deliver better user experience
├── Namespace.Application
│ ├── Commands - Your commands
│ │ ├── MyEntity - Your entity
│ │ │ ├── MyEntityCreateCommand - A command
│ │ │ ├── MyEntityCreateCommandHandler - A command handler
│ ├── Queries - Your queries
│ │ ├── MyEntity - Your entity
│ │ │ ├── MyEntityGetQuery - A query
│ │ │ ├── MyEntityGetQueryHandler - A query handler
├── Namespace.Crosscutting
├── Namespace.Domain
├── Namespace.Domain.Services
├── Namespace.Dto
├── Namespace.Infrastructure
MyEntityGetQuery.cs
:namespace MyCompany.Application.Queries {
public class MyEntityGetQuery : IRequest<MyEntity>
{
public long Id { get; set; }
}
}
MyEntityGetQueryHandler.cs
:namespace MyCompany.Application.Queries {
public class MyEntityGetQueryHandler : IRequestHandler<MyEntityGetQuery, MyEntity>
{
private IReadOnlyMyEntityRepository _myEntityRepository;
public MyEntityGetQueryHandler(IReadOnlyMyEntityRepository myEntityRepository)
{
_myEntityRepository = myEntityRepository;
}
public Task<MyEntity> Handle(MyEntityGetQuery request,
CancellationToken cancellationToken)
{
return _myEntityRepository.QueryHelper()
.GetOneAsync(myEntity => myEntity.Id == request.Id);
}
}
}
ReadOnlyRepository
rather than a service in order to do the segregation between Commands and Queries. Lastly, create your routing method within your controller :[HttpGet("my-entity/{id}")]
public async Task<IActionResult> GetMyEntity([FromRoute] long id)
{
var result = await _mediator.Send(new MyEntityGetQuery { Id = id });
return ActionResultUtil.WrapOrNotFound(result);
}