refactor: replace local DAL abstractions with hrynco-ef packages #2
@@ -29,6 +29,8 @@
|
||||
<!-- HrynCo shared packages -->
|
||||
<PackageVersion Include="HrynCo.Common" Version="1.0.11" />
|
||||
<PackageVersion Include="HrynCo.RabbitMq" Version="1.0.15" />
|
||||
<PackageVersion Include="HrynCo.DAL.Abstract" Version="1.0.1" />
|
||||
<PackageVersion Include="HrynCo.DAL.EF" Version="1.0.1" />
|
||||
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
|
||||
<PackageVersion Include="coverlet.collector" Version="6.0.4" />
|
||||
<PackageVersion Include="xunit" Version="2.9.3" />
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
namespace HrynCo.NotificationService.DAL.Abstract.Entities;
|
||||
|
||||
public abstract class Entity<TId> : IEntity<TId> where TId : struct
|
||||
{
|
||||
public TId Id { get; set; }
|
||||
public DateTimeOffset Created { get; set; }
|
||||
public DateTimeOffset? Updated { get; set; }
|
||||
}
|
||||
|
||||
public abstract class Entity : Entity<Guid>
|
||||
{
|
||||
protected Entity()
|
||||
{
|
||||
Id = Guid.NewGuid();
|
||||
Created = DateTimeOffset.UtcNow;
|
||||
}
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
namespace HrynCo.NotificationService.DAL.Abstract.Entities;
|
||||
|
||||
public interface IEntity<TId> where TId : struct
|
||||
{
|
||||
TId Id { get; set; }
|
||||
DateTimeOffset Created { get; set; }
|
||||
DateTimeOffset? Updated { get; set; }
|
||||
}
|
||||
@@ -1,5 +1,9 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="HrynCo.DAL.Abstract" />
|
||||
</ItemGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
namespace HrynCo.NotificationService.DAL.Abstract;
|
||||
|
||||
public interface ITransaction : IAsyncDisposable
|
||||
{
|
||||
Task CommitAsync(CancellationToken cancellationToken = default);
|
||||
Task RollbackAsync(CancellationToken cancellationToken = default);
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
namespace HrynCo.NotificationService.DAL.Abstract;
|
||||
|
||||
public interface IUnitOfWork
|
||||
{
|
||||
Task SaveChangesAsync(CancellationToken cancellationToken = default);
|
||||
Task<ITransaction> BeginTransactionAsync(CancellationToken cancellationToken = default);
|
||||
ITransaction? GetCurrentTransaction();
|
||||
|
||||
Task ExecuteInTransactionAsync(Func<Task> action);
|
||||
Task<TResult> ExecuteInTransactionAsync<TResult>(Func<Task<TResult>> action);
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract.Entities;
|
||||
using HrynCo.DAL.Abstract.Entities;
|
||||
|
||||
namespace HrynCo.NotificationService.DAL.Abstract.Providers;
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract.Entities;
|
||||
using HrynCo.DAL.Abstract.Entities;
|
||||
|
||||
namespace HrynCo.NotificationService.DAL.Abstract.Providers;
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract.Entities;
|
||||
using HrynCo.DAL.Abstract.Entities;
|
||||
|
||||
namespace HrynCo.NotificationService.DAL.Abstract.Templates;
|
||||
|
||||
|
||||
@@ -1,45 +0,0 @@
|
||||
using System.Linq.Expressions;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace HrynCo.NotificationService.DAL.EF.Core;
|
||||
|
||||
internal abstract class EfRepository<TEntity>
|
||||
where TEntity : class
|
||||
{
|
||||
protected NotificationDbContext DbContext { get; }
|
||||
protected DbSet<TEntity> DbSet { get; }
|
||||
|
||||
protected EfRepository(NotificationDbContext dbContext)
|
||||
{
|
||||
DbContext = dbContext;
|
||||
DbSet = dbContext.Set<TEntity>();
|
||||
}
|
||||
|
||||
protected async Task AddAsync(TEntity entity, CancellationToken ct = default)
|
||||
{
|
||||
await DbSet.AddAsync(entity, ct);
|
||||
}
|
||||
|
||||
protected async Task AddRangeAsync(IEnumerable<TEntity> entities, CancellationToken ct = default)
|
||||
{
|
||||
await DbSet.AddRangeAsync(entities, ct);
|
||||
}
|
||||
|
||||
protected void Update(TEntity entity)
|
||||
{
|
||||
DbSet.Update(entity);
|
||||
}
|
||||
|
||||
protected void Delete(TEntity entity)
|
||||
{
|
||||
DbSet.Remove(entity);
|
||||
}
|
||||
|
||||
protected void DeleteRange(IEnumerable<TEntity> entities)
|
||||
{
|
||||
DbSet.RemoveRange(entities);
|
||||
}
|
||||
|
||||
protected Task<bool> ExistsAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken ct = default) =>
|
||||
DbSet.AnyAsync(predicate, ct);
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract;
|
||||
using Microsoft.EntityFrameworkCore.Storage;
|
||||
|
||||
namespace HrynCo.NotificationService.DAL.EF.Core;
|
||||
|
||||
internal sealed class EfTransactionAdapter : ITransaction
|
||||
{
|
||||
private readonly IDbContextTransaction _transaction;
|
||||
|
||||
internal EfTransactionAdapter(IDbContextTransaction transaction)
|
||||
{
|
||||
_transaction = transaction;
|
||||
}
|
||||
|
||||
public Task CommitAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
return _transaction.CommitAsync(cancellationToken);
|
||||
}
|
||||
|
||||
public Task RollbackAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
return _transaction.RollbackAsync(cancellationToken);
|
||||
}
|
||||
|
||||
public ValueTask DisposeAsync()
|
||||
{
|
||||
return _transaction.DisposeAsync();
|
||||
}
|
||||
}
|
||||
@@ -1,105 +0,0 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Storage;
|
||||
|
||||
namespace HrynCo.NotificationService.DAL.EF.Core;
|
||||
|
||||
internal abstract class EfUnitOfWork<TDbContext> : IUnitOfWork
|
||||
where TDbContext : DbContext
|
||||
{
|
||||
private readonly TDbContext _context;
|
||||
private EfTransactionAdapter? _currentTransaction;
|
||||
|
||||
protected EfUnitOfWork(TDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public Task SaveChangesAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
return _context.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<ITransaction> 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()
|
||||
{
|
||||
return _currentTransaction;
|
||||
}
|
||||
|
||||
public async Task ExecuteInTransactionAsync(Func<Task> action)
|
||||
{
|
||||
ITransaction? existing = GetCurrentTransaction();
|
||||
bool ownsTransaction = existing is null;
|
||||
ITransaction tx = existing ?? await BeginTransactionAsync();
|
||||
|
||||
try
|
||||
{
|
||||
await action();
|
||||
if (ownsTransaction)
|
||||
{
|
||||
await tx.CommitAsync();
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
if (ownsTransaction)
|
||||
{
|
||||
await tx.RollbackAsync();
|
||||
}
|
||||
|
||||
throw;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (ownsTransaction)
|
||||
{
|
||||
await tx.DisposeAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<TResult> ExecuteInTransactionAsync<TResult>(Func<Task<TResult>> action)
|
||||
{
|
||||
ITransaction? existing = GetCurrentTransaction();
|
||||
bool ownsTransaction = existing is null;
|
||||
ITransaction tx = existing ?? await BeginTransactionAsync();
|
||||
|
||||
try
|
||||
{
|
||||
TResult result = await action();
|
||||
if (ownsTransaction)
|
||||
{
|
||||
await tx.CommitAsync();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
catch
|
||||
{
|
||||
if (ownsTransaction)
|
||||
{
|
||||
await tx.RollbackAsync();
|
||||
}
|
||||
|
||||
throw;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (ownsTransaction)
|
||||
{
|
||||
await tx.DisposeAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
using HrynCo.DAL.EF.Core;
|
||||
|
||||
namespace HrynCo.NotificationService.DAL.EF.Core;
|
||||
|
||||
internal sealed class UnitOfWork : EfUnitOfWork<NotificationDbContext>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract.Entities;
|
||||
using HrynCo.DAL.Abstract.Entities;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Providers;
|
||||
|
||||
namespace HrynCo.NotificationService.DAL.EF.Entities;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract.Entities;
|
||||
using HrynCo.DAL.Abstract.Entities;
|
||||
|
||||
namespace HrynCo.NotificationService.DAL.EF.Entities;
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract.Entities;
|
||||
using HrynCo.DAL.Abstract.Entities;
|
||||
|
||||
namespace HrynCo.NotificationService.DAL.EF.Entities;
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="HrynCo.DAL.EF" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
using System.Text.Json;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Providers;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||
using HrynCo.NotificationService.DAL.EF.Core;
|
||||
using HrynCo.DAL.EF.Core;
|
||||
using HrynCo.NotificationService.DAL.EF.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace HrynCo.NotificationService.DAL.EF.Repositories;
|
||||
|
||||
internal sealed class EmailChannelRepository : EfRepository<EmailChannelEntity>, IEmailChannelRepository
|
||||
internal sealed class EmailChannelRepository : EfRepository<NotificationDbContext, EmailChannelEntity>, IEmailChannelRepository
|
||||
{
|
||||
public EmailChannelRepository(NotificationDbContext dbContext) : base(dbContext)
|
||||
{
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||
using HrynCo.NotificationService.DAL.EF.Core;
|
||||
using HrynCo.DAL.EF.Core;
|
||||
using HrynCo.NotificationService.DAL.EF.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace HrynCo.NotificationService.DAL.EF.Repositories;
|
||||
|
||||
internal sealed class EmailChannelUsageRepository : EfRepository<EmailChannelUsageEntity>, IEmailChannelUsageRepository
|
||||
internal sealed class EmailChannelUsageRepository : EfRepository<NotificationDbContext, EmailChannelUsageEntity>, IEmailChannelUsageRepository
|
||||
{
|
||||
public EmailChannelUsageRepository(NotificationDbContext dbContext) : base(dbContext)
|
||||
{
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Templates;
|
||||
using HrynCo.NotificationService.DAL.EF.Core;
|
||||
using HrynCo.DAL.EF.Core;
|
||||
using HrynCo.NotificationService.DAL.EF.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace HrynCo.NotificationService.DAL.EF.Repositories;
|
||||
|
||||
internal sealed class EmailTemplateRepository : EfRepository<EmailTemplateEntity>, IEmailTemplateRepository
|
||||
internal sealed class EmailTemplateRepository : EfRepository<NotificationDbContext, EmailTemplateEntity>, IEmailTemplateRepository
|
||||
{
|
||||
public EmailTemplateRepository(NotificationDbContext dbContext) : base(dbContext)
|
||||
{
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract;
|
||||
using HrynCo.DAL.Abstract;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||
using HrynCo.NotificationService.DAL.EF.Core;
|
||||
using HrynCo.NotificationService.DAL.EF.Repositories;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
using HrynCo.Common;
|
||||
using HrynCo.NotificationService.DAL.Abstract;
|
||||
using HrynCo.DAL.Abstract;
|
||||
using MediatR;
|
||||
|
||||
namespace HrynCo.NotificationService.Services.Behaviors;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract;
|
||||
using HrynCo.DAL.Abstract;
|
||||
using HrynCo.NotificationService.Services.Logging;
|
||||
using MediatR;
|
||||
using Serilog;
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract;
|
||||
using HrynCo.DAL.Abstract;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Providers;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||
using HrynCo.NotificationService.Services.Core;
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract;
|
||||
using HrynCo.DAL.Abstract;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||
using HrynCo.NotificationService.Services.Core;
|
||||
using HrynCo.NotificationService.Services.Logging;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract;
|
||||
using HrynCo.DAL.Abstract;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Providers;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||
using HrynCo.NotificationService.Services.Core;
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract;
|
||||
using HrynCo.DAL.Abstract;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Providers;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||
using HrynCo.NotificationService.Services.Core;
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract;
|
||||
using HrynCo.DAL.Abstract;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Providers;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||
using HrynCo.NotificationService.Services.Core;
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract;
|
||||
using HrynCo.DAL.Abstract;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||
using HrynCo.NotificationService.Services.Core;
|
||||
using HrynCo.NotificationService.Services.Logging;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
using System.Net;
|
||||
using System.Net.Mail;
|
||||
using HrynCo.NotificationService.DAL.Abstract;
|
||||
using HrynCo.DAL.Abstract;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Providers;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||
using HrynCo.NotificationService.Services.Core;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
using System.Net;
|
||||
using System.Net.Mail;
|
||||
using HrynCo.NotificationService.DAL.Abstract;
|
||||
using HrynCo.DAL.Abstract;
|
||||
using HrynCo.NotificationService.Services.Core;
|
||||
using HrynCo.NotificationService.Services.Logging;
|
||||
using static HrynCo.NotificationService.Services.Core.ServiceResultHelper;
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract;
|
||||
using HrynCo.DAL.Abstract;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||
using HrynCo.NotificationService.Services.Core;
|
||||
using HrynCo.NotificationService.Services.Logging;
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract;
|
||||
using HrynCo.DAL.Abstract;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Templates;
|
||||
using HrynCo.NotificationService.Services.Core;
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract;
|
||||
using HrynCo.DAL.Abstract;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||
using HrynCo.NotificationService.Services.Core;
|
||||
using HrynCo.NotificationService.Services.Logging;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract;
|
||||
using HrynCo.DAL.Abstract;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Templates;
|
||||
using HrynCo.NotificationService.Services.Core;
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract;
|
||||
using HrynCo.DAL.Abstract;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Templates;
|
||||
using HrynCo.NotificationService.Services.Core;
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract;
|
||||
using HrynCo.DAL.Abstract;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Templates;
|
||||
using HrynCo.NotificationService.Services.Core;
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract;
|
||||
using HrynCo.DAL.Abstract;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||
using HrynCo.NotificationService.Services.Core;
|
||||
using HrynCo.NotificationService.Services.Logging;
|
||||
|
||||
Reference in New Issue
Block a user