Files
sanctions-search/src/SanctionsSearch.Worker/Persistence/EfUnitOfWork.cs
T

51 lines
1.4 KiB
C#
Raw Normal View History

2024-08-20 23:42:20 -05:00
namespace SanctionsSearch.Worker.Persistence;
2024-08-25 17:08:44 -05:00
class EfUnitOfWork(
DbContext context,
ILogger<EfUnitOfWork> logger,
ILoggerFactory loggerFactory
) : IUnitOfWork, IAsyncDisposable, IDisposable
2024-08-20 23:42:20 -05:00
{
2024-08-23 08:32:34 -05:00
private readonly DbContext _context = context;
2024-08-25 17:08:44 -05:00
private readonly ILogger<EfUnitOfWork> _logger = logger;
2024-08-20 23:42:20 -05:00
public ISdnRepository Sdns { get; } = new SdnRepository(context, loggerFactory.CreateLogger<SdnRepository>());
2024-08-22 16:04:41 -05:00
public IAddressRepository Addresses { get; } = new AddressRepository(context, loggerFactory.CreateLogger<AddressRepository>());
public IAliasRepository Aliases { get; } = new AliasRepository(context, loggerFactory.CreateLogger<AliasRepository>());
public ICommentRepository Comments { get; } = new CommentRepository(context, loggerFactory.CreateLogger<CommentRepository>());
2024-08-20 23:42:20 -05:00
public async Task SaveChangesAsync()
{
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateException ex)
{
_logger.LogError(ex, "Failed to save changes to the database.");
}
2024-08-20 23:42:20 -05:00
}
public async ValueTask DisposeAsync()
{
try
{
await _context.DisposeAsync();
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to dispose of the database context.");
}
2024-08-20 23:42:20 -05:00
}
public void Dispose()
{
try
{
_context.Dispose();
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to dispose of the database context.");
}
}
2024-08-20 23:42:20 -05:00
}