BeforeEntityUpdate

The BeforeEntityUpdate command handler lets you execute custom logic before an entity is updated. You can use it to:

  • Validate entity data

  • Initialize or modify fields

  • Log changes or perform custom actions

This command can be implemented both in the API project (server-side) and in the Blazor UI project (client-side).

If you don’t implement a BeforeEntityUpdate handler, Bloqs will still update the entity normally. Implementing it allows you to enforce rules and logic before the update happens.


How to Implement

  1. Create a class and mark it with [EntityCommandHandler<T>], where T is your entity type.

  2. Implement ICommandHandler<BeforeEntityUpdateCommand, BeforeEntityUpdateCommandResult>.

  3. Override the HandleAsync method and write your custom logic.

  4. Throw an exception if the entity is invalid — this will prevent the update.


API Example

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

namespace Templates.SampleApp.Api.Commands.Activities;

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

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

        if (string.IsNullOrEmpty(activity.Name))
        {
            throw new Exception("Invalid activity Name");
        }

        return Task.FromResult(BeforeEntityUpdateCommandResult.CreateSuccess());
    }
}
  • Runs server-side in the API project.

  • Executes before the entity is persisted to storage.


UI Example

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

namespace Templates.SampleApp.UserInterface.Commands;

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

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

        if (activity.Name == "-")
        {
            throw new Exception("Invalid activity Name");
        }

        return Task.FromResult(BeforeEntityUpdateCommandResult.CreateSuccess());
    }
}
  • Runs client-side in Blazor WASM.

  • Provides early validation and immediate feedback before sending data to the API.


Key Points

  • Both API and UI handlers can coexist for layered validation.

  • The command.DataEntities collection contains the entities being updated.

  • You can throw exceptions to prevent the update if validation fails.

  • You can modify entity fields before they are persisted.

  • The EntityCommandHandler attribute is required to register the handler in the Bloqs system.

  • Useful for enforcing business rules, validation, or logging before the database is updated.


This handler ensures that your entities meet all requirements before they are saved, giving you control over data integrity and business logic.

Last updated