Guest User

Untitled

a guest
Jun 18th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. using System;
  2. using System.Data;
  3. using System.Threading.Tasks;
  4. using Microsoft.AspNetCore.Mvc.Filters;
  5.  
  6. namespace txn {
  7. public class UnitOfWorkFilter : IAsyncActionFilter {
  8. private readonly IDbTransaction transaction;
  9.  
  10. public UnitOfWorkFilter (IDbTransaction transaction) {
  11. this.transaction = transaction;
  12. }
  13.  
  14. public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) {
  15. var connection = transaction.Connection;
  16. if (connection.State != ConnectionState.Open)
  17. throw new NotSupportedException ("The provided connection was not open!");
  18.  
  19. var executedContext = await next.Invoke ();
  20. if (executedContext.Exception == null) {
  21. transaction.Commit ();
  22. } else {
  23. transaction.Rollback ();
  24. }
  25. }
  26. }
  27. }
Add Comment
Please, Sign In to add comment