-
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.
- Loading branch information
1 parent
c94238e
commit 4145ac2
Showing
15 changed files
with
187 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
namespace Cross.CQRS.EF.Behaviors; | ||
|
||
internal sealed class ScopeBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse> | ||
where TRequest : class, IRequest<TResponse> | ||
{ | ||
private readonly IDbContextProvider _dbContextProvider; | ||
private readonly IHandlerLocator _handlerLocator; | ||
|
||
public ScopeBehavior(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(); | ||
} | ||
} | ||
|
||
TResponse response = default; | ||
using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) | ||
{ | ||
response = await next(); | ||
scope.Complete(); | ||
} | ||
|
||
var dbContext = _dbContextProvider.Get(); | ||
|
||
// Clean-up tracked entries | ||
var trackedEntries = dbContext.ChangeTracker.Entries() | ||
.Where(e => e.State is EntityState.Added or EntityState.Modified or EntityState.Deleted) | ||
.ToList(); | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Cross.CQRS.EF.Enums; | ||
|
||
public enum TransactionBehaviorEnum | ||
{ | ||
TransactionalBehavior = 1, | ||
ScopeBehavior = 2, | ||
} |
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
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 |
---|---|---|
@@ -1,5 +1,3 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace SampleWebApp.Infrastructure; | ||
|
||
public class Context : DbContext | ||
|
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,3 @@ | ||
namespace SampleWebApp.Modules.Some.Handlers; | ||
|
||
public record ExternalEvent(Guid CommandId, string Message) : IEvent; |
11 changes: 11 additions & 0 deletions
11
SampleWebApp/Modules/Some/Handlers/ExternalEventHandler.cs
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,11 @@ | ||
namespace SampleWebApp.Modules.Some.Handlers; | ||
|
||
public class ExternalEventHandler : Cross.CQRS.Events.EventHandler<ExternalEvent> | ||
{ | ||
protected override Task HandleAsync(ExternalEvent ev, CancellationToken cancellationToken) | ||
{ | ||
Console.WriteLine($"{nameof(ExternalEventHandler)} with message '{ev.Message}'"); | ||
|
||
return Task.CompletedTask; | ||
} | ||
} |
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,3 @@ | ||
namespace SampleWebApp.Modules.Some.Handlers; | ||
|
||
public record InternalEvent(Guid CommandId, string Message) : IEvent; |
11 changes: 11 additions & 0 deletions
11
SampleWebApp/Modules/Some/Handlers/InternalEventHandler.cs
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,11 @@ | ||
namespace SampleWebApp.Modules.Some.Handlers; | ||
|
||
public class InternalEventHandler : Cross.CQRS.Events.EventHandler<InternalEvent> | ||
{ | ||
protected override Task HandleAsync(InternalEvent ev, CancellationToken cancellationToken) | ||
{ | ||
Console.WriteLine($"{nameof(InternalEventHandler)} with message '{ev.Message}'"); | ||
|
||
return Task.CompletedTask; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
SampleWebApp/Modules/Some/Handlers/SomeScopeExternalCommand.cs
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,8 @@ | ||
namespace SampleWebApp.Modules.Some.Handlers; | ||
|
||
public class SomeScopeExternalCommand : Command | ||
{ | ||
public SomeScopeExternalCommand() | ||
{ | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
SampleWebApp/Modules/Some/Handlers/SomeScopeExternalCommandHandler.cs
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,20 @@ | ||
namespace SampleWebApp.Modules.Some.Handlers; | ||
|
||
public class SomeScopeExternalCommandHandler : CommandHandler<SomeScopeExternalCommand> | ||
{ | ||
|
||
public SomeScopeExternalCommandHandler(IEventQueueWriter eventQueueWriter) | ||
: base(eventQueueWriter) | ||
{ | ||
} | ||
|
||
protected override Task HandleAsync(SomeScopeExternalCommand command, CancellationToken cancellationToken) | ||
{ | ||
Events.Write(new ExternalEvent(command.CommandId, $"hello from {nameof(SomeScopeExternalCommandHandler)}")); | ||
|
||
Console.WriteLine($"{nameof(SomeScopeExternalCommandHandler)} do something"); | ||
|
||
// do nothing | ||
return Task.CompletedTask; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
SampleWebApp/Modules/Some/Handlers/SomeScopeInternalCommand.cs
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,10 @@ | ||
namespace SampleWebApp.Modules.Some.Handlers; | ||
|
||
|
||
|
||
public class SomeScopeInternalCommand : Command | ||
{ | ||
public SomeScopeInternalCommand() | ||
{ | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
SampleWebApp/Modules/Some/Handlers/SomeScopeInternalCommandHandler.cs
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,20 @@ | ||
namespace SampleWebApp.Modules.Some.Handlers; | ||
|
||
public class SomeScopeInternalCommandHandler : CommandHandler<SomeScopeExternalCommand> | ||
{ | ||
|
||
public SomeScopeInternalCommandHandler(IEventQueueWriter eventQueueWriter) | ||
: base(eventQueueWriter) | ||
{ | ||
} | ||
|
||
protected override Task HandleAsync(SomeScopeExternalCommand command, CancellationToken cancellationToken) | ||
{ | ||
Events.Write(new InternalEvent(command.CommandId, $"hello from {nameof(SomeScopeInternalCommandHandler)}")); | ||
|
||
Console.WriteLine($"{nameof(SomeScopeInternalCommandHandler)} do something"); | ||
|
||
// do nothing | ||
return Task.CompletedTask; | ||
} | ||
} |
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