-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from sj-distributor/setting-and-repo
Add settings infrastructure and refactor repo pattern
- Loading branch information
Showing
27 changed files
with
317 additions
and
167 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
namespace Wax.Core.Data; | ||
|
||
public interface IUnitOfWork | ||
{ | ||
Task CommitAsync(CancellationToken cancellationToken = default); | ||
} | ||
|
||
public class UnitOfWork : IUnitOfWork | ||
{ | ||
private readonly ApplicationDbContext _context; | ||
|
||
public UnitOfWork(ApplicationDbContext context) | ||
{ | ||
_context = context; | ||
} | ||
|
||
public Task CommitAsync(CancellationToken cancellationToken = default) | ||
{ | ||
return _context.HasEntitiesChanged ? _context.SaveChangesAsync(cancellationToken) : Task.CompletedTask; | ||
} | ||
} |
This file contains 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
12 changes: 6 additions & 6 deletions
12
src/Wax.Core/Handlers/CommandHandlers/Customers/DeleteCustomerCommandHandler.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,24 @@ | ||
using Mediator.Net.Context; | ||
using Mediator.Net.Contracts; | ||
using Wax.Core.Data; | ||
using Wax.Core.Repositories; | ||
using Wax.Messages.Commands.Customers; | ||
|
||
namespace Wax.Core.Handlers.CommandHandlers.Customers; | ||
|
||
public class DeleteCustomerCommandHandler: ICommandHandler<DeleteCustomerCommand> | ||
{ | ||
private readonly IUnitOfWork _unitOfWork; | ||
private readonly ICustomerRepository _customerRepository; | ||
|
||
public DeleteCustomerCommandHandler(IUnitOfWork unitOfWork) | ||
public DeleteCustomerCommandHandler(ICustomerRepository customerRepository) | ||
{ | ||
_unitOfWork = unitOfWork; | ||
_customerRepository = customerRepository; | ||
} | ||
|
||
public async Task Handle(IReceiveContext<DeleteCustomerCommand> context, CancellationToken cancellationToken) | ||
{ | ||
var customer = await _unitOfWork.Customers.GetByIdAsync(context.Message.CustomerId, cancellationToken); | ||
var customer = await _customerRepository.GetByIdAsync(context.Message.CustomerId); | ||
|
||
await _unitOfWork.Customers.DeleteAsync(customer, cancellationToken); | ||
await _unitOfWork.SaveChangesAsync(cancellationToken); | ||
await _customerRepository.DeleteAsync(customer); | ||
} | ||
} |
This file contains 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
This file contains 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
23 changes: 23 additions & 0 deletions
23
src/Wax.Core/Middlewares/UnitOfWorks/UnitOfWorkMiddleware.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using Mediator.Net; | ||
using Mediator.Net.Context; | ||
using Mediator.Net.Contracts; | ||
using Mediator.Net.Pipeline; | ||
using Wax.Core.Data; | ||
|
||
namespace Wax.Core.Middlewares.UnitOfWorks; | ||
|
||
public static class UnitOfWorkMiddleware | ||
{ | ||
public static void UseUnitOfWork<TContext>(this IPipeConfigurator<TContext> configurator) | ||
where TContext : IContext<IMessage> | ||
{ | ||
if (configurator.DependencyScope == null) | ||
{ | ||
throw new DependencyScopeNotConfiguredException("IDependencyScope is not configured."); | ||
} | ||
|
||
var uow = configurator.DependencyScope.Resolve<IUnitOfWork>(); | ||
|
||
configurator.AddPipeSpecification(new UnitOfWorkSpecification<TContext>(uow)); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/Wax.Core/Middlewares/UnitOfWorks/UnitOfWorkSpecification.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using System.Runtime.ExceptionServices; | ||
using Mediator.Net.Context; | ||
using Mediator.Net.Contracts; | ||
using Mediator.Net.Pipeline; | ||
using Wax.Core.Data; | ||
|
||
namespace Wax.Core.Middlewares.UnitOfWorks; | ||
|
||
public class UnitOfWorkSpecification<TContext> : IPipeSpecification<TContext> | ||
where TContext : IContext<IMessage> | ||
{ | ||
private readonly IUnitOfWork _unitOfWork; | ||
|
||
public UnitOfWorkSpecification(IUnitOfWork unitOfWork) | ||
{ | ||
_unitOfWork = unitOfWork; | ||
} | ||
|
||
public bool ShouldExecute(TContext context, CancellationToken cancellationToken) | ||
{ | ||
return context.Message is ICommand or IEvent; | ||
} | ||
|
||
public Task BeforeExecute(TContext context, CancellationToken cancellationToken) | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
|
||
public Task Execute(TContext context, CancellationToken cancellationToken) | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
|
||
public Task AfterExecute(TContext context, CancellationToken cancellationToken) | ||
{ | ||
return ShouldExecute(context, cancellationToken) | ||
? _unitOfWork.CommitAsync(cancellationToken) | ||
: Task.CompletedTask; | ||
} | ||
|
||
public Task OnException(Exception ex, TContext context) | ||
{ | ||
ExceptionDispatchInfo.Capture(ex).Throw(); | ||
return Task.CompletedTask; | ||
} | ||
} |
Oops, something went wrong.