Skip to content

Commit

Permalink
Merge pull request #149 from binarymash/feature/#147-Fix-optimistic-c…
Browse files Browse the repository at this point in the history
…oncurrency

Make expected version optional cos it's difficult right now to get th…
  • Loading branch information
binarymash committed Dec 24, 2018
2 parents 28dbdf9 + 04b4a20 commit af2cba4
Show file tree
Hide file tree
Showing 12 changed files with 34 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

public class Command : WriteModel.Command
{
public Command(string userId, Guid projectId, string key, string name, int expectedProjectVersion)
public Command(string userId, Guid projectId, string key, string name, int? expectedProjectVersion)
: base(userId)
{
Key = key;
Expand All @@ -13,7 +13,7 @@ public Command(string userId, Guid projectId, string key, string name, int expec
ExpectedProjectVersion = expectedProjectVersion;
}

public int ExpectedProjectVersion { get; set; }
public int? ExpectedProjectVersion { get; set; }

public Guid ProjectId { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

public class Command : WriteModel.Command
{
public Command(string userId, Guid projectId, string key, string name, int expectedProjectVersion)
public Command(string userId, Guid projectId, string key, string name, int? expectedProjectVersion)
: base(userId)
{
Key = key;
Expand All @@ -13,7 +13,7 @@ public Command(string userId, Guid projectId, string key, string name, int expec
ExpectedProjectVersion = expectedProjectVersion;
}

public int ExpectedProjectVersion { get; set; }
public int? ExpectedProjectVersion { get; set; }

public Guid ProjectId { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

public class Command : WriteModel.Command
{
public Command(string userId, Guid projectId, string environmentKey, string toggleKey, string value, int expectedToggleStateVersion)
public Command(string userId, Guid projectId, string environmentKey, string toggleKey, string value, int? expectedToggleStateVersion)
: base(userId)
{
ProjectId = projectId;
Expand All @@ -16,7 +16,7 @@ public Command(string userId, Guid projectId, string environmentKey, string togg

public Guid ProjectId { get; set; }

public int ExpectedToggleStateVersion { get; set; }
public int? ExpectedToggleStateVersion { get; set; }

public string EnvironmentKey { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

public class Command : WriteModel.Command
{
public Command(string userId, Guid projectId, string key, int expectedEnvironmentVersion)
public Command(string userId, Guid projectId, string key, int? expectedEnvironmentVersion)
: base(userId)
{
Key = key;
ProjectId = projectId;
ExpectedEnvironmentVersion = expectedEnvironmentVersion;
}

public int ExpectedEnvironmentVersion { get; set; }
public int? ExpectedEnvironmentVersion { get; set; }

public Guid ProjectId { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

public class Command : WriteModel.Command
{
public Command(string userId, Guid projectId, string key, int expectedToggleVersion)
public Command(string userId, Guid projectId, string key, int? expectedToggleVersion)
: base(userId)
{
Key = key;
ProjectId = projectId;
ExpectedToggleVersion = expectedToggleVersion;
}

public int ExpectedToggleVersion { get; set; }
public int? ExpectedToggleVersion { get; set; }

public Guid ProjectId { get; set; }

Expand Down
16 changes: 8 additions & 8 deletions src/Evelyn.Core/WriteModel/Project/Domain/Project.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public Project(string userId, Guid accountId, Guid projectId, string name)

public string Name { get; private set; }

public void AddEnvironment(string userId, string key, string name, int expectedProjectVersion)
public void AddEnvironment(string userId, string key, string name, int? expectedProjectVersion)
{
AssertNotDeleted();
AssertVersion(expectedProjectVersion);
Expand All @@ -67,7 +67,7 @@ public void AddEnvironment(string userId, string key, string name, int expectedP
ApplyChange(new EnvironmentStateAdded(userId, Id, key, now, toggleStates));
}

public void AddToggle(string userId, string key, string name, int expectedProjectVersion)
public void AddToggle(string userId, string key, string name, int? expectedProjectVersion)
{
AssertNotDeleted();
AssertVersion(expectedProjectVersion);
Expand All @@ -92,7 +92,7 @@ public void AddToggle(string userId, string key, string name, int expectedProjec
}
}

public void ChangeToggleState(string userId, string environmentKey, string toggleKey, string value, int expectedToggleStateVersion)
public void ChangeToggleState(string userId, string environmentKey, string toggleKey, string value, int? expectedToggleStateVersion)
{
AssertNotDeleted();

Expand All @@ -108,7 +108,7 @@ public void ChangeToggleState(string userId, string environmentKey, string toggl
throw new InvalidOperationException($"There is no toggle with the key {toggleKey}");
}

toggleState.AssertVersion(expectedToggleStateVersion, Id);
toggleState.AssertVersion(Id, expectedToggleStateVersion);

if (!bool.TryParse(value, out var parsedValue))
{
Expand All @@ -118,7 +118,7 @@ public void ChangeToggleState(string userId, string environmentKey, string toggl
ApplyChange(new ToggleStateChanged(userId, Id, environmentKey, toggleKey, value, DateTimeOffset.UtcNow));
}

public void DeleteToggle(string userId, string key, int expectedToggleVersion)
public void DeleteToggle(string userId, string key, int? expectedToggleVersion)
{
AssertNotDeleted();

Expand All @@ -128,7 +128,7 @@ public void DeleteToggle(string userId, string key, int expectedToggleVersion)
throw new InvalidOperationException($"There is no toggle with the key {key}");
}

toggle.AssertVersion(expectedToggleVersion, Id);
toggle.AssertVersion(Id, expectedToggleVersion);

ApplyChange(new ToggleDeleted(userId, Id, key, DateTimeOffset.UtcNow));

Expand All @@ -138,7 +138,7 @@ public void DeleteToggle(string userId, string key, int expectedToggleVersion)
}
}

public void DeleteEnvironment(string userId, string key, int expectedEnvironmentVersion)
public void DeleteEnvironment(string userId, string key, int? expectedEnvironmentVersion)
{
AssertNotDeleted();

Expand All @@ -148,7 +148,7 @@ public void DeleteEnvironment(string userId, string key, int expectedEnvironment
throw new InvalidOperationException($"There is no environment with the key {key}");
}

environment.AssertVersion(expectedEnvironmentVersion, Id);
environment.AssertVersion(Id, expectedEnvironmentVersion);

ApplyChange(new EnvironmentDeleted(userId, Id, key, DateTimeOffset.UtcNow));
ApplyChange(new EnvironmentStateDeleted(userId, Id, key, DateTimeOffset.UtcNow));
Expand Down
9 changes: 6 additions & 3 deletions src/Evelyn.Core/WriteModel/ScopedEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ public abstract class ScopedEntity
{
public int LastModifiedVersion { get; protected set; }

public void AssertVersion(int expectedVersion, Guid aggregateId)
public void AssertVersion(Guid aggregateId, int? expectedVersion)
{
if (LastModifiedVersion > expectedVersion)
if (expectedVersion.HasValue)
{
throw new ConcurrencyException(aggregateId, expectedVersion, LastModifiedVersion);
if (LastModifiedVersion > expectedVersion)
{
throw new ConcurrencyException(aggregateId, expectedVersion.Value, LastModifiedVersion);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

public class AddEnvironment
{
public AddEnvironment(string key, string name, int expectedProjectVersion)
public AddEnvironment(string key, string name, int? expectedProjectVersion)
{
Key = key;
Name = name;
Expand All @@ -15,6 +15,6 @@ public AddEnvironment(string key, string name, int expectedProjectVersion)

public string Name { get; set; }

public int ExpectedProjectVersion { get; set; }
public int? ExpectedProjectVersion { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
{
public class DeleteEnvironment
{
public DeleteEnvironment(int expectedEnvironmentVersion)
public DeleteEnvironment(int? expectedEnvironmentVersion)
{
ExpectedEnvironmentVersion = expectedEnvironmentVersion;
}

public int ExpectedEnvironmentVersion { get; set; }
public int? ExpectedEnvironmentVersion { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
{
public class ChangeToggleState
{
public ChangeToggleState(int expectedToggleStateVersion, string state)
public ChangeToggleState(string state, int? expectedToggleStateVersion)
{
ExpectedToggleStateVersion = expectedToggleStateVersion;
State = state;
}

public int ExpectedToggleStateVersion { get; set; }
public int? ExpectedToggleStateVersion { get; set; }

public string State { get; set; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
public class AddToggle
{
public AddToggle(string key, string name, int expectedProjectVersion)
public AddToggle(string key, string name, int? expectedProjectVersion)
{
Key = key;
Name = name;
Expand All @@ -13,6 +13,6 @@ public AddToggle(string key, string name, int expectedProjectVersion)

public string Name { get; set; }

public int ExpectedProjectVersion { get; set; }
public int? ExpectedProjectVersion { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
{
public class DeleteToggle
{
public DeleteToggle(int expectedToggleVersion)
public DeleteToggle(int? expectedToggleVersion)
{
ExpectedToggleVersion = expectedToggleVersion;
}

public int ExpectedToggleVersion { get; set; }
public int? ExpectedToggleVersion { get; set; }
}
}

0 comments on commit af2cba4

Please sign in to comment.