EntitySave

The EntitySaveCommand handler allows you to fully control how an entity is saved. Implementing this handler overrides the default save behavior, so you are responsible for storing the entity yourself.

When to Use

  • Implement custom save logic for an entity.

  • Modify or enrich data before saving.

  • Integrate with external systems or APIs during save.

  • Enforce business rules or validation before storing.

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 ActivitySave
    : ICommandHandler<EntitySaveCommand, EntitySaveCommandResult>
{
    public Task<EntitySaveCommandResult> HandleAsync(
        EntitySaveCommand command,
        CancellationToken cancellationToken = default
    )
    {
        var activity = (Activity)command.DataEntities.First();

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

        // custom save logic here
        // e.g., update via data service, external API, etc.

        return Task.FromResult(EntitySaveCommandResult.CreateSuccess(activity));
    }
}

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 ActivitySave
    : ICommandHandler<EntitySaveCommand, EntitySaveCommandResult>
{
    public Task<EntitySaveCommandResult> HandleAsync(
        EntitySaveCommand command,
        CancellationToken cancellationToken = default
    )
    {
        var activity = (Activity)command.DataEntities.First();

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

        // custom save logic here
        // e.g., call API endpoint to persist changes

        return Task.FromResult(EntitySaveCommandResult.CreateSuccess(activity));
    }
}

Notes

  • Implementing this handler completely overrides the default save mechanism.

  • You must manually persist the entity using a data service or other storage method.

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

  • Throwing an exception will prevent the entity from being saved.

Last updated