Skip to content

Commit

Permalink
EventBuffer.Version nad AggregateVersion as long (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
Corniel authored Jun 8, 2023
1 parent d1d6e4a commit 1323ad2
Show file tree
Hide file tree
Showing 12 changed files with 47 additions and 51 deletions.
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ dotnet_diagnostic.SA1307.severity = none # Fields should begin with upper-case c
dotnet_diagnostic.SA1308.severity = none # Fields should begin with 'm_' prefix.
dotnet_diagnostic.SA1309.severity = none # Fields should begin with underscore.
dotnet_diagnostic.SA1310.severity = none # Fields should not contain underscores.
dotnet_diagnostic.SA1302.severity = none # Interface names should start with an I.
dotnet_diagnostic.SA1311.severity = none # Static read-only fields should begin with upper-case character.
dotnet_diagnostic.SA1503.severity = none # Braces should not be omitted.
dotnet_diagnostic.SA1513.severity = none # Closing brace should be followed by an empty line.
Expand Down
4 changes: 1 addition & 3 deletions specs/Benchmarks/Collections/Creation.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Linq;

namespace Benchmarks.Collections;
namespace Benchmarks.Collections;

[MemoryDiagnoser]
public class Creation
Expand Down
12 changes: 6 additions & 6 deletions src/Qowaiv.DomainModel.TestTools/AggregateRootAssert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public static void HasUncommittedEvents<TId>(EventBuffer<TId> actualBuffer, para
}

[Impure]
private static bool AppendEvents(this StringBuilder sb, int index, object exp, object act)
private static bool AppendEvents(this StringBuilder sb, long index, object exp, object act)
{
if (sb.AppendDifferentTypes(index, exp, act))
{
Expand All @@ -116,7 +116,7 @@ private static bool AppendEvents(this StringBuilder sb, int index, object exp, o
}

[Impure]
private static bool AppendDifferentTypes(this StringBuilder sb, int index, object exp, object act)
private static bool AppendDifferentTypes(this StringBuilder sb, long index, object exp, object act)
{
var actType = act.GetType();
var expType = exp.GetType();
Expand All @@ -130,7 +130,7 @@ private static bool AppendDifferentTypes(this StringBuilder sb, int index, objec
}

[Impure]
private static bool AppendDifferentEvents(this StringBuilder sb, int index, object exp, object act)
private static bool AppendDifferentEvents(this StringBuilder sb, long index, object exp, object act)
{
var failure = false;

Expand Down Expand Up @@ -189,14 +189,14 @@ private static bool AppendDifferentEvents(this StringBuilder sb, int index, obje
}

[Impure]
private static bool AppendIdenticalEvents(this StringBuilder sb, int index, object @event)
private static bool AppendIdenticalEvents(this StringBuilder sb, long index, object @event)
{
sb.AppendLine($"[{index}] {@event.GetType().Name}");
return false;
}

[Impure]
private static bool AppendExtraEvents(this StringBuilder sb, IEnumerable<object> events, int offset, int skip, string prefix)
private static bool AppendExtraEvents(this StringBuilder sb, IEnumerable<object> events, long offset, int skip, string prefix)
{
var index = offset + skip;

Expand All @@ -213,7 +213,7 @@ private static bool AppendExtraEvents(this StringBuilder sb, IEnumerable<object>
}

[Impure]
private static bool AppendExpectedActual(this StringBuilder sb, int index, object expected, object actual)
private static bool AppendExpectedActual(this StringBuilder sb, long index, object expected, object actual)
{
var prefix = $"[{index}] ";
var empty = new string(' ', prefix.Length);
Expand Down
2 changes: 1 addition & 1 deletion src/Qowaiv.DomainModel/Aggregate_TAggregate_TId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ protected Aggregate(TId aggregateId, IValidator<TAggregate> validator) : base(va
public TId Id => Buffer.AggregateId;

/// <summary>Gets the version of aggregate.</summary>
public int Version => Buffer.Version;
public long Version => Buffer.Version;

/// <summary>Gets the buffer with the recently added events.</summary>
public EventBuffer<TId> Buffer { get; protected set; }
Expand Down
23 changes: 15 additions & 8 deletions src/Qowaiv.DomainModel/EventBuffer_TId.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Qowaiv.DomainModel;
using System.Runtime.CompilerServices;

namespace Qowaiv.DomainModel;

/// <summary>A function to convert the event (payload) from the stored event.</summary>
/// <typeparam name="TId">
Expand All @@ -20,7 +22,7 @@
/// The converted event.
/// </returns>
[Pure]
public delegate TStoredEvent ConvertToStoredEvent<in TId, out TStoredEvent>(TId aggregateId, int version, object @event);
public delegate TStoredEvent ConvertToStoredEvent<in TId, out TStoredEvent>(TId aggregateId, long version, object @event);

/// <summary>A buffer of events that should be added to an event stream.</summary>
/// <typeparam name="TId">
Expand All @@ -30,11 +32,12 @@
[DebuggerTypeProxy(typeof(CollectionDebugView))]
public readonly struct EventBuffer<TId> : IReadOnlyCollection<object>, ICollection<object>
{
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private readonly AppendOnlyCollection Buffer;
private readonly int Offset;
private readonly long Offset;

/// <summary>Initializes a new instance of the <see cref="EventBuffer{TId}"/> struct.</summary>
internal EventBuffer(TId aggregateId, int offset, int committed, AppendOnlyCollection buffer)
internal EventBuffer(TId aggregateId, long offset, long committed, AppendOnlyCollection buffer)
{
AggregateId = aggregateId;
CommittedVersion = committed;
Expand All @@ -46,10 +49,10 @@ internal EventBuffer(TId aggregateId, int offset, int committed, AppendOnlyColle
public TId AggregateId { get; }

/// <summary>The version of the event buffer.</summary>
public int Version => Buffer.Count + Offset;
public long Version => Buffer.Count + Offset;

/// <summary>Gets the committed version of the event buffer.</summary>
public int CommittedVersion { get; }
public long CommittedVersion { get; }

/// <summary>Gets the number of events in the event buffer.</summary>
/// <remarks>
Expand All @@ -59,10 +62,14 @@ internal EventBuffer(TId aggregateId, int offset, int committed, AppendOnlyColle
public int Count => Buffer.Count;

/// <summary>Get all committed events in the event buffer.</summary>
public IEnumerable<object> Committed => Buffer.Take(CommittedVersion - Offset);
public IEnumerable<object> Committed => Buffer.Take(CommittedOffset());

/// <summary>Get all uncommitted events in the event buffer.</summary>
public IEnumerable<object> Uncommitted => Buffer.Skip(CommittedVersion - Offset);
public IEnumerable<object> Uncommitted => Buffer.Skip(CommittedOffset());

[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private int CommittedOffset() => checked((int)(CommittedVersion - Offset));

/// <summary>Returns true if the event buffer contains at least one uncommitted event.</summary>
public bool HasUncommitted => Version != CommittedVersion;
Expand Down
3 changes: 2 additions & 1 deletion src/Qowaiv.DomainModel/Qowaiv.DomainModel.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
NextRelease
- Renamed AggregateRoot to Aggregate. #35 (breaking)
- Re-implemented ImmutableCollection. #34 (breaking)
- EventBuffer and Aggregate Version are of type long. #39 (breaking)
- EventBuffer as struct. #34 (breaking)
- EventBuffer implements ICollection&lt;object&gt;. #36
- EventBuffer implements IReadOnlyCollection&lt;object&gt; and ICollection&lt;object&gt;. #36
- EventDispatcher without dynamics. #34 (breaking)
- Enable annotations. #31
- Publish .NET 7.0. #29
Expand Down
4 changes: 2 additions & 2 deletions src/Qowaiv.DomainModel/Qowaiv.Validation.Guarding/Musts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ public static class DomainModelExtensions
/// extension.
/// </remarks>
[Pure]
public static Result<TAggregate> HaveVersion<TAggregate>(this Must<TAggregate> must, int expected)
public static Result<TAggregate> HaveVersion<TAggregate>(this Must<TAggregate> must, long expected)
where TAggregate : Aggregate<TAggregate>, new()
{
Guard.NotNull(must, nameof(must));
Guard.NotNegative(expected, nameof(expected));
int actual = ((dynamic)must.Subject).Version;
long actual = ((dynamic)must.Subject).Version;
return must.Be(actual == expected, ConcurrencyIssue.VersionMismatch(expected, actual));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class Throws_when
[Test]
public void hander_could_not_be_resolved()
{
Action send = () => new InvalidReturnTypeProcessor(null).Send(new EmptyCommand());
Action send = () => new InvalidReturnTypeProcessor(null!).Send(new EmptyCommand());
send.Should().Throw<UnresolvedCommandHandler>().WithMessage(
"The command handler Commands.CommandProcessor_specs.SyncCommandHandler`1[Commands.CommandProcessor_specs.EmptyCommand] could not be resolved.");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
namespace Models.Events;

public sealed record StoredEvent(object Id, int Version, object Payload);
public sealed record StoredEvent(object Id, long Version, object Payload);
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Models.Events;

namespace Qowaiv.DomainModel.UnitTests.Models;
namespace Qowaiv.DomainModel.UnitTests.Models;

public sealed class SimpleEventSourcedAggregate : Aggregate<SimpleEventSourcedAggregate, Guid>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,6 @@ public void Ignores_null_event()
.Should().NotThrow();
}


internal class WhenWithDummyEvent
{
internal void When(DummyEvent _) => throw new Detected();
}

internal class WhenWithMultipleParameters
{
internal void When(DummyEvent @event, int other) => throw new NotSupportedException($"{@event} + {other}.");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
using FluentAssertions.Execution;
using FluentAssertions.Reflection;
using Qowaiv;
using System;
using System.Reflection;

namespace FluentAssertions
namespace FluentAssertions;

/// <summary>Extensions on <see cref="AssemblyAssertions"/>.</summary>
internal static class QowaivAssemblyAssertions
{
/// <summary>Extensions on <see cref="AssemblyAssertions"/>.</summary>
public static class QowaivAssemblyAssertions
/// <summary>Asserts the <see cref="Assembly"/> to have a specific public key.</summary>
public static AndConstraint<AssemblyAssertions> HavePublicKey(this AssemblyAssertions assertions, string publicKey, string because = "", params object[] becauseArgs)
{
/// <summary>Asserts the <see cref="Assembly"/> to have a specific public key.</summary>
public static AndConstraint<AssemblyAssertions> HavePublicKey(this AssemblyAssertions assertions, string publicKey, string because = "", params object[] becauseArgs)
{
Guard.NotNull(assertions, nameof(assertions));
Guard.NotNull(assertions, nameof(assertions));

var bytes = assertions.Subject.GetName().GetPublicKey() ?? Array.Empty<byte>();
var assemblyKey = BitConverter.ToString(bytes).Replace("-", "");
var bytes = assertions.Subject.GetName().GetPublicKey() ?? Array.Empty<byte>();
var assemblyKey = BitConverter.ToString(bytes).Replace("-", "");

Execute.Assertion
.BecauseOf(because, becauseArgs)
.ForCondition(assemblyKey == publicKey)
.FailWith(
$"Expected '{assertions.Subject}' to have public key: {publicKey},{Environment.NewLine}" +
$"but got: {assemblyKey} instead.");
Execute.Assertion
.BecauseOf(because, becauseArgs)
.ForCondition(assemblyKey == publicKey)
.FailWith(
$"Expected '{assertions.Subject}' to have public key: {publicKey},{Environment.NewLine}" +
$"but got: {assemblyKey} instead.");

return new AndConstraint<AssemblyAssertions>(assertions);
}
return new AndConstraint<AssemblyAssertions>(assertions);
}
}

0 comments on commit 1323ad2

Please sign in to comment.