unit of work

This commit is contained in:
2026-02-05 19:31:17 +01:00
parent 61997cc277
commit 82d44c230f
3 changed files with 29 additions and 1 deletions

View File

@@ -1,4 +1,5 @@
using AipsCore.Application.Abstract.Command;
using AipsCore.Domain.Abstract;
using AipsCore.Domain.Models.User.External;
namespace AipsCore.Application.Models.User.Command.CreateUser;
@@ -6,10 +7,12 @@ namespace AipsCore.Application.Models.User.Command.CreateUser;
public class CreateUserCommandHandler : ICommandHandler<CreateUserCommand>
{
private readonly IUserRepository _userRepository;
private readonly IUnitOfWork _unitOfWork;
public CreateUserCommandHandler(IUserRepository userRepository)
public CreateUserCommandHandler(IUserRepository userRepository, IUnitOfWork unitOfWork)
{
_userRepository = userRepository;
_unitOfWork = unitOfWork;
}
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);
await _userRepository.Save(user, cancellationToken);
await _unitOfWork.SaveChangesAsync(cancellationToken);
}
}

View File

@@ -0,0 +1,6 @@
namespace AipsCore.Domain.Abstract;
public interface IUnitOfWork
{
Task SaveChangesAsync(CancellationToken cancellationToken = default);
}

View 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);
}
}