Created
July 5, 2020 17:04
-
-
Save emrekizildas/d5a3d62e954e9c209baec83eb6865919 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ResilientTransaction | |
{ | |
private DbContext _context; | |
private ResilientTransaction(DbContext context) => | |
_context = context ?? throw new ArgumentNullException(nameof(context)); | |
public static ResilientTransaction New (DbContext context) => | |
new ResilientTransaction(context); | |
public async Task ExecuteAsync(Func<Task> action) | |
{ | |
var strategy = _context.Database.CreateExecutionStrategy(); | |
await strategy.ExecuteAsync(async () => | |
{ | |
using (var transaction = _context.Database.BeginTransaction()) | |
{ | |
await action(); | |
transaction.Commit(); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment