unit of work
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
using AipsCore.Application.Abstract.Command;
|
using AipsCore.Application.Abstract.Command;
|
||||||
|
using AipsCore.Domain.Abstract;
|
||||||
using AipsCore.Domain.Models.User.External;
|
using AipsCore.Domain.Models.User.External;
|
||||||
|
|
||||||
namespace AipsCore.Application.Models.User.Command.CreateUser;
|
namespace AipsCore.Application.Models.User.Command.CreateUser;
|
||||||
@@ -6,10 +7,12 @@ namespace AipsCore.Application.Models.User.Command.CreateUser;
|
|||||||
public class CreateUserCommandHandler : ICommandHandler<CreateUserCommand>
|
public class CreateUserCommandHandler : ICommandHandler<CreateUserCommand>
|
||||||
{
|
{
|
||||||
private readonly IUserRepository _userRepository;
|
private readonly IUserRepository _userRepository;
|
||||||
|
private readonly IUnitOfWork _unitOfWork;
|
||||||
|
|
||||||
public CreateUserCommandHandler(IUserRepository userRepository)
|
public CreateUserCommandHandler(IUserRepository userRepository, IUnitOfWork unitOfWork)
|
||||||
{
|
{
|
||||||
_userRepository = userRepository;
|
_userRepository = userRepository;
|
||||||
|
_unitOfWork = unitOfWork;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task Handle(CreateUserCommand command, CancellationToken cancellationToken = default)
|
public async Task Handle(CreateUserCommand command, CancellationToken cancellationToken = default)
|
||||||
@@ -17,5 +20,6 @@ public class CreateUserCommandHandler : ICommandHandler<CreateUserCommand>
|
|||||||
var user = Domain.Models.User.User.Create(command.Email, command.Username);
|
var user = Domain.Models.User.User.Create(command.Email, command.Username);
|
||||||
|
|
||||||
await _userRepository.Save(user, cancellationToken);
|
await _userRepository.Save(user, cancellationToken);
|
||||||
|
await _unitOfWork.SaveChangesAsync(cancellationToken);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
6
dotnet/AipsCore/Domain/Abstract/IUnitOfWork.cs
Normal file
6
dotnet/AipsCore/Domain/Abstract/IUnitOfWork.cs
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
namespace AipsCore.Domain.Abstract;
|
||||||
|
|
||||||
|
public interface IUnitOfWork
|
||||||
|
{
|
||||||
|
Task SaveChangesAsync(CancellationToken cancellationToken = default);
|
||||||
|
}
|
||||||
18
dotnet/AipsCore/Infrastructure/Db/EfUnitOfWork.cs
Normal file
18
dotnet/AipsCore/Infrastructure/Db/EfUnitOfWork.cs
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
using AipsCore.Domain.Abstract;
|
||||||
|
|
||||||
|
namespace AipsCore.Infrastructure.Db;
|
||||||
|
|
||||||
|
public class EfUnitOfWork : IUnitOfWork
|
||||||
|
{
|
||||||
|
private readonly AipsDbContext _dbContext;
|
||||||
|
|
||||||
|
public EfUnitOfWork(AipsDbContext dbContext)
|
||||||
|
{
|
||||||
|
_dbContext = dbContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task SaveChangesAsync(CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
await _dbContext.SaveChangesAsync(cancellationToken);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user