feat: adicionado DbContext com o esquema de transação
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
using System;
|
||||
using System.Data;
|
||||
using System.Reflection;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Storage;
|
||||
|
||||
namespace Shop.Infrastructure.Data;
|
||||
|
||||
public class ShopContext : DbContext
|
||||
{
|
||||
public ShopContext(DbContextOptions<ShopContext> options) : base(options)
|
||||
{
|
||||
ChangeTracker.LazyLoadingEnabled = false;
|
||||
}
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
modelBuilder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());
|
||||
}
|
||||
|
||||
#region Transaction
|
||||
|
||||
private IDbContextTransaction _currentTransaction;
|
||||
public bool HasActiveTransaction => _currentTransaction != null;
|
||||
|
||||
public async Task<IDbContextTransaction> BeginTransactionAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (_currentTransaction != null) return null;
|
||||
|
||||
_currentTransaction = await Database.BeginTransactionAsync(IsolationLevel.ReadCommitted, cancellationToken);
|
||||
|
||||
return _currentTransaction;
|
||||
}
|
||||
|
||||
public async Task<int> CommitTransactionAsync(IDbContextTransaction transaction, CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (transaction == null)
|
||||
throw new ArgumentNullException(nameof(transaction));
|
||||
|
||||
if (transaction != _currentTransaction)
|
||||
throw new InvalidOperationException($"Transaction {transaction.TransactionId} is not current");
|
||||
|
||||
try
|
||||
{
|
||||
var rowsAffected = await SaveChangesAsync(cancellationToken);
|
||||
await transaction.CommitAsync(cancellationToken);
|
||||
return rowsAffected;
|
||||
}
|
||||
catch
|
||||
{
|
||||
await RollbackTransactionAsync(cancellationToken);
|
||||
throw;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (_currentTransaction != null)
|
||||
{
|
||||
_currentTransaction.Dispose();
|
||||
_currentTransaction = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task RollbackTransactionAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _currentTransaction?.RollbackAsync(cancellationToken);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (_currentTransaction != null)
|
||||
{
|
||||
_currentTransaction.Dispose();
|
||||
_currentTransaction = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
using MediatR;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Shop.Infrastructure.Behaviors;
|
||||
|
||||
namespace Shop.Infrastructure;
|
||||
|
||||
public static class ServicesCollectionExtensions
|
||||
{
|
||||
public static IServiceCollection AddInfrastructure(this IServiceCollection serivces)
|
||||
{
|
||||
serivces.AddScoped(typeof(IPipelineBehavior<,>), typeof(LoggingBehavior<,>));
|
||||
serivces.AddScoped(typeof(IPipelineBehavior<,>), typeof(TransactionBehaviour<,>));
|
||||
return serivces;
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,14 @@
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="MediatR" Version="10.0.1" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.5" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="6.0.5" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="6.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Shop.Core\Shop.Core.csproj" />
|
||||
<ProjectReference Include="..\Shop.Domain\Shop.Domain.csproj" />
|
||||
|
||||
Reference in New Issue
Block a user