-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMutationRequestVersionResolutionManager.cs
More file actions
56 lines (47 loc) · 2.4 KB
/
Copy pathMutationRequestVersionResolutionManager.cs
File metadata and controls
56 lines (47 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using ModularityKit.Mutator.Abstractions.Context;
using ModularityKit.Mutator.Governance.Abstractions.Exceptions.Storage;
using ModularityKit.Mutator.Governance.Abstractions.Resolution.Contracts;
using ModularityKit.Mutator.Governance.Abstractions.Resolution.Model;
using ModularityKit.Mutator.Governance.Abstractions.Resolution.Strategies;
using ModularityKit.Mutator.Governance.Abstractions.Storage;
namespace ModularityKit.Mutator.Governance.Runtime.Resolution.Execution;
/// <summary>
/// Resolves governed requests against the current state version and persists the resulting resolution outcome.
/// </summary>
public sealed class MutationRequestVersionResolutionManager(
IMutationRequestStore requestStore,
IMutationRequestVersionResolver versionResolver) : IMutationRequestVersionResolutionManager
{
private readonly IMutationRequestStore _requestStore = requestStore ?? throw new ArgumentNullException(nameof(requestStore));
private readonly IMutationRequestVersionResolver _versionResolver = versionResolver ?? throw new ArgumentNullException(nameof(versionResolver));
/// <summary>
/// Loads a persisted request, resolves it using version-aware governance semantics, and stores the resulting request revision.
/// </summary>
public async Task<MutationRequestVersionResolution> ResolveAndStore(
string requestId,
string currentStateVersion,
MutationContext resolutionContext,
VersionedRequestResolutionStrategy strategy,
CancellationToken cancellationToken = default)
{
if (string.IsNullOrWhiteSpace(requestId))
throw new ArgumentException("Request ID is required.", nameof(requestId));
var request = await _requestStore.Get(requestId, cancellationToken).ConfigureAwait(false);
if (request is null)
throw new MutationRequestNotFoundException(requestId);
var resolution = _versionResolver.Resolve(
request,
currentStateVersion,
resolutionContext,
strategy);
var persistedRequest = await _requestStore
.TryStore(resolution.Request, request.Revision, cancellationToken)
.ConfigureAwait(false);
if (persistedRequest is null)
throw new MutationRequestConcurrencyException(request.RequestId, request.Revision);
return resolution with
{
Request = persistedRequest
};
}
}