Skip to content

Commit 10f2673

Browse files
refactor: clean up logic a little bit
repository just passes in previous version and next version, method will do the math so that it only has to exist in one location
1 parent c3218c2 commit 10f2673

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

src/EntityDb.Common/Exceptions/OptimisticConcurrencyException.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ namespace EntityDb.Common.Exceptions
77
/// <summary>
88
/// The exception that is logged when an actor passes a <see cref="ITransaction{TEntity}" /> to an
99
/// <see cref="ITransactionRepository{TEntity}" /> with a
10-
/// <see cref="ITransactionCommand{TEntity}.ExpectedPreviousVersionNumber" /> unequal to the actual previous version
11-
/// number.
10+
/// <see cref="ITransactionCommand{TEntity}.EntityVersionNumber" /> that is not the next number
11+
/// after the previous version number.
1212
/// </summary>
1313
/// <remarks>
1414
/// A program will not be able to catch this exception if it is thrown.
@@ -18,14 +18,14 @@ namespace EntityDb.Common.Exceptions
1818
public sealed class OptimisticConcurrencyException : Exception
1919
{
2020
/// <summary>
21-
/// Throws a new <see cref="OptimisticConcurrencyException" /> if <paramref name="expectedPreviousVersionNumber" /> is
22-
/// not equal to <paramref name="actualPreviousVersionNumber" />.
21+
/// Throws a new <see cref="OptimisticConcurrencyException" /> if <paramref name="nextVersionNumber" />
22+
/// is not the next number after <paramref name="previousVersionNumber" />.
2323
/// </summary>
24-
/// <param name="expectedPreviousVersionNumber"></param>
25-
/// <param name="actualPreviousVersionNumber"></param>
26-
public static void ThrowIfMismatch(ulong expectedPreviousVersionNumber, ulong actualPreviousVersionNumber)
24+
/// <param name="previousVersionNumber">The previous version number.</param>
25+
/// <param name="nextVersionNumber">The next version number.</param>
26+
public static void ThrowIfDiscontinuous(ulong previousVersionNumber, ulong nextVersionNumber)
2727
{
28-
if (expectedPreviousVersionNumber != actualPreviousVersionNumber)
28+
if (nextVersionNumber != previousVersionNumber + 1)
2929
{
3030
throw new OptimisticConcurrencyException();
3131
}

src/EntityDb.MongoDb/Transactions/MongoDbTransactionRepository.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,13 @@ await SourceDocument.InsertOne(clientSessionHandle, mongoDatabase,
9292

9393
foreach (var transactionCommand in transaction.Commands)
9494
{
95-
var actualPreviousVersionNumber =
95+
VersionZeroReservedException.ThrowIfZero(transactionCommand.EntityVersionNumber);
96+
97+
var previousVersionNumber =
9698
await CommandDocument.GetLastEntityVersionNumber(clientSessionHandle, mongoDatabase,
9799
transactionCommand.EntityId);
98100

99-
VersionZeroReservedException.ThrowIfZero(transactionCommand.EntityVersionNumber);
100-
OptimisticConcurrencyException.ThrowIfMismatch(transactionCommand.EntityVersionNumber - 1,
101-
actualPreviousVersionNumber);
101+
OptimisticConcurrencyException.ThrowIfDiscontinuous(previousVersionNumber, transactionCommand.EntityVersionNumber);
102102

103103
await CommandDocument.InsertOne(clientSessionHandle, mongoDatabase,
104104
CommandDocument.BuildOne(logger, transaction, transactionCommand));

0 commit comments

Comments
 (0)