Skip to content

Commit

Permalink
Dev (#124)
Browse files Browse the repository at this point in the history
* Fixed DI scope issue with ScopedDataStoreEnlistementProvider. (#121)

The StaticDataStore can be a singleton and still accessed through the StaticDataStoreRegistry with is now "Scoped" rather than a singleton to prevent root scope resolver issues.  Updated example application and added some better exception handling around this area.

* Version bump.

* Bug fix/scoped service resolver issues iunit of work scope (#123)

* Refactored CleanWithCQRS example to use Mediator abstractions.

* Refactored Mediator pattern.
  • Loading branch information
jasonmwebb-lv authored Apr 4, 2024
1 parent c63ed70 commit 5273fd0
Show file tree
Hide file tree
Showing 64 changed files with 176 additions and 139 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using RCommon.Mediator;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -19,9 +20,9 @@ namespace HR.LeaveManagement.Api.Controllers
[Authorize]
public class LeaveAllocationsController : ControllerBase
{
private readonly IMediator _mediator;
private readonly IMediatorService _mediator;

public LeaveAllocationsController(IMediator mediator)
public LeaveAllocationsController(IMediatorService mediator)
{
_mediator = mediator;
}
Expand All @@ -30,15 +31,15 @@ public LeaveAllocationsController(IMediator mediator)
[HttpGet]
public async Task<ActionResult<List<LeaveAllocationDto>>> Get(bool isLoggedInUser = false)
{
var leaveAllocations = await _mediator.Send(new GetLeaveAllocationListRequest() { IsLoggedInUser = isLoggedInUser });
var leaveAllocations = await _mediator.Send<GetLeaveAllocationListRequest, List<LeaveAllocationDto>>(new GetLeaveAllocationListRequest() { IsLoggedInUser = isLoggedInUser });
return Ok(leaveAllocations);
}

// GET api/<LeaveAllocationsController>/5
[HttpGet("{id}")]
public async Task<ActionResult<LeaveAllocationDto>> Get(int id)
{
var leaveAllocation = await _mediator.Send(new GetLeaveAllocationDetailRequest { Id = id });
var leaveAllocation = await _mediator.Send<GetLeaveAllocationDetailRequest, LeaveAllocationDto>(new GetLeaveAllocationDetailRequest { Id = id });
return Ok(leaveAllocation);
}

Expand All @@ -47,7 +48,7 @@ public async Task<ActionResult<LeaveAllocationDto>> Get(int id)
public async Task<ActionResult<BaseCommandResponse>> Post([FromBody] CreateLeaveAllocationDto leaveAllocation)
{
var command = new CreateLeaveAllocationCommand { LeaveAllocationDto = leaveAllocation };
var repsonse = await _mediator.Send(command);
var repsonse = await _mediator.Send<CreateLeaveAllocationCommand, BaseCommandResponse>(command);
return Ok(repsonse);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using RCommon.Mediator;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -19,9 +20,9 @@ namespace HR.LeaveManagement.Api.Controllers
[Authorize]
public class LeaveRequestsController : ControllerBase
{
private readonly IMediator _mediator;
private readonly IMediatorService _mediator;

public LeaveRequestsController(IMediator mediator)
public LeaveRequestsController(IMediatorService mediator)
{
_mediator = mediator;
}
Expand All @@ -30,15 +31,15 @@ public LeaveRequestsController(IMediator mediator)
[HttpGet]
public async Task<ActionResult<List<LeaveRequestListDto>>> Get(bool isLoggedInUser = false)
{
var leaveRequests = await _mediator.Send(new GetLeaveRequestListRequest() { IsLoggedInUser = isLoggedInUser });
var leaveRequests = await _mediator.Send< GetLeaveRequestListRequest, List<LeaveRequestListDto>>(new GetLeaveRequestListRequest() { IsLoggedInUser = isLoggedInUser });
return Ok(leaveRequests);
}

// GET api/<LeaveRequestsController>/5
[HttpGet("{id}")]
public async Task<ActionResult<LeaveRequestDto>> Get(int id)
{
var leaveRequest = await _mediator.Send(new GetLeaveRequestDetailRequest { Id = id });
var leaveRequest = await _mediator.Send< GetLeaveRequestDetailRequest, LeaveRequestDto>(new GetLeaveRequestDetailRequest { Id = id });
return Ok(leaveRequest);
}

Expand All @@ -47,7 +48,7 @@ public async Task<ActionResult<LeaveRequestDto>> Get(int id)
public async Task<ActionResult<BaseCommandResponse>> Post([FromBody] CreateLeaveRequestDto leaveRequest)
{
var command = new CreateLeaveRequestCommand { LeaveRequestDto = leaveRequest };
var repsonse = await _mediator.Send(command);
var repsonse = await _mediator.Send< CreateLeaveRequestCommand, BaseCommandResponse>(command);
return Ok(repsonse);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using RCommon.Mediator;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -20,10 +21,10 @@ namespace HR.LeaveManagement.Api.Controllers
[Authorize]
public class LeaveTypesController : ControllerBase
{
private readonly IMediator _mediator;
private readonly IMediatorService _mediator;
private readonly IHttpContextAccessor _httpContextAccessor;

public LeaveTypesController(IMediator mediator, IHttpContextAccessor httpContextAccessor)
public LeaveTypesController(IMediatorService mediator, IHttpContextAccessor httpContextAccessor)
{
_mediator = mediator;
this._httpContextAccessor = httpContextAccessor;
Expand All @@ -33,15 +34,15 @@ public LeaveTypesController(IMediator mediator, IHttpContextAccessor httpContext
[HttpGet]
public async Task<ActionResult<List<LeaveTypeDto>>> Get()
{
var leaveTypes = await _mediator.Send(new GetLeaveTypeListRequest());
var leaveTypes = await _mediator.Send< GetLeaveTypeListRequest, List<LeaveTypeDto>>(new GetLeaveTypeListRequest());
return Ok(leaveTypes);
}

// GET api/<LeaveTypesController>/5
[HttpGet("{id}")]
public async Task<ActionResult<LeaveTypeDto>> Get(int id)
{
var leaveType = await _mediator.Send(new GetLeaveTypeDetailRequest { Id = id });
var leaveType = await _mediator.Send<GetLeaveTypeDetailRequest, LeaveTypeDto>(new GetLeaveTypeDetailRequest { Id = id });
return Ok(leaveType);
}

Expand All @@ -54,7 +55,7 @@ public async Task<ActionResult<BaseCommandResponse>> Post([FromBody] CreateLeave
{
var user = _httpContextAccessor.HttpContext.User;
var command = new CreateLeaveTypeCommand { LeaveTypeDto = leaveType };
var response = await _mediator.Send(command);
var response = await _mediator.Send<CreateLeaveTypeCommand, BaseCommandResponse>(command);
return Ok(response);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MediatR" Version="12.2.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
36 changes: 36 additions & 0 deletions Examples/CleanWithCQRS/HR.LeaveManagement.API/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@
using RCommon.Persistence.Transactions;
using RCommon.Mediator.MediatR;
using System.Reflection;
using HR.LeaveManagement.Application.Features.LeaveAllocations.Requests.Commands;
using HR.LeaveManagement.Application.Features.LeaveAllocations.Handlers.Commands;
using HR.LeaveManagement.Application.Responses;
using HR.LeaveManagement.Application.Features.LeaveAllocations.Requests.Queries;
using HR.LeaveManagement.Application.Features.LeaveAllocations.Handlers.Queries;
using HR.LeaveManagement.Application.DTOs.LeaveAllocation;
using HR.LeaveManagement.Application.Features.LeaveRequests.Requests.Commands;
using HR.LeaveManagement.Application.Features.LeaveRequests.Handlers.Commands;
using HR.LeaveManagement.Application.Features.LeaveTypes.Requests.Commands;
using HR.LeaveManagement.Application.Features.LeaveTypes.Handlers.Commands;
using HR.LeaveManagement.Application.Features.LeaveRequests.Requests.Queries;
using HR.LeaveManagement.Application.Features.LeaveRequests.Handlers.Queries;
using HR.LeaveManagement.Application.DTOs.LeaveRequest;
using HR.LeaveManagement.Application.Features.LeaveTypes.Requests.Queries;
using HR.LeaveManagement.Application.Features.LeaveTypes.Handlers.Queries;
using HR.LeaveManagement.Application.DTOs.LeaveType;

var builder = WebApplication.CreateBuilder(args);

Expand All @@ -34,6 +50,26 @@
.WithSequentialGuidGenerator(guid => guid.DefaultSequentialGuidType = SequentialGuidType.SequentialAsString)
.WithMediator<MediatRBuilder>(mediator =>
{
mediator.AddRequest<CreateLeaveAllocationCommand, BaseCommandResponse, CreateLeaveAllocationCommandHandler>();
mediator.AddRequest<DeleteLeaveAllocationCommand, DeleteLeaveAllocationCommandHandler>();
mediator.AddRequest<UpdateLeaveAllocationCommand, UpdateLeaveAllocationCommandHandler>();
mediator.AddRequest<GetLeaveAllocationDetailRequest, LeaveAllocationDto, GetLeaveAllocationDetailRequestHandler>();
mediator.AddRequest<GetLeaveAllocationListRequest, List<LeaveAllocationDto>, GetLeaveAllocationListRequestHandler>();
mediator.AddRequest<CreateLeaveAllocationCommand, BaseCommandResponse, CreateLeaveAllocationCommandHandler>();
mediator.AddRequest<DeleteLeaveAllocationCommand, DeleteLeaveAllocationCommandHandler>();
mediator.AddRequest<UpdateLeaveAllocationCommand, UpdateLeaveAllocationCommandHandler>();
mediator.AddRequest<CreateLeaveRequestCommand, BaseCommandResponse, CreateLeaveRequestCommandHandler>();
mediator.AddRequest<DeleteLeaveRequestCommand, DeleteLeaveRequestCommandHandler>();
mediator.AddRequest<UpdateLeaveRequestCommand, UpdateLeaveRequestCommandHandler>();
mediator.AddRequest<CreateLeaveTypeCommand, BaseCommandResponse, CreateLeaveTypeCommandHandler>();
mediator.AddRequest<GetLeaveRequestDetailRequest, LeaveRequestDto, GetLeaveRequestDetailRequestHandler>();
mediator.AddRequest<GetLeaveRequestListRequest, List<LeaveRequestListDto>, GetLeaveRequestListRequestHandler>();
mediator.AddRequest<CreateLeaveTypeCommand, BaseCommandResponse, CreateLeaveTypeCommandHandler>();
mediator.AddRequest<DeleteLeaveTypeCommand, DeleteLeaveTypeCommandHandler>();
mediator.AddRequest<UpdateLeaveTypeCommand, UpdateLeaveTypeCommandHandler>();
mediator.AddRequest<GetLeaveTypeDetailRequest, LeaveTypeDto, GetLeaveTypeDetailRequestHandler>();
mediator.AddRequest<GetLeaveTypeListRequest, List<LeaveTypeDto>, GetLeaveTypeListRequestHandler>();
mediator.Configure(config =>
{
config.RegisterServicesFromAssemblies((typeof(ApplicationServicesRegistration).GetTypeInfo().Assembly));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public CreateLeaveTypeCommandHandlerTests()
[Test]
public async Task Valid_LeaveType_Added()
{
var result = await _handler.Handle(new CreateLeaveTypeCommand() { LeaveTypeDto = _leaveTypeDto }, CancellationToken.None);
var result = await _handler.HandleAsync(new CreateLeaveTypeCommand() { LeaveTypeDto = _leaveTypeDto }, CancellationToken.None);

result.ShouldBeOfType<BaseCommandResponse>();
}
Expand All @@ -66,7 +66,7 @@ public async Task InValid_LeaveType_Added()
{
_leaveTypeDto.DefaultDays = -1;

var result = await _handler.Handle(new CreateLeaveTypeCommand() { LeaveTypeDto = _leaveTypeDto }, CancellationToken.None);
var result = await _handler.HandleAsync(new CreateLeaveTypeCommand() { LeaveTypeDto = _leaveTypeDto }, CancellationToken.None);

//leaveTypes.Count.ShouldBe(3);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public async Task GetLeaveTypeListTest()
.Returns(() => Task.FromResult(testData as ICollection<LeaveType>));

var handler = new GetLeaveTypeListRequestHandler(mock.Object, _mapper);
var result = await handler.Handle(new GetLeaveTypeListRequest(), CancellationToken.None);
var result = await handler.HandleAsync(new GetLeaveTypeListRequest(), CancellationToken.None);

result.ShouldBeOfType<List<LeaveTypeDto>>();
result.Count.ShouldBe(5);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using FluentAssertions;
using MediatR;
using NUnit.Framework;
using RCommon.Mediator.Subscribers;
using RCommon.TestBase;
using Shouldly;
using System;
Expand Down Expand Up @@ -35,12 +35,12 @@ private static void ShouldContainHandlerForRequest(IEnumerable<Type> handlerType

private static bool IsRequest(Type type)
{
return typeof(IBaseRequest).IsAssignableFrom(type);
return typeof(IAppRequest).IsAssignableFrom(type);
}

private static bool IsIRequestHandler(Type type)
{
return type.GetInterfaces().Any(interfaceType => interfaceType.IsGenericType && interfaceType.GetGenericTypeDefinition() == typeof(IRequestHandler<,>));
return type.GetInterfaces().Any(interfaceType => interfaceType.IsGenericType && interfaceType.GetGenericTypeDefinition() == typeof(IAppRequestHandler<,>));
}

private static bool IsHandlerForRequest(Type handlerType, Type requestType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using HR.LeaveManagement.Application.Features.LeaveAllocations.Requests.Commands;
using HR.LeaveManagement.Application.Features.LeaveTypes.Requests.Commands;
using HR.LeaveManagement.Domain;
using MediatR;
using RCommon.Mediator.Subscribers;
using System;
using System.Collections.Generic;
using System.Text;
Expand All @@ -19,7 +19,7 @@

namespace HR.LeaveManagement.Application.Features.LeaveAllocations.Handlers.Commands
{
public class CreateLeaveAllocationCommandHandler : IRequestHandler<CreateLeaveAllocationCommand, BaseCommandResponse>
public class CreateLeaveAllocationCommandHandler : IAppRequestHandler<CreateLeaveAllocationCommand, BaseCommandResponse>
{
private readonly IGraphRepository<LeaveType> _leaveTypeRepository;
private readonly IGraphRepository<LeaveAllocation> _leaveAllocationRepository;
Expand All @@ -39,7 +39,7 @@ public CreateLeaveAllocationCommandHandler(IGraphRepository<LeaveType> leaveType
_mapper = mapper;
}

public async Task<BaseCommandResponse> Handle(CreateLeaveAllocationCommand request, CancellationToken cancellationToken)
public async Task<BaseCommandResponse> HandleAsync(CreateLeaveAllocationCommand request, CancellationToken cancellationToken)
{
var response = new BaseCommandResponse();
var validator = new CreateLeaveAllocationDtoValidator(_leaveTypeRepository);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using HR.LeaveManagement.Application.Features.LeaveAllocations.Requests.Commands;
using HR.LeaveManagement.Application.Features.LeaveTypes.Requests.Commands;
using HR.LeaveManagement.Domain;
using MediatR;
using RCommon.Mediator.Subscribers;
using System;
using System.Collections.Generic;
using System.Text;
Expand All @@ -14,7 +14,7 @@

namespace HR.LeaveManagement.Application.Features.LeaveAllocations.Handlers.Commands
{
public class DeleteLeaveAllocationCommandHandler : IRequestHandler<DeleteLeaveAllocationCommand>
public class DeleteLeaveAllocationCommandHandler : IAppRequestHandler<DeleteLeaveAllocationCommand>
{
private readonly IGraphRepository<LeaveAllocation> _leaveAllocationRepository;
private readonly IMapper _mapper;
Expand All @@ -26,7 +26,7 @@ public DeleteLeaveAllocationCommandHandler(IGraphRepository<LeaveAllocation> lea
_mapper = mapper;
}

public async Task Handle(DeleteLeaveAllocationCommand request, CancellationToken cancellationToken)
public async Task HandleAsync(DeleteLeaveAllocationCommand request, CancellationToken cancellationToken)
{
var leaveAllocation = await _leaveAllocationRepository.FindAsync(request.Id);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using HR.LeaveManagement.Application.Features.LeaveAllocations.Requests.Commands;
using HR.LeaveManagement.Application.Features.LeaveTypes.Requests.Commands;
using HR.LeaveManagement.Domain;
using MediatR;
using RCommon.Mediator.Subscribers;
using System;
using System.Collections.Generic;
using System.Text;
Expand All @@ -15,7 +15,7 @@

namespace HR.LeaveManagement.Application.Features.LeaveAllocations.Handlers.Commands
{
public class UpdateLeaveAllocationCommandHandler : IRequestHandler<UpdateLeaveAllocationCommand, Unit>
public class UpdateLeaveAllocationCommandHandler : IAppRequestHandler<UpdateLeaveAllocationCommand>
{
private readonly IGraphRepository<LeaveAllocation> _leaveAllocationRepository;
private readonly IReadOnlyRepository<LeaveType> _leaveTypeRepository;
Expand All @@ -32,7 +32,7 @@ public UpdateLeaveAllocationCommandHandler(IGraphRepository<LeaveAllocation> lea
_mapper = mapper;
}

public async Task<Unit> Handle(UpdateLeaveAllocationCommand request, CancellationToken cancellationToken)
public async Task HandleAsync(UpdateLeaveAllocationCommand request, CancellationToken cancellationToken)
{
var validator = new UpdateLeaveAllocationDtoValidator(this._leaveTypeRepository);
var validationResult = await validator.ValidateAsync(request.LeaveAllocationDto);
Expand All @@ -48,7 +48,7 @@ public async Task<Unit> Handle(UpdateLeaveAllocationCommand request, Cancellatio
_mapper.Map(request.LeaveAllocationDto, leaveAllocation);

await _leaveAllocationRepository.UpdateAsync(leaveAllocation);
return Unit.Value;

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
using HR.LeaveManagement.Application.DTOs.LeaveAllocation;
using HR.LeaveManagement.Application.Features.LeaveAllocations.Requests.Queries;
using HR.LeaveManagement.Domain;
using MediatR;
using RCommon.Mediator.Subscribers;
using RCommon.Persistence;
using RCommon.Persistence.Crud;
using System.Threading;
using System.Threading.Tasks;

namespace HR.LeaveManagement.Application.Features.LeaveAllocations.Handlers.Queries
{
public class GetLeaveAllocationDetailRequestHandler : IRequestHandler<GetLeaveAllocationDetailRequest, LeaveAllocationDto>
public class GetLeaveAllocationDetailRequestHandler : IAppRequestHandler<GetLeaveAllocationDetailRequest, LeaveAllocationDto>
{
private readonly IGraphRepository<LeaveAllocation> _leaveAllocationRepository;
private readonly IMapper _mapper;
Expand All @@ -22,7 +22,7 @@ public GetLeaveAllocationDetailRequestHandler(IGraphRepository<LeaveAllocation>
this._leaveAllocationRepository.DataStoreName = DataStoreNamesConst.LeaveManagement;
_mapper = mapper;
}
public async Task<LeaveAllocationDto> Handle(GetLeaveAllocationDetailRequest request, CancellationToken cancellationToken)
public async Task<LeaveAllocationDto> HandleAsync(GetLeaveAllocationDetailRequest request, CancellationToken cancellationToken)
{
_leaveAllocationRepository.Include(x => x.LeaveType);
var leaveAllocation = await _leaveAllocationRepository.FindAsync(request.Id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using HR.LeaveManagement.Application.DTOs;
using HR.LeaveManagement.Application.DTOs.LeaveAllocation;
using HR.LeaveManagement.Application.Features.LeaveAllocations.Requests.Queries;
using MediatR;
using RCommon.Mediator.Subscribers;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -16,7 +16,7 @@

namespace HR.LeaveManagement.Application.Features.LeaveAllocations.Handlers.Queries
{
public class GetLeaveAllocationListRequestHandler : IRequestHandler<GetLeaveAllocationListRequest, List<LeaveAllocationDto>>
public class GetLeaveAllocationListRequestHandler : IAppRequestHandler<GetLeaveAllocationListRequest, List<LeaveAllocationDto>>
{
private readonly IGraphRepository<LeaveAllocation> _leaveAllocationRepository;
private readonly IMapper _mapper;
Expand All @@ -35,7 +35,7 @@ public GetLeaveAllocationListRequestHandler(IGraphRepository<LeaveAllocation> le
this._userService = userService;
}

public async Task<List<LeaveAllocationDto>> Handle(GetLeaveAllocationListRequest request, CancellationToken cancellationToken)
public async Task<List<LeaveAllocationDto>> HandleAsync(GetLeaveAllocationListRequest request, CancellationToken cancellationToken)
{
var leaveAllocations = new List<LeaveAllocation>();
var allocations = new List<LeaveAllocationDto>();
Expand Down
Loading

0 comments on commit 5273fd0

Please sign in to comment.