Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add delete implicit operation #178

Merged
merged 9 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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