using System; using System.Threading.Tasks; using CleanArchitecture.Domain.Interfaces; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; namespace CleanArchitecture.Infrastructure; public sealed class UnitOfWork : IUnitOfWork where TContext : DbContext { private readonly TContext _context; private readonly ILogger> _logger; public UnitOfWork(TContext context, ILogger> logger) { _context = context; _logger = logger; } public async Task CommitAsync() { try { await _context.SaveChangesAsync(); return true; } catch (DbUpdateException dbUpdateException) { _logger.LogError(dbUpdateException, "An error occured during commiting changes"); return false; } } public void Dispose() { Dispose(true); // ReSharper disable once GCSuppressFinalizeForTypeWithoutDestructor GC.SuppressFinalize(this); } private void Dispose(bool disposing) { if (disposing) { _context.Dispose(); } } }