-
Notifications
You must be signed in to change notification settings - Fork 3
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
ccec6d1
commit fc1872c
Showing
6 changed files
with
57 additions
and
54 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
40 changes: 0 additions & 40 deletions
40
...rten.Infrastructure/Persistence/IdempotentNotifications/IIdempotentNotificationContext.cs
This file was deleted.
Oops, something went wrong.
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
43 changes: 43 additions & 0 deletions
43
...frastructure/Persistence/IdempotentNotifications/INotificationProcessingContextFactory.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,43 @@ | ||
using System.Collections.Concurrent; | ||
using Digdir.Domain.Dialogporten.Domain.Common.EventPublisher; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Digdir.Domain.Dialogporten.Infrastructure.Persistence.IdempotentNotifications; | ||
|
||
public interface INotificationProcessingContextFactory | ||
{ | ||
Task<INotificationProcessingContext> CreateContext(IDomainEvent domainEvent, bool isFirstAttempt = false, CancellationToken cancellationToken = default); | ||
INotificationProcessingContext GetExistingContext(Guid eventId); | ||
} | ||
|
||
internal sealed class NotificationProcessingContextFactory : INotificationProcessingContextFactory | ||
{ | ||
private readonly ConcurrentDictionary<Guid, NotificationProcessingContext> _contextByEventId = new(); | ||
private readonly IServiceScopeFactory _serviceScopeFactory; | ||
|
||
public NotificationProcessingContextFactory(IServiceScopeFactory serviceScopeFactory) | ||
{ | ||
_serviceScopeFactory = serviceScopeFactory ?? throw new ArgumentNullException(nameof(serviceScopeFactory)); | ||
} | ||
|
||
public async Task<INotificationProcessingContext> CreateContext( | ||
IDomainEvent domainEvent, | ||
bool isFirstAttempt = false, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
var transaction = _contextByEventId.GetOrAdd( | ||
key: domainEvent.EventId, | ||
valueFactory: eventId => new(_serviceScopeFactory, eventId, onDispose: RemoveTransaction)); | ||
await transaction.Initialize(isFirstAttempt, cancellationToken); | ||
return transaction; | ||
} | ||
|
||
public INotificationProcessingContext GetExistingContext(Guid eventId) | ||
{ | ||
return _contextByEventId.TryGetValue(eventId, out var transaction) | ||
? transaction | ||
: throw new InvalidOperationException("Notification context not found."); | ||
} | ||
|
||
private void RemoveTransaction(Guid eventId) => _contextByEventId.TryRemove(eventId, out _); | ||
} |
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