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()
|
|
|
|
|
{
|
2024-08-24 16:06:44 -05:00
|
|
|
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()
|
|
|
|
|
{
|
2024-08-24 16:06:44 -05:00
|
|
|
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
|
|
|
}
|
2024-08-25 16:31:12 -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
|
|
|
}
|