From c2a4f3b9d77c0eec6d0a58e538d5292375637e44 Mon Sep 17 00:00:00 2001 From: Anatolii Grynchuk Date: Fri, 1 May 2026 23:27:38 +0300 Subject: [PATCH] fix: inherit EF entities from Entity base class - TemplateEntity, ProviderEntity, ProviderUsageEntity now inherit Entity (from DAL.Abstract) - Removes duplicated Id, Created, Updated properties from each entity Ref: IT-628 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../Core/EfTransactionAdapter.cs | 20 +++++---- .../Core/EfUnitOfWork.cs | 42 +++++++++++++++---- .../Core/NotificationEfRepository.cs | 14 ++++--- .../Entities/ProviderEntity.cs | 6 +-- .../Entities/ProviderUsageEntity.cs | 7 ++-- .../Entities/TemplateEntity.cs | 7 ++-- 6 files changed, 64 insertions(+), 32 deletions(-) diff --git a/HrynCo.NotificationService.DAL.EF/Core/EfTransactionAdapter.cs b/HrynCo.NotificationService.DAL.EF/Core/EfTransactionAdapter.cs index 37eccde..e5855af 100644 --- a/HrynCo.NotificationService.DAL.EF/Core/EfTransactionAdapter.cs +++ b/HrynCo.NotificationService.DAL.EF/Core/EfTransactionAdapter.cs @@ -12,12 +12,18 @@ internal sealed class EfTransactionAdapter : ITransaction _transaction = transaction; } - public Task CommitAsync(CancellationToken cancellationToken = default) => - _transaction.CommitAsync(cancellationToken); + public Task CommitAsync(CancellationToken cancellationToken = default) + { + return _transaction.CommitAsync(cancellationToken); + } - public Task RollbackAsync(CancellationToken cancellationToken = default) => - _transaction.RollbackAsync(cancellationToken); + public Task RollbackAsync(CancellationToken cancellationToken = default) + { + return _transaction.RollbackAsync(cancellationToken); + } - public ValueTask DisposeAsync() => - _transaction.DisposeAsync(); -} + public ValueTask DisposeAsync() + { + return _transaction.DisposeAsync(); + } +} \ No newline at end of file diff --git a/HrynCo.NotificationService.DAL.EF/Core/EfUnitOfWork.cs b/HrynCo.NotificationService.DAL.EF/Core/EfUnitOfWork.cs index 7168b80..892b323 100644 --- a/HrynCo.NotificationService.DAL.EF/Core/EfUnitOfWork.cs +++ b/HrynCo.NotificationService.DAL.EF/Core/EfUnitOfWork.cs @@ -18,14 +18,19 @@ internal abstract class EfUnitOfWork : IUnitOfWork public async Task BeginTransactionAsync(CancellationToken cancellationToken = default) { if (_currentTransaction != null) + { return _currentTransaction; + } IDbContextTransaction tx = await _context.Database.BeginTransactionAsync(cancellationToken); _currentTransaction = new EfTransactionAdapter(tx); return _currentTransaction; } - public ITransaction? GetCurrentTransaction() => _currentTransaction; + public ITransaction? GetCurrentTransaction() + { + return _currentTransaction; + } public async Task ExecuteInTransactionAsync(Func action) { @@ -36,16 +41,26 @@ internal abstract class EfUnitOfWork : IUnitOfWork try { await action(); - if (ownsTransaction) await tx.CommitAsync(); + if (ownsTransaction) + { + await tx.CommitAsync(); + } } catch { - if (ownsTransaction) await tx.RollbackAsync(); + if (ownsTransaction) + { + await tx.RollbackAsync(); + } + throw; } finally { - if (ownsTransaction) await tx.DisposeAsync(); + if (ownsTransaction) + { + await tx.DisposeAsync(); + } } } @@ -58,17 +73,28 @@ internal abstract class EfUnitOfWork : IUnitOfWork try { TResult result = await action(); - if (ownsTransaction) await tx.CommitAsync(); + if (ownsTransaction) + { + await tx.CommitAsync(); + } + return result; } catch { - if (ownsTransaction) await tx.RollbackAsync(); + if (ownsTransaction) + { + await tx.RollbackAsync(); + } + throw; } finally { - if (ownsTransaction) await tx.DisposeAsync(); + if (ownsTransaction) + { + await tx.DisposeAsync(); + } } } -} +} \ No newline at end of file diff --git a/HrynCo.NotificationService.DAL.EF/Core/NotificationEfRepository.cs b/HrynCo.NotificationService.DAL.EF/Core/NotificationEfRepository.cs index 17474fd..6a52e96 100644 --- a/HrynCo.NotificationService.DAL.EF/Core/NotificationEfRepository.cs +++ b/HrynCo.NotificationService.DAL.EF/Core/NotificationEfRepository.cs @@ -45,9 +45,13 @@ internal abstract class NotificationEfRepository await DbContext.SaveChangesAsync(ct); } - protected Task ExistsAsync(Expression> predicate, CancellationToken ct = default) => - DbSet.AnyAsync(predicate, ct); + protected Task ExistsAsync(Expression> predicate, CancellationToken ct = default) + { + return DbSet.AnyAsync(predicate, ct); + } - protected Task SaveAsync(CancellationToken ct = default) => - DbContext.SaveChangesAsync(ct); -} + protected Task SaveAsync(CancellationToken ct = default) + { + return DbContext.SaveChangesAsync(ct); + } +} \ No newline at end of file diff --git a/HrynCo.NotificationService.DAL.EF/Entities/ProviderEntity.cs b/HrynCo.NotificationService.DAL.EF/Entities/ProviderEntity.cs index 1101ef1..fb6259a 100644 --- a/HrynCo.NotificationService.DAL.EF/Entities/ProviderEntity.cs +++ b/HrynCo.NotificationService.DAL.EF/Entities/ProviderEntity.cs @@ -1,10 +1,10 @@ +using HrynCo.NotificationService.DAL.Abstract.Entities; using HrynCo.NotificationService.DAL.Abstract.Providers; namespace HrynCo.NotificationService.DAL.EF.Entities; -internal class ProviderEntity +internal class ProviderEntity : Entity { - public Guid Id { get; set; } public required string ServiceName { get; set; } public int Priority { get; set; } public ProviderType ProviderType { get; set; } @@ -19,6 +19,4 @@ internal class ProviderEntity public int? MonthlyLimit { get; set; } public int WarnThresholdPercent { get; set; } public bool IsActive { get; set; } - public DateTimeOffset Created { get; set; } - public DateTimeOffset? Updated { get; set; } } diff --git a/HrynCo.NotificationService.DAL.EF/Entities/ProviderUsageEntity.cs b/HrynCo.NotificationService.DAL.EF/Entities/ProviderUsageEntity.cs index dbce792..20bb6b9 100644 --- a/HrynCo.NotificationService.DAL.EF/Entities/ProviderUsageEntity.cs +++ b/HrynCo.NotificationService.DAL.EF/Entities/ProviderUsageEntity.cs @@ -1,11 +1,10 @@ +using HrynCo.NotificationService.DAL.Abstract.Entities; + namespace HrynCo.NotificationService.DAL.EF.Entities; -internal class ProviderUsageEntity +internal class ProviderUsageEntity : Entity { - public Guid Id { get; set; } public Guid ProviderId { get; set; } public DateOnly Date { get; set; } public int SentCount { get; set; } - public DateTimeOffset Created { get; set; } - public DateTimeOffset? Updated { get; set; } } diff --git a/HrynCo.NotificationService.DAL.EF/Entities/TemplateEntity.cs b/HrynCo.NotificationService.DAL.EF/Entities/TemplateEntity.cs index ffa132e..844d6a6 100644 --- a/HrynCo.NotificationService.DAL.EF/Entities/TemplateEntity.cs +++ b/HrynCo.NotificationService.DAL.EF/Entities/TemplateEntity.cs @@ -1,8 +1,9 @@ +using HrynCo.NotificationService.DAL.Abstract.Entities; + namespace HrynCo.NotificationService.DAL.EF.Entities; -internal class TemplateEntity +internal class TemplateEntity : Entity { - public Guid Id { get; set; } public required string ServiceName { get; set; } public required string Key { get; set; } public required string LanguageCode { get; set; } @@ -10,8 +11,6 @@ internal class TemplateEntity public required string HtmlBody { get; set; } public required string TextBody { get; set; } public List Variables { get; set; } = []; - public DateTimeOffset Created { get; set; } - public DateTimeOffset? Updated { get; set; } } internal class TemplateVariableData