-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Release/add new transactional scope behavior (#6)
* add new TransactionalScopeBehavior * add new TransactionalScopeBehavior
- Loading branch information
1 parent
4145ac2
commit d77551b
Showing
4 changed files
with
73 additions
and
2 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
namespace Cross.CQRS.EF.Behaviors; | ||
|
||
internal sealed class TransactionalScopeBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse> | ||
where TRequest : class, IRequest<TResponse> | ||
{ | ||
private readonly IDbContextProvider _dbContextProvider; | ||
private readonly IHandlerLocator _handlerLocator; | ||
|
||
public TransactionalScopeBehavior(IHandlerLocator handlerLocator, IDbContextProvider dbContextProvider) | ||
{ | ||
_handlerLocator = handlerLocator; | ||
_dbContextProvider = dbContextProvider; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken) | ||
{ | ||
if (request is not ICommand<TResponse>) | ||
{ | ||
return await next(); | ||
} | ||
|
||
var handler = _handlerLocator.FindHandlerTypeByRequest(typeof(TRequest)); | ||
if (handler != null) | ||
{ | ||
var isExplicitTransactionSet = handler | ||
.GetCustomAttributes(typeof(ExplicitTransactionAttribute), inherit: false) | ||
.Any(); | ||
|
||
if (isExplicitTransactionSet) | ||
{ | ||
// Skip behavior if requested explicit transaction management. | ||
return await next(); | ||
} | ||
} | ||
|
||
var dbContext = _dbContextProvider.Get(); | ||
var executionStrategy = dbContext.Database.CreateExecutionStrategy(); | ||
|
||
TResponse response = default; | ||
|
||
await executionStrategy.ExecuteAsync(async () => | ||
{ | ||
using var transactionScope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled); | ||
response = await next(); | ||
transactionScope.Complete(); | ||
}); | ||
|
||
// Clean-up tracked entries | ||
var trackedEntries = dbContext.ChangeTracker.Entries() | ||
.Where(e => e.State is EntityState.Added or EntityState.Modified or EntityState.Deleted) | ||
.ToArray(); | ||
|
||
foreach (var entry in trackedEntries) | ||
{ | ||
entry.State = EntityState.Detached; | ||
} | ||
|
||
return response; | ||
} | ||
} |
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
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