Skip to content

Commit

Permalink
Tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
halgari committed Dec 19, 2023
1 parent 8ef2e14 commit de00849
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 12 deletions.
6 changes: 4 additions & 2 deletions src/NexusMods.EventSourcing.Abstractions/IEntityContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,19 @@ public interface IEntityContext
public ITransaction Begin();

/// <summary>
/// Gets the value of the attribute for the given entity.
/// Gets the value of the attribute for the given entity. If createIfMissing is true, then the attribute will be
/// created if it does not exist, this is useful for attributes that have a default "empty" value.
/// </summary>
/// <param name="ownerId"></param>
/// <param name="attributeDefinition"></param>
/// <param name="accumulator"></param>
/// <param name="createIfMissing"></param>
/// <typeparam name="TType"></typeparam>
/// <typeparam name="TOwner"></typeparam>
/// <typeparam name="TAttribute"></typeparam>
/// <typeparam name="TAccumulator"></typeparam>
/// <returns></returns>
bool GetReadOnlyAccumulator<TOwner, TAttribute, TAccumulator>(EntityId<TOwner> ownerId, TAttribute attributeDefinition, out TAccumulator accumulator)
bool GetReadOnlyAccumulator<TOwner, TAttribute, TAccumulator>(EntityId<TOwner> ownerId, TAttribute attributeDefinition, out TAccumulator accumulator, bool createIfMissing = false)
where TOwner : IEntity
where TAttribute : IAttribute<TAccumulator>
where TAccumulator : IAccumulator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public ReadOnlyObservableCollection<TOther> Get(TOwner entity)
{
if (!entity.Context
.GetReadOnlyAccumulator<TOwner, MultiEntityAttributeDefinition<TOwner, TOther>,
MultiEntityAccumulator<TOther>>(entity.Id, this, out var accumulator))
MultiEntityAccumulator<TOther>>(entity.Id, this, out var accumulator, true))
throw new InvalidOperationException("No accumulator found for entity");

accumulator.Init(entity.Context);
Expand Down
15 changes: 14 additions & 1 deletion src/NexusMods.EventSourcing/EntityContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public TransactionId Add<TEvent>(TEvent newEvent) where TEvent : IEvent

/// <inheritdoc />
public bool GetReadOnlyAccumulator<TOwner, TAttribute, TAccumulator>(EntityId<TOwner> ownerId, TAttribute attributeDefinition,
out TAccumulator accumulator)
out TAccumulator accumulator, bool createIfMissing = false)
where TOwner : IEntity
where TAttribute : IAttribute<TAccumulator>
where TAccumulator : IAccumulator
Expand All @@ -116,6 +116,19 @@ public bool GetReadOnlyAccumulator<TOwner, TAttribute, TAccumulator>(EntityId<TO
accumulator = (TAccumulator) value;
return true;
}

if (createIfMissing)
{
var newAccumulator = attributeDefinition.CreateAccumulator();
if (values.TryAdd(attributeDefinition, newAccumulator))
{
accumulator = newAccumulator;
return true;
}
accumulator = (TAccumulator) values[attributeDefinition];
return true;
}

accumulator = default!;
return false;
}
Expand Down
4 changes: 3 additions & 1 deletion src/NexusMods.EventSourcing/Transaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ public TransactionId Commit()
{
if (events.Count == 1)
return context.Add(events[0]);
return context.Add(new TransactionEvent(events.ToArray()));
var id = context.Add(new TransactionEvent(events.ToArray()));
events.Clear();
return id;
}

public void Dispose()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@ public void Apply<T>(T context) where T : IEventContext
{
Loadout._name.Set(context, Id, Name);
}

public static void Create(ITransaction tx, EntityId<Loadout> id, string name)
{
tx.Add(new RenameLoadout(id, name));
}
}
20 changes: 13 additions & 7 deletions tests/NexusMods.EventSourcing.Tests/BasicFunctionalityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,22 +99,28 @@ public void CanGetSingletonEntities()
[Fact]
public void UpdatingAValueCallesNotifyPropertyChanged()
{
var loadout = _ctx.Get<LoadoutRegistry>();
loadout.Loadouts.Should().BeEmpty();
var loadouts = _ctx.Get<LoadoutRegistry>();
loadouts.Loadouts.Should().BeEmpty();

using var tx = _ctx.Begin();
var loadoutId = CreateLoadout.Create(tx, "Test");
tx.Commit();

var loadout = _ctx.Get(loadoutId);

var called = false;
loadout.PropertyChanged += (sender, args) =>
{
called = true;
args.PropertyName.Should().Be(nameof(LoadoutRegistry.Loadouts));
args.PropertyName.Should().Be(nameof(Loadout.Name));
};

using var tx = _ctx.Begin();
var loadoutId = CreateLoadout.Create(tx, "Test");
tx.Commit();
using var tx2 = _ctx.Begin();
RenameLoadout.Create(tx2, loadoutId, "New Name");
tx2.Commit();

called.Should().BeTrue();
loadout.Loadouts.Should().NotBeEmpty();
loadout.Name.Should().Be("New Name");
}

[Fact]
Expand Down

0 comments on commit de00849

Please sign in to comment.