Skip to content

Commit

Permalink
Merge pull request #45 from marcwittke/hotfix/4.0.1
Browse files Browse the repository at this point in the history
FlushFilter added again. Still necesary because of MVC design
  • Loading branch information
marcwittke committed Sep 30, 2018
2 parents a023810 + 72560ce commit cd6c63e
Showing 1 changed file with 35 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Diagnostics;
using Backend.Fx.Patterns.DependencyInjection;
using Backend.Fx.Patterns.UnitOfWork;
using Microsoft.AspNetCore.Mvc.Filters;

namespace Backend.Fx.AspNetCore.Mvc.ErrorHandling
{
/// <summary>
/// Enforces flushing of db changes when an action was executed, so that possible schema violation errors
/// are raised before the http response starts, and our exception handling can still change the response.
/// </summary>
/// <remarks>This cannot be done in a middleware. MVC will start the response as soon as the controller
/// action returns. The <see cref="Backend.Fx.AspNetCore.Scoping.UnitOfWorkMiddleware"/> would commit changes
/// too late for the <see cref="Backend.Fx.AspNetCore.ErrorHandling.JsonErrorHandlingMiddleware"/> to
/// manipulate the response status.</remarks>
[DebuggerStepThrough]
public class FlushFilter : IActionFilter
{
private readonly IBackendFxApplication _backendFxApplication;

public FlushFilter(IBackendFxApplication backendFxApplication)
{
_backendFxApplication = backendFxApplication;
}

public void OnActionExecuting(ActionExecutingContext context)
{ }

public void OnActionExecuted(ActionExecutedContext context)
{
// that's all:
_backendFxApplication.CompositionRoot.GetInstance<ICanFlush>().Flush();
}
}
}

0 comments on commit cd6c63e

Please sign in to comment.