Skip to content

Commit

Permalink
Merge pull request #94 from marcwittke/develop
Browse files Browse the repository at this point in the history
IMiddleware Umstellung
  • Loading branch information
marcwittke committed Dec 24, 2019
2 parents 6c094e9 + d31ea64 commit 7e9fee2
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 148 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
using System.Data;
using Backend.Fx.Logging;

namespace Backend.Fx.Patterns.Transactions
{
public class ReadonlyDecorator : ITransactionContext
{
private static readonly ILogger Logger = LogManager.Create<ReadonlyDecorator>();

private readonly ITransactionContext _transactionContext;

public ReadonlyDecorator(ITransactionContext transactionContext)
{
Logger.Debug("Making this transaction context readonly");
_transactionContext = transactionContext;
}

public void BeginTransaction() => _transactionContext.BeginTransaction();

public void CommitTransaction()
{
Logger.Debug("Committing transaction is intercepted and replaced with rollback transaction to ensure readonly behavior");
RollbackTransaction();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,28 +53,33 @@ public void BeginTransaction()
{
if (_shouldHandleConnectionState)
{
Logger.Debug("Opening connection");
Connection.Open();
}

Logger.Debug("Beginning transaction");
CurrentTransaction = Connection.BeginTransaction();
_transactionLifetimeLogger = Logger.DebugDuration("Transaction open");
}

public void CommitTransaction()
{
Logger.Debug("Committing transaction");
CurrentTransaction.Commit();
CurrentTransaction.Dispose();
CurrentTransaction = null;
_transactionLifetimeLogger?.Dispose();
_transactionLifetimeLogger = null;
if (_shouldHandleConnectionState)
{
Logger.Debug("Closing connection");
Connection.Close();
}
}

public void RollbackTransaction()
{
Logger.Debug("rolling back transaction");
CurrentTransaction.Rollback();
CurrentTransaction.Dispose();
CurrentTransaction = null;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,21 @@

namespace Backend.Fx.AspNetCore.Scoping
{
public abstract class HoldCurrentMiddleware<T> where T : class
public abstract class HoldCurrentMiddleware<T> : IMiddleware where T : class
{
private readonly RequestDelegate _next;
private readonly IBackendFxApplication _application;

private readonly ICurrentTHolder<T> _currentTHolder;

[UsedImplicitly]
protected HoldCurrentMiddleware(RequestDelegate next, IBackendFxApplication application)
protected HoldCurrentMiddleware(ICurrentTHolder<T> currentTHolder)
{
_next = next;
_application = application;
_currentTHolder = currentTHolder;
}

/// <summary>
/// This method is being called by the previous middleware in the HTTP pipeline
/// </summary>
[UsedImplicitly]
public async Task Invoke(HttpContext context)
public async Task InvokeAsync(HttpContext context, RequestDelegate next)
{
T current = GetCurrent(context);
var tenantIdHolder = _application.CompositionRoot.GetInstance<ICurrentTHolder<T>>();
tenantIdHolder.ReplaceCurrent(current);
await _next.Invoke(context);
_currentTHolder.ReplaceCurrent(current);
await next.Invoke(context);
}

protected abstract T GetCurrent(HttpContext context);
Expand Down
36 changes: 0 additions & 36 deletions src/environments/Backend.Fx.AspNetCore/Scoping/ScopeMiddleware.cs

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,17 @@
{
using System.Reflection;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Primitives;

public class VersionHeaderMiddleware
public class VersionHeaderMiddleware : IMiddleware
{
private readonly RequestDelegate _next;
private readonly AssemblyName _entryAssemblyName = Assembly.GetEntryAssembly().GetName();

/// <summary>
/// This constructor is being called by the framework DI container
/// </summary>
[UsedImplicitly]
public VersionHeaderMiddleware(RequestDelegate next)
{
_next = next;
}

/// <summary>
/// This method is being called by the previous middleware in the HTTP pipeline
/// </summary>
[UsedImplicitly]
public async Task Invoke(HttpContext context)
public async Task InvokeAsync(HttpContext context, RequestDelegate next)
{
context.Response.Headers.Add(_entryAssemblyName.Name, new StringValues(_entryAssemblyName.Version.ToString(3)));
await _next.Invoke(context);
await next.Invoke(context);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ public static class DbContextExtensions
{
private static readonly ILogger Logger = LogManager.Create(typeof(DbContextExtensions));

public static void DisableChangeTracking(this DbContext dbContext)
{
Logger.Debug($"Disabling change tracking on {dbContext.GetType().Name} instance");
dbContext.ChangeTracker.AutoDetectChangesEnabled = false;
dbContext.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
}

public static void RegisterRowVersionProperty(this ModelBuilder modelBuilder)
{
modelBuilder.Model
Expand Down

0 comments on commit 7e9fee2

Please sign in to comment.