diff --git a/Examples/CleanWithCQRS/HR.LeaveManagement.Application.UnitTests/HR.LeaveManagement.Application.UnitTests.csproj b/Examples/CleanWithCQRS/HR.LeaveManagement.Application.UnitTests/HR.LeaveManagement.Application.UnitTests.csproj
index cd52e612..835784b7 100644
--- a/Examples/CleanWithCQRS/HR.LeaveManagement.Application.UnitTests/HR.LeaveManagement.Application.UnitTests.csproj
+++ b/Examples/CleanWithCQRS/HR.LeaveManagement.Application.UnitTests/HR.LeaveManagement.Application.UnitTests.csproj
@@ -8,7 +8,6 @@
-
diff --git a/Examples/CleanWithCQRS/HR.LeaveManagement.Application.UnitTests/LeaveTypes/Commands/CreateLeaveTypeCommandHandlerTests.cs b/Examples/CleanWithCQRS/HR.LeaveManagement.Application.UnitTests/LeaveTypes/Commands/CreateLeaveTypeCommandHandlerTests.cs
index f5d8d737..74ed2300 100644
--- a/Examples/CleanWithCQRS/HR.LeaveManagement.Application.UnitTests/LeaveTypes/Commands/CreateLeaveTypeCommandHandlerTests.cs
+++ b/Examples/CleanWithCQRS/HR.LeaveManagement.Application.UnitTests/LeaveTypes/Commands/CreateLeaveTypeCommandHandlerTests.cs
@@ -1,11 +1,9 @@
-using AutoMapper;
using HR.LeaveManagement.Application.DTOs.LeaveType;
using HR.LeaveManagement.Application.Exceptions;
using HR.LeaveManagement.Application.Features.LeaveTypes.Handlers.Commands;
using HR.LeaveManagement.Application.Features.LeaveTypes.Handlers.Queries;
using HR.LeaveManagement.Application.Features.LeaveTypes.Requests.Commands;
using HR.LeaveManagement.Application.Features.LeaveTypes.Requests.Queries;
-using HR.LeaveManagement.Application.Profiles;
using HR.LeaveManagement.Application.Responses;
using HR.LeaveManagement.Domain;
using Moq;
@@ -26,21 +24,11 @@ namespace HR.LeaveManagement.Application.UnitTests.LeaveTypes.Commands
[TestFixture()]
public class CreateLeaveTypeCommandHandlerTests
{
- private readonly IMapper _mapper;
-
private readonly CreateLeaveTypeDto _leaveTypeDto;
private readonly CreateLeaveTypeCommandHandler _handler;
public CreateLeaveTypeCommandHandlerTests()
{
-
- var mapperConfig = new MapperConfiguration(c =>
- {
- c.AddProfile();
- }, null);
-
- _mapper = mapperConfig.CreateMapper();
-
var testData = new List();
var mock = new Mock>();
var validationMock = new Mock();
@@ -56,7 +44,7 @@ public CreateLeaveTypeCommandHandlerTests()
validationMock.Setup(x => x.ValidateAsync(_leaveTypeDto, false, CancellationToken.None))
.Returns(() => Task.FromResult(new ValidationOutcome()));
- _handler = new CreateLeaveTypeCommandHandler(_mapper, mock.Object, validationMock.Object);
+ _handler = new CreateLeaveTypeCommandHandler(mock.Object, validationMock.Object);
}
[Test]
@@ -77,7 +65,7 @@ public async Task InValid_LeaveType_Added()
//leaveTypes.Count.ShouldBe(3);
result.ShouldBeOfType();
-
+
}
}
}
diff --git a/Examples/CleanWithCQRS/HR.LeaveManagement.Application.UnitTests/LeaveTypes/Queries/GetLeaveTypeListRequestHandlerTests.cs b/Examples/CleanWithCQRS/HR.LeaveManagement.Application.UnitTests/LeaveTypes/Queries/GetLeaveTypeListRequestHandlerTests.cs
index e34e34b7..35965ad4 100644
--- a/Examples/CleanWithCQRS/HR.LeaveManagement.Application.UnitTests/LeaveTypes/Queries/GetLeaveTypeListRequestHandlerTests.cs
+++ b/Examples/CleanWithCQRS/HR.LeaveManagement.Application.UnitTests/LeaveTypes/Queries/GetLeaveTypeListRequestHandlerTests.cs
@@ -1,8 +1,6 @@
-using AutoMapper;
using HR.LeaveManagement.Application.DTOs.LeaveType;
using HR.LeaveManagement.Application.Features.LeaveTypes.Handlers.Queries;
using HR.LeaveManagement.Application.Features.LeaveTypes.Requests.Queries;
-using HR.LeaveManagement.Application.Profiles;
using HR.LeaveManagement.Domain;
using Moq;
using NUnit.Framework;
@@ -22,19 +20,6 @@ namespace HR.LeaveManagement.Application.UnitTests.LeaveTypes.Queries
[TestFixture()]
public class GetLeaveTypeListRequestHandlerTests
{
- private readonly IMapper _mapper;
- public GetLeaveTypeListRequestHandlerTests()
- {
- //_mockRepo = MockLeaveTypeRepository.GetLeaveTypeRepository();
-
- var mapperConfig = new MapperConfiguration(c =>
- {
- c.AddProfile();
- }, null);
-
- _mapper = mapperConfig.CreateMapper();
- }
-
[Test]
public async Task GetLeaveTypeListTest()
{
@@ -46,8 +31,8 @@ public async Task GetLeaveTypeListTest()
var mock = new Mock>();
mock.Setup(x => x.FindAsync(x=>true, CancellationToken.None))
.Returns(() => Task.FromResult(testData as ICollection));
-
- var handler = new GetLeaveTypeListRequestHandler(mock.Object, _mapper);
+
+ var handler = new GetLeaveTypeListRequestHandler(mock.Object);
var result = await handler.HandleAsync(new GetLeaveTypeListRequest(), CancellationToken.None);
result.ShouldBeOfType>();
diff --git a/Examples/CleanWithCQRS/HR.LeaveManagement.Application/ApplicationServicesRegistration.cs b/Examples/CleanWithCQRS/HR.LeaveManagement.Application/ApplicationServicesRegistration.cs
index 61825b54..d5b8ad88 100644
--- a/Examples/CleanWithCQRS/HR.LeaveManagement.Application/ApplicationServicesRegistration.cs
+++ b/Examples/CleanWithCQRS/HR.LeaveManagement.Application/ApplicationServicesRegistration.cs
@@ -1,4 +1,3 @@
-using HR.LeaveManagement.Application.Profiles;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
@@ -11,8 +10,6 @@ public static class ApplicationServicesRegistration
{
public static IServiceCollection ConfigureApplicationServices(this IServiceCollection services)
{
- services.AddAutoMapper(x => x.AddProfile(new MappingProfile()));
-
return services;
}
}
diff --git a/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Features/LeaveAllocations/Handlers/Commands/CreateLeaveAllocationCommandHandler.cs b/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Features/LeaveAllocations/Handlers/Commands/CreateLeaveAllocationCommandHandler.cs
index 780a6755..9b5ae37b 100644
--- a/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Features/LeaveAllocations/Handlers/Commands/CreateLeaveAllocationCommandHandler.cs
+++ b/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Features/LeaveAllocations/Handlers/Commands/CreateLeaveAllocationCommandHandler.cs
@@ -1,4 +1,3 @@
-using AutoMapper;
using HR.LeaveManagement.Application.DTOs.LeaveAllocation.Validators;
using HR.LeaveManagement.Application.Exceptions;
using HR.LeaveManagement.Application.Features.LeaveAllocations.Requests.Commands;
@@ -25,13 +24,11 @@ public class CreateLeaveAllocationCommandHandler : IAppRequestHandler _leaveTypeRepository;
private readonly IGraphRepository _leaveAllocationRepository;
private readonly IUserService _userService;
- private readonly IMapper _mapper;
private readonly IValidationService _validationService;
public CreateLeaveAllocationCommandHandler(IGraphRepository leaveTypeRepository,
IGraphRepository leaveAllocationRepository,
IUserService userService,
- IMapper mapper,
IValidationService validationService)
{
this._leaveTypeRepository = leaveTypeRepository;
@@ -39,7 +36,6 @@ public CreateLeaveAllocationCommandHandler(IGraphRepository leaveType
this._leaveAllocationRepository.DataStoreName = DataStoreNamesConst.LeaveManagement;
this._leaveTypeRepository.DataStoreName = DataStoreNamesConst.LeaveManagement;
this._userService = userService;
- _mapper = mapper;
_validationService = validationService;
}
@@ -77,12 +73,11 @@ public async Task HandleAsync(CreateLeaveAllocationCommand
{
await _leaveAllocationRepository.AddAsync(item);
}
-
+
response.Success = true;
response.Message = "Allocations Successful";
}
-
return response;
}
}
diff --git a/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Features/LeaveAllocations/Handlers/Commands/DeleteLeaveAllocationCommandHandler.cs b/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Features/LeaveAllocations/Handlers/Commands/DeleteLeaveAllocationCommandHandler.cs
index 704c1158..ee36dac8 100644
--- a/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Features/LeaveAllocations/Handlers/Commands/DeleteLeaveAllocationCommandHandler.cs
+++ b/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Features/LeaveAllocations/Handlers/Commands/DeleteLeaveAllocationCommandHandler.cs
@@ -1,4 +1,3 @@
-using AutoMapper;
using HR.LeaveManagement.Application.Exceptions;
using HR.LeaveManagement.Application.Features.LeaveAllocations.Requests.Commands;
using HR.LeaveManagement.Application.Features.LeaveTypes.Requests.Commands;
@@ -17,13 +16,11 @@ namespace HR.LeaveManagement.Application.Features.LeaveAllocations.Handlers.Comm
public class DeleteLeaveAllocationCommandHandler : IAppRequestHandler
{
private readonly IGraphRepository _leaveAllocationRepository;
- private readonly IMapper _mapper;
- public DeleteLeaveAllocationCommandHandler(IGraphRepository leaveAllocationRepository, IMapper mapper)
+ public DeleteLeaveAllocationCommandHandler(IGraphRepository leaveAllocationRepository)
{
this._leaveAllocationRepository = leaveAllocationRepository;
this._leaveAllocationRepository.DataStoreName = DataStoreNamesConst.LeaveManagement;
- _mapper = mapper;
}
public async Task HandleAsync(DeleteLeaveAllocationCommand request, CancellationToken cancellationToken)
diff --git a/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Features/LeaveAllocations/Handlers/Commands/UpdateLeaveAllocationCommandHandler.cs b/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Features/LeaveAllocations/Handlers/Commands/UpdateLeaveAllocationCommandHandler.cs
index f67537e3..9f1860d0 100644
--- a/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Features/LeaveAllocations/Handlers/Commands/UpdateLeaveAllocationCommandHandler.cs
+++ b/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Features/LeaveAllocations/Handlers/Commands/UpdateLeaveAllocationCommandHandler.cs
@@ -1,8 +1,8 @@
-using AutoMapper;
using HR.LeaveManagement.Application.DTOs.LeaveAllocation.Validators;
using HR.LeaveManagement.Application.Exceptions;
using HR.LeaveManagement.Application.Features.LeaveAllocations.Requests.Commands;
using HR.LeaveManagement.Application.Features.LeaveTypes.Requests.Commands;
+using HR.LeaveManagement.Application.Mappings;
using HR.LeaveManagement.Domain;
using RCommon.Mediator.Subscribers;
using System;
@@ -20,19 +20,16 @@ public class UpdateLeaveAllocationCommandHandler : IAppRequestHandler _leaveAllocationRepository;
private readonly IReadOnlyRepository _leaveTypeRepository;
- private readonly IMapper _mapper;
private readonly IValidationService _validationService;
public UpdateLeaveAllocationCommandHandler(IGraphRepository leaveAllocationRepository,
IReadOnlyRepository leaveTypeRepository,
- IMapper mapper,
IValidationService validationService)
{
this._leaveAllocationRepository = leaveAllocationRepository;
this._leaveTypeRepository = leaveTypeRepository;
this._leaveAllocationRepository.DataStoreName = DataStoreNamesConst.LeaveManagement;
this._leaveTypeRepository.DataStoreName = DataStoreNamesConst.LeaveManagement;
- _mapper = mapper;
_validationService = validationService;
}
@@ -48,10 +45,9 @@ public async Task HandleAsync(UpdateLeaveAllocationCommand request, Cancellation
if (leaveAllocation is null)
throw new NotFoundException(nameof(leaveAllocation), request.LeaveAllocationDto.Id);
- _mapper.Map(request.LeaveAllocationDto, leaveAllocation);
+ request.LeaveAllocationDto.ApplyTo(leaveAllocation);
await _leaveAllocationRepository.UpdateAsync(leaveAllocation);
-
}
}
}
diff --git a/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Features/LeaveAllocations/Handlers/Queries/GetLeaveAllocationDetailRequestHandler.cs b/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Features/LeaveAllocations/Handlers/Queries/GetLeaveAllocationDetailRequestHandler.cs
index eb09fbe4..524cc9f6 100644
--- a/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Features/LeaveAllocations/Handlers/Queries/GetLeaveAllocationDetailRequestHandler.cs
+++ b/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Features/LeaveAllocations/Handlers/Queries/GetLeaveAllocationDetailRequestHandler.cs
@@ -1,7 +1,7 @@
-using AutoMapper;
using HR.LeaveManagement.Application.DTOs;
using HR.LeaveManagement.Application.DTOs.LeaveAllocation;
using HR.LeaveManagement.Application.Features.LeaveAllocations.Requests.Queries;
+using HR.LeaveManagement.Application.Mappings;
using HR.LeaveManagement.Domain;
using RCommon.Mediator.Subscribers;
using RCommon.Persistence;
@@ -14,19 +14,18 @@ namespace HR.LeaveManagement.Application.Features.LeaveAllocations.Handlers.Quer
public class GetLeaveAllocationDetailRequestHandler : IAppRequestHandler
{
private readonly IGraphRepository _leaveAllocationRepository;
- private readonly IMapper _mapper;
- public GetLeaveAllocationDetailRequestHandler(IGraphRepository leaveAllocationRepository, IMapper mapper)
+ public GetLeaveAllocationDetailRequestHandler(IGraphRepository leaveAllocationRepository)
{
_leaveAllocationRepository = leaveAllocationRepository;
this._leaveAllocationRepository.DataStoreName = DataStoreNamesConst.LeaveManagement;
- _mapper = mapper;
}
+
public async Task HandleAsync(GetLeaveAllocationDetailRequest request, CancellationToken cancellationToken)
{
_leaveAllocationRepository.Include(x => x.LeaveType);
var leaveAllocation = await _leaveAllocationRepository.FindAsync(request.Id);
- return _mapper.Map(leaveAllocation);
+ return leaveAllocation.ToLeaveAllocationDto();
}
}
}
diff --git a/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Features/LeaveAllocations/Handlers/Queries/GetLeaveAllocationListRequestHandler.cs b/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Features/LeaveAllocations/Handlers/Queries/GetLeaveAllocationListRequestHandler.cs
index 2622e4ac..51444c31 100644
--- a/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Features/LeaveAllocations/Handlers/Queries/GetLeaveAllocationListRequestHandler.cs
+++ b/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Features/LeaveAllocations/Handlers/Queries/GetLeaveAllocationListRequestHandler.cs
@@ -1,9 +1,10 @@
-using AutoMapper;
using HR.LeaveManagement.Application.DTOs;
using HR.LeaveManagement.Application.DTOs.LeaveAllocation;
using HR.LeaveManagement.Application.Features.LeaveAllocations.Requests.Queries;
+using HR.LeaveManagement.Application.Mappings;
using RCommon.Mediator.Subscribers;
using System.Collections.Generic;
+using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using HR.LeaveManagement.Application.Contracts.Identity;
@@ -19,18 +20,15 @@ namespace HR.LeaveManagement.Application.Features.LeaveAllocations.Handlers.Quer
public class GetLeaveAllocationListRequestHandler : IAppRequestHandler>
{
private readonly IGraphRepository _leaveAllocationRepository;
- private readonly IMapper _mapper;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly IUserService _userService;
public GetLeaveAllocationListRequestHandler(IGraphRepository leaveAllocationRepository,
- IMapper mapper,
IHttpContextAccessor httpContextAccessor,
IUserService userService)
{
_leaveAllocationRepository = leaveAllocationRepository;
this._leaveAllocationRepository.DataStoreName = DataStoreNamesConst.LeaveManagement;
- _mapper = mapper;
this._httpContextAccessor = httpContextAccessor;
this._userService = userService;
}
@@ -44,10 +42,10 @@ public async Task> HandleAsync(GetLeaveAllocationListRe
{
var userId = _httpContextAccessor.HttpContext.User.FindFirst(
q => q.Type == CustomClaimTypes.Uid)?.Value;
- leaveAllocations = await _leaveAllocationRepository.FindAsync(x=>x.EmployeeId == userId) as List;
+ leaveAllocations = await _leaveAllocationRepository.FindAsync(x => x.EmployeeId == userId) as List;
var employee = await _userService.GetEmployee(userId);
- allocations = _mapper.Map>(leaveAllocations);
+ allocations = leaveAllocations.Select(x => x.ToLeaveAllocationDto()).ToList();
foreach (var alloc in allocations)
{
alloc.Employee = employee;
@@ -55,8 +53,8 @@ public async Task> HandleAsync(GetLeaveAllocationListRe
}
else
{
- leaveAllocations = await _leaveAllocationRepository.FindAsync(x=>true) as List;
- allocations = _mapper.Map>(leaveAllocations);
+ leaveAllocations = await _leaveAllocationRepository.FindAsync(x => true) as List;
+ allocations = leaveAllocations.Select(x => x.ToLeaveAllocationDto()).ToList();
foreach (var req in allocations)
{
req.Employee = await _userService.GetEmployee(req.EmployeeId);
diff --git a/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Features/LeaveRequests/Handlers/Commands/CreateLeaveRequestCommandHandler.cs b/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Features/LeaveRequests/Handlers/Commands/CreateLeaveRequestCommandHandler.cs
index 4e11cb32..74c6aef2 100644
--- a/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Features/LeaveRequests/Handlers/Commands/CreateLeaveRequestCommandHandler.cs
+++ b/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Features/LeaveRequests/Handlers/Commands/CreateLeaveRequestCommandHandler.cs
@@ -1,8 +1,8 @@
-using AutoMapper;
using HR.LeaveManagement.Application.DTOs.LeaveRequest.Validators;
using HR.LeaveManagement.Application.Exceptions;
using HR.LeaveManagement.Application.Features.LeaveRequests.Requests.Commands;
using HR.LeaveManagement.Application.Features.LeaveTypes.Requests.Commands;
+using HR.LeaveManagement.Application.Mappings;
using HR.LeaveManagement.Application.Responses;
using HR.LeaveManagement.Domain;
using RCommon.Mediator.Subscribers;
@@ -32,7 +32,6 @@ public class CreateLeaveRequestCommandHandler : IAppRequestHandler _emailSettings;
- private readonly IMapper _mapper;
private readonly IValidationService _validationService;
private readonly IReadOnlyRepository _leaveTypeRepository;
private readonly IGraphRepository _leaveAllocationRepository;
@@ -45,7 +44,6 @@ public CreateLeaveRequestCommandHandler(
IEmailService emailSender,
ICurrentUser currentUser,
IOptions emailSettings,
- IMapper mapper,
IValidationService validationService)
{
_leaveTypeRepository = leaveTypeRepository;
@@ -56,8 +54,7 @@ public CreateLeaveRequestCommandHandler(
this._leaveRequestRepository.DataStoreName = DataStoreNamesConst.LeaveManagement;
_emailSender = emailSender;
this._currentUser = currentUser;
- _emailSettings=emailSettings;
- _mapper = mapper;
+ _emailSettings = emailSettings;
_validationService = validationService;
}
@@ -67,8 +64,8 @@ public async Task HandleAsync(CreateLeaveRequestCommand req
var validationResult = await _validationService.ValidateAsync(request.LeaveRequestDto);
var userId = _currentUser.FindClaimValue(CustomClaimTypes.Uid);
- var allocation = _leaveAllocationRepository.FirstOrDefault(x=>x.EmployeeId == userId && x.LeaveTypeId == request.LeaveRequestDto.LeaveTypeId);
- if(allocation is null)
+ var allocation = _leaveAllocationRepository.FirstOrDefault(x => x.EmployeeId == userId && x.LeaveTypeId == request.LeaveRequestDto.LeaveTypeId);
+ if (allocation is null)
{
validationResult.Errors.Add(new ValidationFault(nameof(request.LeaveRequestDto.LeaveTypeId),
"You do not have any allocations for this leave type."));
@@ -82,7 +79,7 @@ public async Task HandleAsync(CreateLeaveRequestCommand req
nameof(request.LeaveRequestDto.EndDate), "You do not have enough days for this request"));
}
}
-
+
if (validationResult.IsValid == false)
{
response.Success = false;
@@ -91,7 +88,7 @@ public async Task HandleAsync(CreateLeaveRequestCommand req
}
else
{
- var leaveRequest = _mapper.Map(request.LeaveRequestDto);
+ var leaveRequest = request.LeaveRequestDto.ToLeaveRequest();
leaveRequest.RequestingEmployeeId = userId;
await _leaveRequestRepository.AddAsync(leaveRequest);
//TODO: May need to get Id out
@@ -104,7 +101,7 @@ public async Task HandleAsync(CreateLeaveRequestCommand req
{
var emailAddress = _currentUser.FindClaimValue(ClaimTypes.Email);
- var email = new MailMessage(new MailAddress(this._emailSettings.Value.FromEmailDefault, this._emailSettings.Value.FromNameDefault),
+ var email = new MailMessage(new MailAddress(this._emailSettings.Value.FromEmailDefault, this._emailSettings.Value.FromNameDefault),
new MailAddress(emailAddress))
{
Body = $"Your leave request for {request.LeaveRequestDto.StartDate:D} to {request.LeaveRequestDto.EndDate:D} " +
@@ -119,7 +116,7 @@ public async Task HandleAsync(CreateLeaveRequestCommand req
//// Log or handle error, but don't throw...
}
}
-
+
return response;
}
}
diff --git a/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Features/LeaveRequests/Handlers/Commands/DeleteLeaveRequestCommandHandler.cs b/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Features/LeaveRequests/Handlers/Commands/DeleteLeaveRequestCommandHandler.cs
index 33f6d3a4..ff11797c 100644
--- a/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Features/LeaveRequests/Handlers/Commands/DeleteLeaveRequestCommandHandler.cs
+++ b/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Features/LeaveRequests/Handlers/Commands/DeleteLeaveRequestCommandHandler.cs
@@ -1,4 +1,3 @@
-using AutoMapper;
using HR.LeaveManagement.Application.Exceptions;
using HR.LeaveManagement.Application.Features.LeaveRequests.Requests.Commands;
using HR.LeaveManagement.Application.Features.LeaveTypes.Requests.Commands;
diff --git a/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Features/LeaveRequests/Handlers/Commands/UpdateLeaveRequestCommandHandler.cs b/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Features/LeaveRequests/Handlers/Commands/UpdateLeaveRequestCommandHandler.cs
index 93f6af47..06b2e975 100644
--- a/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Features/LeaveRequests/Handlers/Commands/UpdateLeaveRequestCommandHandler.cs
+++ b/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Features/LeaveRequests/Handlers/Commands/UpdateLeaveRequestCommandHandler.cs
@@ -1,9 +1,9 @@
-using AutoMapper;
using HR.LeaveManagement.Application.DTOs.LeaveRequest.Validators;
using HR.LeaveManagement.Application.Exceptions;
using HR.LeaveManagement.Application.Features.LeaveAllocations.Requests.Commands;
using HR.LeaveManagement.Application.Features.LeaveRequests.Requests.Commands;
using HR.LeaveManagement.Application.Features.LeaveTypes.Requests.Commands;
+using HR.LeaveManagement.Application.Mappings;
using HR.LeaveManagement.Domain;
using RCommon.Mediator.Subscribers;
using System;
@@ -22,15 +22,13 @@ public class UpdateLeaveRequestCommandHandler : IAppRequestHandler _leaveRequestRepository;
private readonly IReadOnlyRepository _leaveTypeRepository;
private readonly IGraphRepository _leaveAllocationRepository;
- private readonly IMapper _mapper;
private readonly IValidationService _validationService;
public UpdateLeaveRequestCommandHandler(
IGraphRepository leaveRequestRepository,
IReadOnlyRepository leaveTypeRepository,
IGraphRepository leaveAllocationRepository,
- IMapper mapper,
- IValidationService validationService)
+ IValidationService validationService)
{
this._leaveRequestRepository = leaveRequestRepository;
_leaveTypeRepository = leaveTypeRepository;
@@ -38,7 +36,6 @@ public UpdateLeaveRequestCommandHandler(
this._leaveAllocationRepository.DataStoreName = DataStoreNamesConst.LeaveManagement;
this._leaveTypeRepository.DataStoreName = DataStoreNamesConst.LeaveManagement;
this._leaveRequestRepository.DataStoreName = DataStoreNamesConst.LeaveManagement;
- _mapper = mapper;
_validationService = validationService;
}
@@ -46,7 +43,7 @@ public async Task HandleAsync(UpdateLeaveRequestCommand request, CancellationTok
{
var leaveRequest = await _leaveRequestRepository.FindAsync(request.Id);
- if(leaveRequest is null)
+ if (leaveRequest is null)
throw new NotFoundException(nameof(leaveRequest), request.Id);
if (request.LeaveRequestDto != null)
@@ -55,11 +52,11 @@ public async Task HandleAsync(UpdateLeaveRequestCommand request, CancellationTok
if (validationResult.IsValid == false)
throw new ValidationException(validationResult.Errors);
- _mapper.Map(request.LeaveRequestDto, leaveRequest);
+ request.LeaveRequestDto.ApplyTo(leaveRequest);
await _leaveRequestRepository.UpdateAsync(leaveRequest);
}
- else if(request.ChangeLeaveRequestApprovalDto != null)
+ else if (request.ChangeLeaveRequestApprovalDto != null)
{
leaveRequest.Approved = request.ChangeLeaveRequestApprovalDto.Approved;
await _leaveRequestRepository.UpdateAsync(leaveRequest);
@@ -75,8 +72,6 @@ public async Task HandleAsync(UpdateLeaveRequestCommand request, CancellationTok
await _leaveAllocationRepository.UpdateAsync(allocation);
}
}
-
-
}
}
}
diff --git a/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Features/LeaveRequests/Handlers/Queries/GetLeaveRequestDetailRequestHandler.cs b/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Features/LeaveRequests/Handlers/Queries/GetLeaveRequestDetailRequestHandler.cs
index 7136a0e2..92458bfc 100644
--- a/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Features/LeaveRequests/Handlers/Queries/GetLeaveRequestDetailRequestHandler.cs
+++ b/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Features/LeaveRequests/Handlers/Queries/GetLeaveRequestDetailRequestHandler.cs
@@ -1,9 +1,9 @@
-using AutoMapper;
using HR.LeaveManagement.Application.DTOs;
using HR.LeaveManagement.Application.DTOs.LeaveRequest;
using HR.LeaveManagement.Application.Features.LeaveRequests.Requests.Queries;
using HR.LeaveManagement.Application.Features.LeaveTypes.Requests;
using HR.LeaveManagement.Application.Features.LeaveTypes.Requests.Queries;
+using HR.LeaveManagement.Application.Mappings;
using RCommon.Mediator.Subscribers;
using System;
using System.Collections.Generic;
@@ -20,24 +20,22 @@ namespace HR.LeaveManagement.Application.Features.LeaveRequests.Handlers.Queries
public class GetLeaveRequestDetailRequestHandler : IAppRequestHandler
{
private readonly IGraphRepository _leaveRequestRepository;
- private readonly IMapper _mapper;
private readonly IUserService _userService;
public GetLeaveRequestDetailRequestHandler(IGraphRepository leaveRequestRepository,
- IMapper mapper,
IUserService userService)
{
_leaveRequestRepository = leaveRequestRepository;
this._leaveRequestRepository.DataStoreName = DataStoreNamesConst.LeaveManagement;
- _mapper = mapper;
this._userService = userService;
}
+
public async Task HandleAsync(GetLeaveRequestDetailRequest request, CancellationToken cancellationToken)
{
_leaveRequestRepository.Include(x => x.LeaveType);
- var leaveRequest = _mapper.Map(await _leaveRequestRepository.FindAsync(request.Id));
- leaveRequest.Employee = await _userService.GetEmployee(leaveRequest.RequestingEmployeeId);
- return leaveRequest;
+ var leaveRequestDto = (await _leaveRequestRepository.FindAsync(request.Id)).ToLeaveRequestDto();
+ leaveRequestDto.Employee = await _userService.GetEmployee(leaveRequestDto.RequestingEmployeeId);
+ return leaveRequestDto;
}
}
}
diff --git a/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Features/LeaveRequests/Handlers/Queries/GetLeaveRequestListRequestHandler.cs b/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Features/LeaveRequests/Handlers/Queries/GetLeaveRequestListRequestHandler.cs
index 31c60552..e6f8d667 100644
--- a/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Features/LeaveRequests/Handlers/Queries/GetLeaveRequestListRequestHandler.cs
+++ b/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Features/LeaveRequests/Handlers/Queries/GetLeaveRequestListRequestHandler.cs
@@ -1,12 +1,13 @@
-using AutoMapper;
using HR.LeaveManagement.Application.DTOs;
using HR.LeaveManagement.Application.DTOs.LeaveRequest;
using HR.LeaveManagement.Application.Features.LeaveRequests.Requests.Queries;
using HR.LeaveManagement.Application.Features.LeaveTypes.Requests;
using HR.LeaveManagement.Application.Features.LeaveTypes.Requests.Queries;
+using HR.LeaveManagement.Application.Mappings;
using RCommon.Mediator.Subscribers;
using System;
using System.Collections.Generic;
+using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
@@ -22,18 +23,15 @@ namespace HR.LeaveManagement.Application.Features.LeaveRequests.Handlers.Queries
public class GetLeaveRequestListRequestHandler : IAppRequestHandler>
{
private readonly IGraphRepository _leaveRequestRepository;
- private readonly IMapper _mapper;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly IUserService _userService;
public GetLeaveRequestListRequestHandler(IGraphRepository leaveRequestRepository,
- IMapper mapper,
IHttpContextAccessor httpContextAccessor,
IUserService userService)
{
_leaveRequestRepository = leaveRequestRepository;
this._leaveRequestRepository.DataStoreName = DataStoreNamesConst.LeaveManagement;
- _mapper = mapper;
this._httpContextAccessor = httpContextAccessor;
this._userService = userService;
}
@@ -48,10 +46,10 @@ public async Task> HandleAsync(GetLeaveRequestListRequ
{
var userId = _httpContextAccessor.HttpContext.User.FindFirst(
q => q.Type == CustomClaimTypes.Uid)?.Value;
- leaveRequests = await _leaveRequestRepository.FindAsync(x=>x.RequestingEmployeeId == userId) as List;
+ leaveRequests = await _leaveRequestRepository.FindAsync(x => x.RequestingEmployeeId == userId) as List;
var employee = await _userService.GetEmployee(userId);
- requests = _mapper.Map>(leaveRequests);
+ requests = leaveRequests.Select(x => x.ToLeaveRequestListDto()).ToList();
foreach (var req in requests)
{
req.Employee = employee;
@@ -60,7 +58,7 @@ public async Task> HandleAsync(GetLeaveRequestListRequ
else
{
leaveRequests = await _leaveRequestRepository.FindAsync(x => true) as List;
- requests = _mapper.Map>(leaveRequests);
+ requests = leaveRequests.Select(x => x.ToLeaveRequestListDto()).ToList();
foreach (var req in requests)
{
req.Employee = await _userService.GetEmployee(req.RequestingEmployeeId);
@@ -68,8 +66,6 @@ public async Task> HandleAsync(GetLeaveRequestListRequ
}
return requests;
-
-
}
}
}
diff --git a/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Features/LeaveTypes/Handlers/Commands/CreateLeaveTypeCommandHandler.cs b/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Features/LeaveTypes/Handlers/Commands/CreateLeaveTypeCommandHandler.cs
index 87a89b01..5c03cf37 100644
--- a/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Features/LeaveTypes/Handlers/Commands/CreateLeaveTypeCommandHandler.cs
+++ b/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Features/LeaveTypes/Handlers/Commands/CreateLeaveTypeCommandHandler.cs
@@ -1,7 +1,7 @@
-using AutoMapper;
using HR.LeaveManagement.Application.DTOs.LeaveType.Validators;
using HR.LeaveManagement.Application.Exceptions;
using HR.LeaveManagement.Application.Features.LeaveTypes.Requests.Commands;
+using HR.LeaveManagement.Application.Mappings;
using HR.LeaveManagement.Domain;
using RCommon.Mediator.Subscribers;
using System;
@@ -19,13 +19,11 @@ namespace HR.LeaveManagement.Application.Features.LeaveTypes.Handlers.Commands
{
public class CreateLeaveTypeCommandHandler : IAppRequestHandler
{
- private readonly IMapper _mapper;
private readonly IGraphRepository _leaveTypeRepository;
private readonly IValidationService _validationService;
- public CreateLeaveTypeCommandHandler(IMapper mapper, IGraphRepository leaveTypeRepository, IValidationService validationService)
+ public CreateLeaveTypeCommandHandler(IGraphRepository leaveTypeRepository, IValidationService validationService)
{
- _mapper = mapper;
_leaveTypeRepository = leaveTypeRepository;
_validationService = validationService;
this._leaveTypeRepository.DataStoreName = DataStoreNamesConst.LeaveManagement;
@@ -44,7 +42,7 @@ public async Task HandleAsync(CreateLeaveTypeCommand reques
}
else
{
- var leaveType = _mapper.Map(request.LeaveTypeDto);
+ var leaveType = request.LeaveTypeDto.ToLeaveType();
await _leaveTypeRepository.AddAsync(leaveType);
diff --git a/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Features/LeaveTypes/Handlers/Commands/DeleteLeaveTypeCommandHandler.cs b/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Features/LeaveTypes/Handlers/Commands/DeleteLeaveTypeCommandHandler.cs
index d3de1054..bccc384c 100644
--- a/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Features/LeaveTypes/Handlers/Commands/DeleteLeaveTypeCommandHandler.cs
+++ b/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Features/LeaveTypes/Handlers/Commands/DeleteLeaveTypeCommandHandler.cs
@@ -1,4 +1,3 @@
-using AutoMapper;
using HR.LeaveManagement.Application.Exceptions;
using HR.LeaveManagement.Application.Features.LeaveTypes.Requests.Commands;
using HR.LeaveManagement.Domain;
@@ -15,12 +14,10 @@ namespace HR.LeaveManagement.Application.Features.LeaveTypes.Handlers.Commands
{
public class DeleteLeaveTypeCommandHandler : IAppRequestHandler
{
- private readonly IMapper _mapper;
private readonly IGraphRepository _leaveTypeRepository;
- public DeleteLeaveTypeCommandHandler(IMapper mapper, IGraphRepository leaveTypeRepository)
+ public DeleteLeaveTypeCommandHandler(IGraphRepository leaveTypeRepository)
{
- _mapper = mapper;
_leaveTypeRepository = leaveTypeRepository;
this._leaveTypeRepository.DataStoreName = DataStoreNamesConst.LeaveManagement;
}
diff --git a/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Features/LeaveTypes/Handlers/Commands/UpdateLeaveTypeCommandHandler.cs b/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Features/LeaveTypes/Handlers/Commands/UpdateLeaveTypeCommandHandler.cs
index 331a23da..7b09d655 100644
--- a/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Features/LeaveTypes/Handlers/Commands/UpdateLeaveTypeCommandHandler.cs
+++ b/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Features/LeaveTypes/Handlers/Commands/UpdateLeaveTypeCommandHandler.cs
@@ -1,7 +1,7 @@
-using AutoMapper;
using HR.LeaveManagement.Application.DTOs.LeaveType.Validators;
using HR.LeaveManagement.Application.Exceptions;
using HR.LeaveManagement.Application.Features.LeaveTypes.Requests.Commands;
+using HR.LeaveManagement.Application.Mappings;
using HR.LeaveManagement.Domain;
using RCommon.Mediator.Subscribers;
using System;
@@ -17,13 +17,11 @@ namespace HR.LeaveManagement.Application.Features.LeaveTypes.Handlers.Commands
{
public class UpdateLeaveTypeCommandHandler : IAppRequestHandler
{
- private readonly IMapper _mapper;
private readonly IGraphRepository _leaveTypeRepository;
private readonly IValidationService _validationService;
- public UpdateLeaveTypeCommandHandler(IMapper mapper, IGraphRepository leaveTypeRepository, IValidationService validationService)
+ public UpdateLeaveTypeCommandHandler(IGraphRepository leaveTypeRepository, IValidationService validationService)
{
- _mapper = mapper;
_leaveTypeRepository = leaveTypeRepository;
_validationService = validationService;
this._leaveTypeRepository.DataStoreName = DataStoreNamesConst.LeaveManagement;
@@ -41,11 +39,9 @@ public async Task HandleAsync(UpdateLeaveTypeCommand request, CancellationToken
if (leaveType is null)
throw new NotFoundException(nameof(leaveType), request.LeaveTypeDto.Id);
- _mapper.Map(request.LeaveTypeDto, leaveType);
+ request.LeaveTypeDto.ApplyTo(leaveType);
await _leaveTypeRepository.UpdateAsync(leaveType);
-
-
}
}
}
diff --git a/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Features/LeaveTypes/Handlers/Queries/GetLeaveTypeDetailRequestHandler.cs b/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Features/LeaveTypes/Handlers/Queries/GetLeaveTypeDetailRequestHandler.cs
index 76f3b9c4..fda1bb41 100644
--- a/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Features/LeaveTypes/Handlers/Queries/GetLeaveTypeDetailRequestHandler.cs
+++ b/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Features/LeaveTypes/Handlers/Queries/GetLeaveTypeDetailRequestHandler.cs
@@ -1,9 +1,9 @@
-using AutoMapper;
using HR.LeaveManagement.Application.DTOs;
using HR.LeaveManagement.Application.DTOs.LeaveType;
using HR.LeaveManagement.Application.Features.LeaveRequests.Requests.Queries;
using HR.LeaveManagement.Application.Features.LeaveTypes.Requests;
using HR.LeaveManagement.Application.Features.LeaveTypes.Requests.Queries;
+using HR.LeaveManagement.Application.Mappings;
using HR.LeaveManagement.Domain;
using RCommon.Mediator.Subscribers;
using RCommon.Persistence;
@@ -19,18 +19,17 @@ namespace HR.LeaveManagement.Application.Features.LeaveTypes.Handlers.Queries
public class GetLeaveTypeDetailRequestHandler : IAppRequestHandler
{
private readonly IGraphRepository _leaveTypeRepository;
- private readonly IMapper _mapper;
- public GetLeaveTypeDetailRequestHandler(IGraphRepository leaveTypeRepository, IMapper mapper)
+ public GetLeaveTypeDetailRequestHandler(IGraphRepository leaveTypeRepository)
{
_leaveTypeRepository = leaveTypeRepository;
this._leaveTypeRepository.DataStoreName = DataStoreNamesConst.LeaveManagement;
- _mapper = mapper;
}
+
public async Task HandleAsync(GetLeaveTypeDetailRequest request, CancellationToken cancellationToken)
{
var leaveType = await _leaveTypeRepository.FindAsync(request.Id);
- return _mapper.Map(leaveType);
+ return leaveType.ToLeaveTypeDto();
}
}
}
diff --git a/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Features/LeaveTypes/Handlers/Queries/GetLeaveTypeListRequestHandler.cs b/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Features/LeaveTypes/Handlers/Queries/GetLeaveTypeListRequestHandler.cs
index b4f7a4f1..325209ec 100644
--- a/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Features/LeaveTypes/Handlers/Queries/GetLeaveTypeListRequestHandler.cs
+++ b/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Features/LeaveTypes/Handlers/Queries/GetLeaveTypeListRequestHandler.cs
@@ -1,14 +1,15 @@
-using AutoMapper;
using HR.LeaveManagement.Application.DTOs;
using HR.LeaveManagement.Application.DTOs.LeaveType;
using HR.LeaveManagement.Application.Features.LeaveTypes.Requests;
using HR.LeaveManagement.Application.Features.LeaveTypes.Requests.Queries;
+using HR.LeaveManagement.Application.Mappings;
using HR.LeaveManagement.Domain;
using RCommon.Mediator.Subscribers;
using RCommon.Persistence;
using RCommon.Persistence.Crud;
using System;
using System.Collections.Generic;
+using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
@@ -18,19 +19,17 @@ namespace HR.LeaveManagement.Application.Features.LeaveTypes.Handlers.Queries
public class GetLeaveTypeListRequestHandler : IAppRequestHandler>
{
private readonly IGraphRepository _leaveTypeRepository;
- private readonly IMapper _mapper;
- public GetLeaveTypeListRequestHandler(IGraphRepository leaveTypeRepository, IMapper mapper)
+ public GetLeaveTypeListRequestHandler(IGraphRepository leaveTypeRepository)
{
_leaveTypeRepository = leaveTypeRepository;
this._leaveTypeRepository.DataStoreName = DataStoreNamesConst.LeaveManagement;
- _mapper = mapper;
}
public async Task> HandleAsync(GetLeaveTypeListRequest request, CancellationToken cancellationToken)
{
- var leaveTypes = await _leaveTypeRepository.FindAsync(x=> true);
- return _mapper.Map>(leaveTypes);
+ var leaveTypes = await _leaveTypeRepository.FindAsync(x => true);
+ return leaveTypes.Select(x => x.ToLeaveTypeDto()).ToList();
}
}
}
diff --git a/Examples/CleanWithCQRS/HR.LeaveManagement.Application/HR.LeaveManagement.Application.csproj b/Examples/CleanWithCQRS/HR.LeaveManagement.Application/HR.LeaveManagement.Application.csproj
index be0e28e2..396fe89b 100644
--- a/Examples/CleanWithCQRS/HR.LeaveManagement.Application/HR.LeaveManagement.Application.csproj
+++ b/Examples/CleanWithCQRS/HR.LeaveManagement.Application/HR.LeaveManagement.Application.csproj
@@ -9,7 +9,6 @@
-
diff --git a/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Mappings/LeaveAllocationMappings.cs b/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Mappings/LeaveAllocationMappings.cs
new file mode 100644
index 00000000..b099c26b
--- /dev/null
+++ b/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Mappings/LeaveAllocationMappings.cs
@@ -0,0 +1,30 @@
+using HR.LeaveManagement.Application.DTOs.LeaveAllocation;
+using HR.LeaveManagement.Domain;
+
+namespace HR.LeaveManagement.Application.Mappings
+{
+ public static class LeaveAllocationMappings
+ {
+ public static LeaveAllocationDto ToLeaveAllocationDto(this LeaveAllocation source)
+ {
+ if (source == null) return null;
+ return new LeaveAllocationDto
+ {
+ Id = source.Id,
+ NumberOfDays = source.NumberOfDays,
+ LeaveTypeId = source.LeaveTypeId,
+ LeaveType = source.LeaveType?.ToLeaveTypeDto(),
+ Period = source.Period,
+ EmployeeId = source.EmployeeId
+ };
+ }
+
+ public static void ApplyTo(this UpdateLeaveAllocationDto source, LeaveAllocation destination)
+ {
+ if (source == null || destination == null) return;
+ destination.NumberOfDays = source.NumberOfDays;
+ destination.LeaveTypeId = source.LeaveTypeId;
+ destination.Period = source.Period;
+ }
+ }
+}
diff --git a/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Mappings/LeaveRequestMappings.cs b/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Mappings/LeaveRequestMappings.cs
new file mode 100644
index 00000000..1a376007
--- /dev/null
+++ b/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Mappings/LeaveRequestMappings.cs
@@ -0,0 +1,66 @@
+using HR.LeaveManagement.Application.DTOs.LeaveRequest;
+using HR.LeaveManagement.Domain;
+using System;
+
+namespace HR.LeaveManagement.Application.Mappings
+{
+ public static class LeaveRequestMappings
+ {
+ public static LeaveRequestDto ToLeaveRequestDto(this LeaveRequest source)
+ {
+ if (source == null) return null;
+ return new LeaveRequestDto
+ {
+ Id = source.Id,
+ StartDate = source.StartDate,
+ EndDate = source.EndDate,
+ LeaveTypeId = source.LeaveTypeId,
+ LeaveType = source.LeaveType?.ToLeaveTypeDto(),
+ DateRequested = source.DateRequested,
+ RequestComments = source.RequestComments,
+ DateActioned = source.DateActioned,
+ Approved = source.Approved,
+ Cancelled = source.Cancelled,
+ RequestingEmployeeId = source.RequestingEmployeeId
+ };
+ }
+
+ public static LeaveRequestListDto ToLeaveRequestListDto(this LeaveRequest source)
+ {
+ if (source == null) return null;
+ return new LeaveRequestListDto
+ {
+ Id = source.Id,
+ RequestingEmployeeId = source.RequestingEmployeeId,
+ LeaveType = source.LeaveType?.ToLeaveTypeDto(),
+ // DateRequested maps from DateCreated (audit field) per MappingProfile
+ DateRequested = source.DateCreated ?? DateTime.MinValue,
+ StartDate = source.StartDate,
+ EndDate = source.EndDate,
+ Approved = source.Approved
+ };
+ }
+
+ public static LeaveRequest ToLeaveRequest(this CreateLeaveRequestDto source)
+ {
+ if (source == null) return null;
+ return new LeaveRequest
+ {
+ StartDate = source.StartDate,
+ EndDate = source.EndDate,
+ LeaveTypeId = source.LeaveTypeId,
+ RequestComments = source.RequestComments
+ };
+ }
+
+ public static void ApplyTo(this UpdateLeaveRequestDto source, LeaveRequest destination)
+ {
+ if (source == null || destination == null) return;
+ destination.StartDate = source.StartDate;
+ destination.EndDate = source.EndDate;
+ destination.LeaveTypeId = source.LeaveTypeId;
+ destination.RequestComments = source.RequestComments;
+ destination.Cancelled = source.Cancelled;
+ }
+ }
+}
diff --git a/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Mappings/LeaveTypeMappings.cs b/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Mappings/LeaveTypeMappings.cs
new file mode 100644
index 00000000..ba60af8f
--- /dev/null
+++ b/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Mappings/LeaveTypeMappings.cs
@@ -0,0 +1,36 @@
+using HR.LeaveManagement.Application.DTOs.LeaveType;
+using HR.LeaveManagement.Domain;
+
+namespace HR.LeaveManagement.Application.Mappings
+{
+ public static class LeaveTypeMappings
+ {
+ public static LeaveTypeDto ToLeaveTypeDto(this LeaveType source)
+ {
+ if (source == null) return null;
+ return new LeaveTypeDto
+ {
+ Id = source.Id,
+ Name = source.Name,
+ DefaultDays = source.DefaultDays
+ };
+ }
+
+ public static LeaveType ToLeaveType(this CreateLeaveTypeDto source)
+ {
+ if (source == null) return null;
+ return new LeaveType
+ {
+ Name = source.Name,
+ DefaultDays = source.DefaultDays
+ };
+ }
+
+ public static void ApplyTo(this LeaveTypeDto source, LeaveType destination)
+ {
+ if (source == null || destination == null) return;
+ destination.Name = source.Name;
+ destination.DefaultDays = source.DefaultDays;
+ }
+ }
+}
diff --git a/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Profiles/MappingProfile.cs b/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Profiles/MappingProfile.cs
deleted file mode 100644
index 1f52c4b8..00000000
--- a/Examples/CleanWithCQRS/HR.LeaveManagement.Application/Profiles/MappingProfile.cs
+++ /dev/null
@@ -1,34 +0,0 @@
-using AutoMapper;
-using HR.LeaveManagement.Application.DTOs;
-using HR.LeaveManagement.Application.DTOs.LeaveAllocation;
-using HR.LeaveManagement.Application.DTOs.LeaveRequest;
-using HR.LeaveManagement.Application.DTOs.LeaveType;
-using HR.LeaveManagement.Domain;
-using System;
-using System.Collections.Generic;
-using System.Text;
-
-namespace HR.LeaveManagement.Application.Profiles
-{
- public class MappingProfile : Profile
- {
- public MappingProfile()
- {
- #region LeaveRequest Mappings
- CreateMap().ReverseMap();
- CreateMap()
- .ForMember(dest => dest.DateRequested, opt => opt.MapFrom(src => src.DateCreated))
- .ReverseMap();
- CreateMap().ReverseMap();
- CreateMap().ReverseMap();
- #endregion LeaveRequest
-
- CreateMap().ReverseMap();
- CreateMap().ReverseMap();
- CreateMap().ReverseMap();
-
- CreateMap().ReverseMap();
- CreateMap().ReverseMap();
- }
- }
-}
diff --git a/Examples/CleanWithCQRS/HR.LeaveManagement.Identity/Services/UserService.cs b/Examples/CleanWithCQRS/HR.LeaveManagement.Identity/Services/UserService.cs
index 8c84247a..3cc4ca31 100644
--- a/Examples/CleanWithCQRS/HR.LeaveManagement.Identity/Services/UserService.cs
+++ b/Examples/CleanWithCQRS/HR.LeaveManagement.Identity/Services/UserService.cs
@@ -1,4 +1,3 @@
-using AutoMapper;
using HR.LeaveManagement.Application.Contracts.Identity;
using HR.LeaveManagement.Application.Models.Identity;
using HR.LeaveManagement.Identity.Models;
@@ -36,7 +35,7 @@ public async Task GetEmployee(string userId)
public async Task> GetEmployees()
{
var employees = await _userManager.GetUsersInRoleAsync("Employee");
- return employees.Select(q => new Employee {
+ return employees.Select(q => new Employee {
Id = q.Id,
Email = q.Email,
Firstname = q.FirstName,
diff --git a/Examples/CleanWithCQRS/HR.LeaveManagement.MVC/Controllers/LeaveRequestsController.cs b/Examples/CleanWithCQRS/HR.LeaveManagement.MVC/Controllers/LeaveRequestsController.cs
index 6153fb8a..a0eae72c 100644
--- a/Examples/CleanWithCQRS/HR.LeaveManagement.MVC/Controllers/LeaveRequestsController.cs
+++ b/Examples/CleanWithCQRS/HR.LeaveManagement.MVC/Controllers/LeaveRequestsController.cs
@@ -1,8 +1,7 @@
-using System;
+using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
-using AutoMapper;
using HR.LeaveManagement.MVC.Contracts;
using HR.LeaveManagement.MVC.Models;
using Microsoft.AspNetCore.Authorization;
@@ -17,14 +16,11 @@ public class LeaveRequestsController : Controller
{
private readonly ILeaveTypeService _leaveTypeService;
private readonly ILeaveRequestService _leaveRequestService;
- private readonly IMapper _mapper;
- public LeaveRequestsController(ILeaveTypeService leaveTypeService, ILeaveRequestService leaveRequestService,
- IMapper mapper)
+ public LeaveRequestsController(ILeaveTypeService leaveTypeService, ILeaveRequestService leaveRequestService)
{
this._leaveTypeService = leaveTypeService;
this._leaveRequestService = leaveRequestService;
- this._mapper = mapper;
}
// GET: LeaveRequest/Create
@@ -97,4 +93,4 @@ public async Task ApproveRequest(int id, bool approved)
}
}
}
-}
\ No newline at end of file
+}
diff --git a/Examples/CleanWithCQRS/HR.LeaveManagement.MVC/Controllers/UsersController.cs b/Examples/CleanWithCQRS/HR.LeaveManagement.MVC/Controllers/UsersController.cs
index 487e334e..88f8cf17 100644
--- a/Examples/CleanWithCQRS/HR.LeaveManagement.MVC/Controllers/UsersController.cs
+++ b/Examples/CleanWithCQRS/HR.LeaveManagement.MVC/Controllers/UsersController.cs
@@ -1,4 +1,3 @@
-using AutoMapper;
using HR.LeaveManagement.MVC.Contracts;
using HR.LeaveManagement.MVC.Models;
using HR.LeaveManagement.MVC.Services.Base;
@@ -53,7 +52,7 @@ public async Task Register(RegisterVM registration)
if (isCreated)
return LocalRedirect(returnUrl);
}
-
+
ModelState.AddModelError("", "Registration Attempt Failed. Please try again.");
return View(registration);
}
diff --git a/Examples/CleanWithCQRS/HR.LeaveManagement.MVC/HR.LeaveManagement.MVC.csproj b/Examples/CleanWithCQRS/HR.LeaveManagement.MVC/HR.LeaveManagement.MVC.csproj
index 16872819..b01e1000 100644
--- a/Examples/CleanWithCQRS/HR.LeaveManagement.MVC/HR.LeaveManagement.MVC.csproj
+++ b/Examples/CleanWithCQRS/HR.LeaveManagement.MVC/HR.LeaveManagement.MVC.csproj
@@ -9,7 +9,6 @@
-
diff --git a/Examples/CleanWithCQRS/HR.LeaveManagement.MVC/MappingProfile.cs b/Examples/CleanWithCQRS/HR.LeaveManagement.MVC/MappingProfile.cs
deleted file mode 100644
index f5b0353b..00000000
--- a/Examples/CleanWithCQRS/HR.LeaveManagement.MVC/MappingProfile.cs
+++ /dev/null
@@ -1,34 +0,0 @@
-using AutoMapper;
-using HR.LeaveManagement.MVC.Models;
-using HR.LeaveManagement.MVC.Services.Base;
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Threading.Tasks;
-
-namespace HR.LeaveManagement.MVC
-{
- public class MappingProfile : Profile
- {
- public MappingProfile()
- {
- CreateMap().ReverseMap();
- CreateMap().ReverseMap();
- CreateMap()
- .ForMember(q => q.DateRequested, opt => opt.MapFrom(x => x.DateRequested.DateTime))
- .ForMember(q => q.StartDate, opt => opt.MapFrom(x => x.StartDate.DateTime))
- .ForMember(q => q.EndDate, opt => opt.MapFrom(x => x.EndDate.DateTime))
- .ReverseMap();
- CreateMap()
- .ForMember(q => q.DateRequested, opt => opt.MapFrom(x => x.DateRequested.DateTime))
- .ForMember(q => q.StartDate, opt => opt.MapFrom(x => x.StartDate.DateTime))
- .ForMember(q => q.EndDate, opt => opt.MapFrom(x => x.EndDate.DateTime))
- .ReverseMap();
- CreateMap().ReverseMap();
- CreateMap().ReverseMap();
- CreateMap().ReverseMap();
- CreateMap().ReverseMap();
- }
- }
-
-}
diff --git a/Examples/CleanWithCQRS/HR.LeaveManagement.MVC/Mappings/ViewModelMappings.cs b/Examples/CleanWithCQRS/HR.LeaveManagement.MVC/Mappings/ViewModelMappings.cs
new file mode 100644
index 00000000..f7803043
--- /dev/null
+++ b/Examples/CleanWithCQRS/HR.LeaveManagement.MVC/Mappings/ViewModelMappings.cs
@@ -0,0 +1,155 @@
+using HR.LeaveManagement.MVC.Models;
+using HR.LeaveManagement.MVC.Services.Base;
+using System.Collections.Generic;
+using System.Linq;
+
+namespace HR.LeaveManagement.MVC.Mappings
+{
+ public static class ViewModelMappings
+ {
+ // ─── LeaveType ───────────────────────────────────────────────────────────
+
+ public static LeaveTypeVM ToLeaveTypeVM(this LeaveTypeDto source)
+ {
+ if (source == null) return null;
+ return new LeaveTypeVM
+ {
+ Id = source.Id,
+ Name = source.Name,
+ DefaultDays = source.DefaultDays
+ };
+ }
+
+ public static List ToLeaveTypeVMList(this ICollection source)
+ {
+ if (source == null) return new List();
+ return source.Select(x => x.ToLeaveTypeVM()).ToList();
+ }
+
+ public static CreateLeaveTypeDto ToCreateLeaveTypeDto(this CreateLeaveTypeVM source)
+ {
+ if (source == null) return null;
+ return new CreateLeaveTypeDto
+ {
+ Name = source.Name,
+ DefaultDays = source.DefaultDays
+ };
+ }
+
+ public static LeaveTypeDto ToLeaveTypeDto(this LeaveTypeVM source)
+ {
+ if (source == null) return null;
+ return new LeaveTypeDto
+ {
+ Id = source.Id,
+ Name = source.Name,
+ DefaultDays = source.DefaultDays
+ };
+ }
+
+ // ─── Employee ────────────────────────────────────────────────────────────
+
+ public static EmployeeVM ToEmployeeVM(this Employee source)
+ {
+ if (source == null) return null;
+ return new EmployeeVM
+ {
+ Id = source.Id,
+ Email = source.Email,
+ Firstname = source.Firstname,
+ Lastname = source.Lastname
+ };
+ }
+
+ // ─── LeaveRequest ────────────────────────────────────────────────────────
+
+ public static LeaveRequestVM ToLeaveRequestVM(this LeaveRequestDto source)
+ {
+ if (source == null) return null;
+ return new LeaveRequestVM
+ {
+ Id = source.Id,
+ StartDate = source.StartDate.DateTime,
+ EndDate = source.EndDate.DateTime,
+ DateRequested = source.DateRequested.DateTime,
+ DateActioned = source.DateActioned?.DateTime ?? default,
+ LeaveTypeId = source.LeaveTypeId,
+ LeaveType = source.LeaveType?.ToLeaveTypeVM(),
+ Employee = source.Employee?.ToEmployeeVM(),
+ RequestComments = source.RequestComments,
+ Approved = source.Approved,
+ Cancelled = source.Cancelled
+ };
+ }
+
+ public static LeaveRequestVM ToLeaveRequestVM(this LeaveRequestListDto source)
+ {
+ if (source == null) return null;
+ return new LeaveRequestVM
+ {
+ Id = source.Id,
+ StartDate = source.StartDate.DateTime,
+ EndDate = source.EndDate.DateTime,
+ DateRequested = source.DateRequested.DateTime,
+ LeaveTypeId = source.LeaveType?.Id ?? 0,
+ LeaveType = source.LeaveType?.ToLeaveTypeVM(),
+ Employee = source.Employee?.ToEmployeeVM(),
+ Approved = source.Approved
+ };
+ }
+
+ public static List ToLeaveRequestVMList(this ICollection source)
+ {
+ if (source == null) return new List();
+ return source.Select(x => x.ToLeaveRequestVM()).ToList();
+ }
+
+ public static CreateLeaveRequestDto ToCreateLeaveRequestDto(this CreateLeaveRequestVM source)
+ {
+ if (source == null) return null;
+ return new CreateLeaveRequestDto
+ {
+ StartDate = source.StartDate,
+ EndDate = source.EndDate,
+ LeaveTypeId = source.LeaveTypeId,
+ RequestComments = source.RequestComments
+ };
+ }
+
+ // ─── LeaveAllocation ─────────────────────────────────────────────────────
+
+ public static LeaveAllocationVM ToLeaveAllocationVM(this LeaveAllocationDto source)
+ {
+ if (source == null) return null;
+ return new LeaveAllocationVM
+ {
+ Id = source.Id,
+ NumberOfDays = source.NumberOfDays,
+ Period = source.Period,
+ LeaveTypeId = source.LeaveTypeId,
+ LeaveType = source.LeaveType?.ToLeaveTypeVM()
+ };
+ }
+
+ public static List ToLeaveAllocationVMList(this ICollection source)
+ {
+ if (source == null) return new List();
+ return source.Select(x => x.ToLeaveAllocationVM()).ToList();
+ }
+
+ // ─── Registration ────────────────────────────────────────────────────────
+
+ public static RegistrationRequest ToRegistrationRequest(this RegisterVM source)
+ {
+ if (source == null) return null;
+ return new RegistrationRequest
+ {
+ FirstName = source.FirstName,
+ LastName = source.LastName,
+ Email = source.Email,
+ UserName = source.UserName,
+ Password = source.Password
+ };
+ }
+ }
+}
diff --git a/Examples/CleanWithCQRS/HR.LeaveManagement.MVC/Program.cs b/Examples/CleanWithCQRS/HR.LeaveManagement.MVC/Program.cs
index f8374e1e..4e47eb50 100644
--- a/Examples/CleanWithCQRS/HR.LeaveManagement.MVC/Program.cs
+++ b/Examples/CleanWithCQRS/HR.LeaveManagement.MVC/Program.cs
@@ -5,7 +5,6 @@
using System.Reflection;
using HR.LeaveManagement.MVC.Middleware;
using Microsoft.Extensions.DependencyInjection;
-using HR.LeaveManagement.MVC;
var builder = WebApplication.CreateBuilder(args);
@@ -26,7 +25,6 @@
builder.Services.AddTransient();
builder.Services.AddHttpClient(cl => cl.BaseAddress = new Uri("https://localhost:7273"));
-builder.Services.AddAutoMapper(x => x.AddProfile(new MappingProfile()));
builder.Services.AddScoped();
builder.Services.AddScoped();
diff --git a/Examples/CleanWithCQRS/HR.LeaveManagement.MVC/Services/AuthenticationService.cs b/Examples/CleanWithCQRS/HR.LeaveManagement.MVC/Services/AuthenticationService.cs
index 0f1bb470..3f30d5bc 100644
--- a/Examples/CleanWithCQRS/HR.LeaveManagement.MVC/Services/AuthenticationService.cs
+++ b/Examples/CleanWithCQRS/HR.LeaveManagement.MVC/Services/AuthenticationService.cs
@@ -1,4 +1,4 @@
-using AutoMapper;
+using HR.LeaveManagement.MVC.Mappings;
using HR.LeaveManagement.MVC.Models;
using HR.LeaveManagement.MVC.Services.Base;
using Microsoft.AspNetCore.Authentication;
@@ -18,15 +18,12 @@ namespace HR.LeaveManagement.MVC.Contracts
public class AuthenticationService : BaseHttpService, IAuthenticationService
{
private readonly IHttpContextAccessor _httpContextAccessor;
- private readonly IMapper _mapper;
private JsonWebTokenHandler _tokenHandler;
- public AuthenticationService(IClient client, ILocalStorageService localStorage, IHttpContextAccessor httpContextAccessor,
- IMapper mapper)
+ public AuthenticationService(IClient client, ILocalStorageService localStorage, IHttpContextAccessor httpContextAccessor)
: base(client, localStorage)
{
this._httpContextAccessor = httpContextAccessor;
- this._mapper = mapper;
this._tokenHandler = new JsonWebTokenHandler();
}
@@ -50,7 +47,7 @@ public async Task Authenticate(string email, string password)
}
return false;
}
- catch
+ catch
{
return false;
}
@@ -58,8 +55,7 @@ public async Task Authenticate(string email, string password)
public async Task Register(RegisterVM registration)
{
-
- RegistrationRequest registrationRequest = _mapper.Map(registration);
+ RegistrationRequest registrationRequest = registration.ToRegistrationRequest();
var response = await _client.RegisterAsync(registrationRequest);
if (!string.IsNullOrEmpty(response.UserId))
diff --git a/Examples/CleanWithCQRS/HR.LeaveManagement.MVC/Services/LeaveAllocationService.cs b/Examples/CleanWithCQRS/HR.LeaveManagement.MVC/Services/LeaveAllocationService.cs
index f1b812ad..e6d41350 100644
--- a/Examples/CleanWithCQRS/HR.LeaveManagement.MVC/Services/LeaveAllocationService.cs
+++ b/Examples/CleanWithCQRS/HR.LeaveManagement.MVC/Services/LeaveAllocationService.cs
@@ -1,5 +1,4 @@
-using AutoMapper;
-using HR.LeaveManagement.MVC.Contracts;
+using HR.LeaveManagement.MVC.Contracts;
using HR.LeaveManagement.MVC.Models;
using HR.LeaveManagement.MVC.Services.Base;
using System;
diff --git a/Examples/CleanWithCQRS/HR.LeaveManagement.MVC/Services/LeaveRequestService.cs b/Examples/CleanWithCQRS/HR.LeaveManagement.MVC/Services/LeaveRequestService.cs
index 56059636..0fa25926 100644
--- a/Examples/CleanWithCQRS/HR.LeaveManagement.MVC/Services/LeaveRequestService.cs
+++ b/Examples/CleanWithCQRS/HR.LeaveManagement.MVC/Services/LeaveRequestService.cs
@@ -1,5 +1,5 @@
-using AutoMapper;
using HR.LeaveManagement.MVC.Contracts;
+using HR.LeaveManagement.MVC.Mappings;
using HR.LeaveManagement.MVC.Models;
using HR.LeaveManagement.MVC.Services.Base;
using System;
@@ -12,13 +12,11 @@ namespace HR.LeaveManagement.MVC.Services
public class LeaveRequestService : BaseHttpService, ILeaveRequestService
{
private readonly ILocalStorageService _localStorageService;
- private readonly IMapper _mapper;
private readonly IClient _httpclient;
- public LeaveRequestService(IMapper mapper, IClient httpclient, ILocalStorageService localStorageService) : base(httpclient, localStorageService)
+ public LeaveRequestService(IClient httpclient, ILocalStorageService localStorageService) : base(httpclient, localStorageService)
{
this._localStorageService = localStorageService;
- this._mapper = mapper;
this._httpclient = httpclient;
}
@@ -32,7 +30,6 @@ public async Task ApproveLeaveRequest(int id, bool approved)
}
catch (Exception)
{
-
throw;
}
}
@@ -42,7 +39,7 @@ public async Task> CreateLeaveRequest(CreateLeaveRequestVM leaveRe
try
{
var response = new Response();
- CreateLeaveRequestDto createLeaveRequest = _mapper.Map(leaveRequest);
+ CreateLeaveRequestDto createLeaveRequest = leaveRequest.ToCreateLeaveRequestDto();
AddBearerToken();
var apiResponse = await _client.LeaveRequestsPOSTAsync(createLeaveRequest);
if (apiResponse.Success)
@@ -81,7 +78,7 @@ public async Task GetAdminLeaveRequestList()
ApprovedRequests = leaveRequests.Count(q => q.Approved == true),
PendingRequests = leaveRequests.Count(q => q.Approved == null),
RejectedRequests = leaveRequests.Count(q => q.Approved == false),
- LeaveRequests = _mapper.Map>(leaveRequests)
+ LeaveRequests = leaveRequests.ToLeaveRequestVMList()
};
return model;
}
@@ -90,7 +87,7 @@ public async Task GetLeaveRequest(int id)
{
AddBearerToken();
var leaveRequest = await _client.LeaveRequestsGETAsync(id);
- return _mapper.Map(leaveRequest);
+ return leaveRequest.ToLeaveRequestVM();
}
public async Task GetUserLeaveRequests()
@@ -100,8 +97,8 @@ public async Task GetUserLeaveRequests()
var allocations = await _client.LeaveAllocationsAllAsync(isLoggedInUser: true);
var model = new EmployeeLeaveRequestViewVM
{
- LeaveAllocations = _mapper.Map>(allocations),
- LeaveRequests = _mapper.Map>(leaveRequests)
+ LeaveAllocations = allocations.ToLeaveAllocationVMList(),
+ LeaveRequests = leaveRequests.ToLeaveRequestVMList()
};
return model;
diff --git a/Examples/CleanWithCQRS/HR.LeaveManagement.MVC/Services/LeaveTypeService.cs b/Examples/CleanWithCQRS/HR.LeaveManagement.MVC/Services/LeaveTypeService.cs
index 80a53ea3..91482b1e 100644
--- a/Examples/CleanWithCQRS/HR.LeaveManagement.MVC/Services/LeaveTypeService.cs
+++ b/Examples/CleanWithCQRS/HR.LeaveManagement.MVC/Services/LeaveTypeService.cs
@@ -1,5 +1,5 @@
-using AutoMapper;
using HR.LeaveManagement.MVC.Contracts;
+using HR.LeaveManagement.MVC.Mappings;
using HR.LeaveManagement.MVC.Models;
using HR.LeaveManagement.MVC.Services.Base;
using System;
@@ -12,13 +12,11 @@ namespace HR.LeaveManagement.MVC.Services
public class LeaveTypeService : BaseHttpService, ILeaveTypeService
{
private readonly ILocalStorageService _localStorageService;
- private readonly IMapper _mapper;
private readonly IClient _httpclient;
- public LeaveTypeService(IMapper mapper, IClient httpclient, ILocalStorageService localStorageService) : base(httpclient, localStorageService)
+ public LeaveTypeService(IClient httpclient, ILocalStorageService localStorageService) : base(httpclient, localStorageService)
{
this._localStorageService = localStorageService;
- this._mapper = mapper;
this._httpclient = httpclient;
}
@@ -27,7 +25,7 @@ public async Task> CreateLeaveType(CreateLeaveTypeVM leaveType)
try
{
var response = new Response();
- CreateLeaveTypeDto createLeaveType = _mapper.Map(leaveType);
+ CreateLeaveTypeDto createLeaveType = leaveType.ToCreateLeaveTypeDto();
AddBearerToken();
var apiResponse = await _client.LeaveTypesPOSTAsync(createLeaveType);
if (apiResponse.Success)
@@ -68,21 +66,21 @@ public async Task GetLeaveTypeDetails(int id)
{
AddBearerToken();
var leaveType = await _client.LeaveTypesGETAsync(id);
- return _mapper.Map(leaveType);
+ return leaveType.ToLeaveTypeVM();
}
public async Task> GetLeaveTypes()
{
AddBearerToken();
var leaveTypes = await _client.LeaveTypesAllAsync();
- return _mapper.Map>(leaveTypes);
+ return leaveTypes.ToLeaveTypeVMList();
}
public async Task> UpdateLeaveType(int id, LeaveTypeVM leaveType)
{
try
{
- LeaveTypeDto leaveTypeDto = _mapper.Map(leaveType);
+ LeaveTypeDto leaveTypeDto = leaveType.ToLeaveTypeDto();
AddBearerToken();
await _client.LeaveTypesPUTAsync(id.ToString(), leaveTypeDto);
return new Response() { Success = true };
@@ -92,6 +90,5 @@ public async Task> UpdateLeaveType(int id, LeaveTypeVM leaveType)
return ConvertApiExceptions(ex);
}
}
-
}
}
diff --git a/Src/RCommon.ApplicationServices/Validation/ValidationService.cs b/Src/RCommon.ApplicationServices/Validation/ValidationService.cs
index ab5c785b..d7bb9ea9 100644
--- a/Src/RCommon.ApplicationServices/Validation/ValidationService.cs
+++ b/Src/RCommon.ApplicationServices/Validation/ValidationService.cs
@@ -38,7 +38,7 @@ public async Task ValidateAsync(T target, bool throwOnFaul
{
var provider = scope.ServiceProvider.GetService();
Guard.IsNotNull(provider!, nameof(provider));
- var outcome = await provider!.ValidateAsync(target, throwOnFaults, cancellationToken);
+ var outcome = await provider!.ValidateAsync(target, throwOnFaults, cancellationToken).ConfigureAwait(false);
return outcome;
}
}
diff --git a/Src/RCommon.Core/DisposableResource.cs b/Src/RCommon.Core/DisposableResource.cs
index c67bd65b..22324c5e 100644
--- a/Src/RCommon.Core/DisposableResource.cs
+++ b/Src/RCommon.Core/DisposableResource.cs
@@ -13,19 +13,10 @@ namespace RCommon
///
///
/// Derived classes should override and/or
- /// to release managed and unmanaged resources. The finalizer calls
- /// with false to release unmanaged resources only.
+ /// to release managed and unmanaged resources.
///
public abstract class DisposableResource : IDisposable, IAsyncDisposable
{
- ///
- /// Finalizer that invokes with false to release unmanaged resources.
- ///
- ~DisposableResource()
- {
- Dispose(false);
- }
-
///
/// Performs application-defined tasks associated with freeing, releasing, or resetting resources.
/// Suppresses finalization after disposal.
@@ -44,7 +35,7 @@ public void Dispose()
/// A representing the asynchronous dispose operation.
public async ValueTask DisposeAsync()
{
- await this.DisposeAsync(true);
+ await this.DisposeAsync(true).ConfigureAwait(false);
GC.SuppressFinalize(this);
}
@@ -61,10 +52,9 @@ protected virtual void Dispose(bool disposing)
///
/// true to release both managed and unmanaged resources; false to release only unmanaged resources.
/// A representing the asynchronous dispose operation.
- protected async virtual Task DisposeAsync(bool disposing)
+ protected virtual Task DisposeAsync(bool disposing)
{
-
- await Task.Yield();
+ return Task.CompletedTask;
}
}
}
diff --git a/Src/RCommon.Core/EventHandling/IEventBus.cs b/Src/RCommon.Core/EventHandling/IEventBus.cs
index 17034927..c1efcf0d 100644
--- a/Src/RCommon.Core/EventHandling/IEventBus.cs
+++ b/Src/RCommon.Core/EventHandling/IEventBus.cs
@@ -1,4 +1,5 @@
-using System.Threading.Tasks;
+using System.Threading;
+using System.Threading.Tasks;
namespace RCommon.EventHandling
{
@@ -13,8 +14,9 @@ public interface IEventBus
///
/// The type of event to publish.
/// The event instance to publish.
+ /// Optional cancellation token.
/// A representing the asynchronous operation.
- Task PublishAsync(TEvent @event);
+ Task PublishAsync(TEvent @event, CancellationToken cancellationToken = default);
///
/// Subscribes a specific event handler to a specific event type.
diff --git a/Src/RCommon.Core/EventHandling/InMemoryEventBus.cs b/Src/RCommon.Core/EventHandling/InMemoryEventBus.cs
index aa224abe..7008aa1d 100644
--- a/Src/RCommon.Core/EventHandling/InMemoryEventBus.cs
+++ b/Src/RCommon.Core/EventHandling/InMemoryEventBus.cs
@@ -1,18 +1,18 @@
-#region MIT License
+#region MIT License
// The MIT License (MIT)
-//
+//
// Original Source: https://github.com/jacqueskang/EventBus/blob/develop/src/JKang.EventBus.Core/InMemory/InMemoryEventBus.cs
-//
+//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
-//
+//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
-//
+//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
@@ -39,38 +39,42 @@ namespace RCommon.EventHandling
/// handlers from the dependency injection container.
///
///
- /// Subscriptions are registered directly into the at configuration time.
- /// Publishing creates a new scope and resolves all handlers via reflection to support polymorphic event dispatch.
+ /// Subscriptions registered via or
+ /// are tracked internally and resolved
+ /// at publish time via . For best results, register subscribers
+ /// in the DI container during configuration using InMemoryEventBusBuilderExtensions.AddSubscriber.
///
public class InMemoryEventBus : IEventBus
{
- private readonly IServiceCollection _services;
private readonly IServiceProvider _serviceProvider;
+ private readonly ConcurrentBag<(Type serviceType, Type implementationType)> _dynamicSubscriptions = new();
///
/// Initializes a new instance of .
///
/// The root service provider used to create scopes for event publishing.
- /// The service collection for registering subscriber services at configuration time.
- public InMemoryEventBus(IServiceProvider serviceProvider, IServiceCollection services)
+ public InMemoryEventBus(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
- _services = services;
}
///
+ ///
+ /// Tracks the subscription internally. The handler will be resolved via
+ /// at publish time within a new scope.
+ ///
public IEventBus Subscribe()
where TEvent : class
where TEventHandler : class, ISubscriber
{
- _services.AddScoped, TEventHandler>();
+ _dynamicSubscriptions.Add((typeof(ISubscriber), typeof(TEventHandler)));
return this;
}
///
///
/// Uses reflection to discover all interfaces on
- /// and registers each as a scoped service.
+ /// and tracks each for resolution at publish time.
///
public IEventBus SubscribeAllHandledEvents()
where TEventHandler : class
@@ -84,7 +88,7 @@ public IEventBus SubscribeAllHandledEvents()
foreach (Type serviceType in serviceTypes)
{
- _services.AddScoped(serviceType, implementationType);
+ _dynamicSubscriptions.Add((serviceType, implementationType));
}
return this;
@@ -94,8 +98,9 @@ public IEventBus SubscribeAllHandledEvents()
///
/// Creates a new DI scope and uses reflection to resolve handlers for the runtime event type,
/// invoking on each handler sequentially.
+ /// Also resolves handlers from dynamic subscriptions registered via .
///
- public async Task PublishAsync(TEvent @event)
+ public async Task PublishAsync(TEvent @event, CancellationToken cancellationToken = default)
{
using (IServiceScope scope = _serviceProvider.CreateScope())
{
@@ -104,7 +109,13 @@ public async Task PublishAsync(TEvent @event)
Type openHandlerType = typeof(ISubscriber<>);
Type handlerType = openHandlerType.MakeGenericType(eventType);
IEnumerable