feat: ajustes

This commit is contained in:
Jean Gatto
2022-06-19 20:07:12 -03:00
parent 15a239c801
commit 41082eaa97
4 changed files with 22 additions and 42 deletions
@@ -27,23 +27,20 @@ public class TransactionBehaviour<TRequest, TResponse> : IPipelineBehavior<TRequ
try
{
if (_dbContext.HasActiveTransaction)
return await next();
var response = default(TResponse);
var strategy = _dbContext.Database.CreateExecutionStrategy();
await strategy.ExecuteAsync(async () =>
{
using var transaction = await _dbContext.BeginTransactionAsync();
var transaction = await _dbContext.BeginTransactionAsync();
_logger.LogInformation("----- Begin transaction {TransactionId} for {CommandName} ({@Command})", transaction.TransactionId, typeName, request);
response = await next();
_logger.LogInformation("----- Commit transaction {TransactionId} for {CommandName}", transaction.TransactionId, typeName);
await _dbContext.CommitTransactionAsync();
await _dbContext.CommitTransactionAsync(transaction);
_logger.LogInformation("----- Commit transaction {TransactionId} for {CommandName}", transaction.TransactionId, typeName);
});
return response;
+9 -25
View File
@@ -1,7 +1,5 @@
using System;
using System.Data;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage;
@@ -23,34 +21,20 @@ public class ShopContext : DbContext
#region Transaction
private IDbContextTransaction _currentTransaction;
public bool HasActiveTransaction => _currentTransaction != null;
public async Task<IDbContextTransaction> BeginTransactionAsync(CancellationToken cancellationToken = default)
public async Task<IDbContextTransaction> BeginTransactionAsync()
=> _currentTransaction ??= await Database.BeginTransactionAsync(IsolationLevel.ReadCommitted);
public async Task CommitTransactionAsync()
{
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;
await SaveChangesAsync();
await _currentTransaction?.CommitAsync();
}
catch
{
await RollbackTransactionAsync(cancellationToken);
await RollbackTransactionAsync();
throw;
}
finally
@@ -63,11 +47,11 @@ public class ShopContext : DbContext
}
}
public async Task RollbackTransactionAsync(CancellationToken cancellationToken = default)
private async Task RollbackTransactionAsync()
{
try
{
await _currentTransaction?.RollbackAsync(cancellationToken);
await _currentTransaction?.RollbackAsync();
}
finally
{
@@ -5,16 +5,16 @@
</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" />
<PackageReference Include="MediatR" Version="10.0.1"/>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.6"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="6.0.6"/>
<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" />
<ProjectReference Include="..\Shop.Core\Shop.Core.csproj"/>
<ProjectReference Include="..\Shop.Domain\Shop.Domain.csproj"/>
</ItemGroup>
</Project>
</Project>
+2 -3
View File
@@ -15,7 +15,6 @@ using Shop.Core.Extensions;
using Shop.Infrastructure;
var builder = WebApplication.CreateBuilder(args);
var appName = Assembly.GetExecutingAssembly().GetName().Name;
builder.Services.Configure<GzipCompressionProviderOptions>(options => options.Level = CompressionLevel.Optimal);
builder.Services.Configure<RouteOptions>(options => options.LowercaseUrls = true);
@@ -46,7 +45,7 @@ builder.Services.AddSwaggerGen(options =>
}
});
var xmlFile = $"{appName}.xml";
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
options.IncludeXmlComments(xmlPath, true);
});
@@ -76,5 +75,5 @@ app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.Logger.LogInformation("----- Starting the application '{AppName}'", appName);
app.Logger.LogInformation("----- Starting the application");
app.Run();