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

47 lines
1.4 KiB
C#
Raw Normal View History

2024-08-20 23:42:20 -05:00
namespace SanctionsSearch.Worker.Persistence;
class EfUnitOfWork(DbContext context, 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;
private readonly ILogger<EfUnitOfWork> _logger = loggerFactory.CreateLogger<EfUnitOfWork>();
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
}