Last active
August 7, 2022 02:48
-
-
Save jtabuloc/017dcbcf1d8f51ded6acf723fa6d92ba to your computer and use it in GitHub Desktop.
Generic MediatR Request Handler with Generic Repository
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| namespace Customer.WebApi.Controllers | |
| { | |
| [ApiController] | |
| [Route("api/v{version:apiVersion}/[controller]")] | |
| public abstract class BaseApiController<TDto> : ControllerBase where TDto : class, new() | |
| { | |
| private IMediator _mediator; | |
| protected IMediator Mediator => _mediator ??= HttpContext.RequestServices.GetService<IMediator>(); | |
| [HttpDelete("Remove")] | |
| public virtual async Task<IActionResult> Delete(TDto model) | |
| { | |
| return Ok(await Mediator.Send(new RemoveCommand<TDto>(model))); | |
| } | |
| } | |
| } | |
| namespace Customer.WebApi.Controllers.v1 | |
| { | |
| [ApiVersion("1.0")] | |
| public class CustomerController : BaseApiController<CustomerDto> | |
| { | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| namespace Core.Application.Command.Remove | |
| { | |
| public class RemoveCommand<TDto> : IRequest<Response<TDto>> where TDto : class, new() | |
| { | |
| public TDto Data { get; set; } | |
| public RemoveCommand(TDto dto) | |
| { | |
| Data = dto; | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| namespace Core.Application.Command.Remove | |
| { | |
| public class RemoveQueryHandler<TEntity, TDto> : IRequestHandler<RemoveCommand<TDto>, Response<TDto>> | |
| where TEntity : AuditableBaseEntity | |
| where TDto : class, new() | |
| { | |
| private readonly IMapper _mapper; | |
| private readonly IBaseRepositoryAsync<TEntity> _genericRepository; | |
| public RemoveQueryHandler(IBaseRepositoryAsync<TEntity> genericRepository, IMapper mapper) | |
| { | |
| _mapper = mapper; | |
| _genericRepository = genericRepository; | |
| } | |
| public async Task<Response<TDto>> Handle(RemoveCommand<TDto> command, CancellationToken cancellationToken) | |
| { | |
| // Sample execution only | |
| var entity = _mapper.Map<TEntity>(command.Data); | |
| var isDeleted = await _genericRepository.DeleteAsync(entity); | |
| var model = _mapper.Map<TDto>(entity); | |
| return await Task.FromResult(new Response<TDto>(model)); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| namespace Core.Application.DTOs | |
| { | |
| public class CustomerDto | |
| { | |
| public int Id { get; set; } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| namespace Core.Domain.Entities | |
| { | |
| public abstract class AuditableBaseEntity | |
| { | |
| [Key] | |
| public virtual int Id { get; set; } | |
| } | |
| [Table("Customer")] | |
| public class CustomerEntity : AuditableBaseEntity | |
| { | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| namespace Core.Application | |
| { | |
| public static class ServiceExtensions | |
| { | |
| public static void AddApplicationLayer(this IServiceCollection services) | |
| { | |
| var assembly = Assembly.GetExecutingAssembly(); | |
| services.AddAutoMapper(assembly); | |
| services.AddValidatorsFromAssembly(assembly); | |
| services.AddMediatR(assembly); | |
| services.AddTransient(typeof(IPipelineBehavior<,>), typeof(ValidationBehavior<,>)); | |
| services.AddTransient(typeof(IRequestHandler<RemoveCommand<CustomerDto>, Response<CustomerDto>>), typeof(RemoveQueryHandler<CustomerEntity, CustomerDto>)); | |
| services.AddTransient(typeof(IBaseRepositoryAsync<>), typeof(BaseRepositoryAsync<>)); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment