EntityDelete

The EntityDeleteCommand handler allows you to fully control what happens when an entity is deleted. Unlike BeforeEntityUpdateCommand or AfterEntityUpdateCommand, you are responsible for handling the entire delete operation yourself. Bloqs will not automatically delete the entity if you implement this handler.

When to Use

  • Implement custom deletion logic for entities.

  • Enforce business rules or constraints before deletion.

  • Delete related data or cascade deletions manually.

  • Trigger custom events or logging during deletion.

Sample API Implementation

using Bloqs.App.Engine.Commands;
using Bloqs.App.Engine.Commands.Data;
using Templates.SampleApp.Models.Data;

namespace Templates.SampleApp.Api.Commands.Activities;

[EntityCommandHandler<Activity>]
public class ActivityDelete
    : ICommandHandler<EntityDeleteCommand, EntityDeleteCommandResult>
{
    public Task<EntityDeleteCommandResult> HandleAsync(
        EntityDeleteCommand command,
        CancellationToken cancellationToken = default
    )
    {
        var activity = (Activity)command.DataEntities.First();

        if (activity.Name == "Protected")
        {
            throw new Exception("Cannot delete protected activities.");
        }

        Console.WriteLine($"{AppConstants.App.Name} [API]: Deleting activity: " + activity.Name);

        // manually delete entity here
        // e.g., using a data service or repository

        return Task.FromResult(EntityDeleteCommandResult.CreateSuccess());
    }
}

Sample UI Implementation (Blazor WASM)

using Bloqs.App.Engine.Commands;
using Bloqs.App.Engine.Commands.Data;
using Templates.SampleApp.Models.Data;

namespace Templates.SampleApp.UserInterface.Commands;

[EntityCommandHandler<Activity>]
public class ActivityDelete
    : ICommandHandler<EntityDeleteCommand, EntityDeleteCommandResult>
{
    public Task<EntityDeleteCommandResult> HandleAsync(
        EntityDeleteCommand command,
        CancellationToken cancellationToken = default
    )
    {
        var activity = (Activity)command.DataEntities.First();

        Console.WriteLine($"{AppConstants.App.Name} [UI]: Deleting activity: " + activity.Name);

        // manually delete entity here
        // e.g., remove from API via service call

        return Task.FromResult(EntityDeleteCommandResult.CreateSuccess());
    }
}

Notes

  • Implementing this handler overrides the default deletion behavior completely.

  • You must manually delete the entity if needed.

  • Can be implemented in API, UI, or both, depending on your requirements.

  • Throwing an exception will prevent the deletion.

Last updated