mirror of
https://github.com/alex289/CleanArchitecture.git
synced 2025-06-29 18:21:08 +00:00
48 lines
1.2 KiB
C#
48 lines
1.2 KiB
C#
using System;
|
|
using System.Threading.Tasks;
|
|
using CleanArchitecture.Domain.Interfaces;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace CleanArchitecture.Infrastructure;
|
|
|
|
public sealed class UnitOfWork<TContext> : IUnitOfWork where TContext : DbContext
|
|
{
|
|
private readonly TContext _context;
|
|
private readonly ILogger<UnitOfWork<TContext>> _logger;
|
|
|
|
public UnitOfWork(TContext context, ILogger<UnitOfWork<TContext>> logger)
|
|
{
|
|
_context = context;
|
|
_logger = logger;
|
|
}
|
|
|
|
public async Task<bool> 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();
|
|
}
|
|
}
|
|
} |