Skip to content

Commit

Permalink
Add delete implicit operation (#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
jviau authored Aug 28, 2023
1 parent 2aaabed commit a446909
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/Abstractions/Entities/TaskEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ public interface ITaskEntity
/// Entity state will be hydrated into the <see cref="TaskEntity{TState}.State"/> property. See <see cref="State"/> for
/// more information.
/// </para>
///
/// <para><b>Implicit Operations</b></para>
/// <para>
/// This class supports some operations implicitly. Implicit operations have the lowest priority, after entity and state
/// method dispatching. To override an implicit operation, implement a public method of the same name. Throw
/// <see cref="NotSupportedException"/> from the method to indicate the implicit operation is not supported at all.
/// </para>
/// <para><c>delete</c>: deletes the entity state from storage.</para>
/// </remarks>
public abstract class TaskEntity<TState> : ITaskEntity
{
Expand Down Expand Up @@ -120,6 +128,12 @@ public abstract class TaskEntity<TState> : ITaskEntity
if (!operation.TryDispatch(this, out object? result, out Type returnType)
&& !this.TryDispatchState(operation, out result, out returnType))
{
if (TryDispatchImplicit(operation, out ValueTask<object?> task))
{
// We do not go into UnwrapAsync for implicit tasks
return task;
}

throw new NotSupportedException($"No suitable method found for entity operation '{operation}'.");
}

Expand All @@ -144,6 +158,20 @@ protected virtual TState InitializeState()
return Activator.CreateInstance<TState>();
}

static bool TryDispatchImplicit(TaskEntityOperation operation, out ValueTask<object?> result)
{
// We do not implement implicit operations via methods because then they would supersede state-dispatching.
// As such, implicit operations are manually implemented here.
result = default;
if (string.Equals(operation.Name, "delete", StringComparison.OrdinalIgnoreCase))
{
operation.State.SetState(null);
return true;
}

return false;
}

bool TryDispatchState(TaskEntityOperation operation, out object? result, out Type returnType)
{
if (!this.AllowStateDispatch)
Expand Down
33 changes: 33 additions & 0 deletions test/Abstractions.Tests/Entities/EntityTaskEntityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,34 @@ public async Task DefaultValue_Input_Succeeds()
result.Should().BeOfType<string>().Which.Should().Be("not-default");
}

[Theory]
[InlineData("delete")]
[InlineData("Delete")]
public async Task ImplicitDelete_ClearsState(string op)
{
TestEntityOperation operation = new(op, 10, default);
TestEntity entity = new();

object? result = await entity.RunAsync(operation);

result.Should().BeNull();
operation.State.GetState(typeof(object)).Should().BeNull();
}

[Theory]
[InlineData("delete")]
[InlineData("Delete")]
public async Task ExplicitDelete_Overridden(string op)
{
TestEntityOperation operation = new(op, 10, default);
DeleteEntity entity = new();

object? result = await entity.RunAsync(operation);

result.Should().BeNull();
operation.State.GetState(typeof(int)).Should().Be(0);
}

#pragma warning disable CA1822 // Mark members as static
#pragma warning disable IDE0060 // Remove unused parameter
class TestEntity : TaskEntity<int>
Expand Down Expand Up @@ -215,6 +243,11 @@ int Get(Optional<TaskEntityContext> context)
return this.State;
}
}

class DeleteEntity : TaskEntity<int>
{
public void Delete() => this.State = 0;
}
#pragma warning restore IDE0060 // Remove unused parameter
#pragma warning restore CA1822 // Mark members as static
}
16 changes: 16 additions & 0 deletions test/Abstractions.Tests/Entities/StateTaskEntityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,20 @@ public async Task DefaultValue_Input_Succeeds()
result.Should().BeOfType<string>().Which.Should().Be("not-default");
}

[Theory]
[InlineData("delete")]
[InlineData("Delete")]
public async Task ExplicitDelete_Overridden(string op)
{
TestEntityOperation operation = new(op, State(10), default);
TestEntity entity = new();

object? result = await entity.RunAsync(operation);

result.Should().BeNull();
operation.State.GetState(typeof(TestState)).Should().BeOfType<TestState>().Which.Value.Should().Be(0);
}

static TestState State(int value) => new() { Value = value };

class NullStateEntity : TestEntity
Expand Down Expand Up @@ -187,6 +201,8 @@ class TestState

public static string StaticMethod() => throw new NotImplementedException();

public void Delete() => this.Value = 0;

public int Precedence() => 10;

public int Add0(int value) => this.Add(value, default);
Expand Down

0 comments on commit a446909

Please sign in to comment.