About Entity Events

Learn how to use entity generated events

Handling Entity Events with IBkEventHandler

The Blazor For Kids framework automatically tracks changes to your entities and allows you to react to those changes using Entity Events.

🔍 What is an Entity Event?

An entity event is a way for your application to detect when something important happens to your data—like when a new record is created, an existing one is updated, or a record is deleted. Instead of manually checking for these changes, the framework automatically notifies your application by publishing an event.

⚙️ How Does It Work?

The framework uses a feature called SaveChangesInterceptor. This interceptor runs every time the SaveChanges method is called in your database context.

If any entity is added, modified, or deleted during that operation, an event is automatically published by the framework.

🧩 Example Event Record

For every entity in your system, the framework generates a specific event class. For example, if you have an entity called Employee, the framework will generate the following event record:


        public record EmployeeChangedEvent(IEmployeeEditModel EditModel,BkOperationType OperationType) : IBkEntityChangedEvent;
        

The OperationType tells you what kind of change was made. It is defined as:


        public enum BkOperationType
        {
            None,
            Create,
            Update,
            Delete
        }
        

🧑‍💻 How to Handle an Event

To respond to a published event (for example, to send a welcome message when a new employee is created), you need to:

  1. Create a class that implements IBkEventHandler<TEvent>
  2. publicinterfaceIBkEventHandler<inTEvent>

    {

        Task<IBkCommandResult>HandleAsync(TEventpayload,CancellationTokencancellationToken);

    }

  3. Implement the HandleAsync method

Here’s an example:

publicclassAddWelcomeMessage(IBkMediatorDomainmediator):IBkEventHandler<EmployeeChangedEvent>

{

    publicTask<IBkCommandResult>HandleAsync(EmployeeChangedEventpayload,CancellationTokencancellationToken)

    {

        if(payload.OperationType!=BkOperationType.Create)

            returnTask.FromResult(BkCommandResult.Warning());

        varmessage=newEmployeeMessageModel()

        {

            EmployeeId=payload.EditModel.Id,

            Type=EmployeeMessageType.Info,

            Message=$"Welcome {payload.EditModel.FirstName} {payload.EditModel.LastName} to the company"};

        returnmediator.EmployeeMessageCreateRequest(message,cancellationToken);

    }

}

This handler will only run when a new employee is created. If the operation is an update or delete, it does nothing.

📝 Summary

  • Entity events are automatically triggered when you call SaveChanges().
  • The framework publishes a specific event record for each entity that changed.
  • You can handle these events by implementing IBkEventHandler<TEvent>.
  • The event includes both the updated entity data and the type of change (Create, Update, or Delete).

This system makes it easy to add custom behavior when your data changes, without modifying your entity logic directly.

An unhandled error has occurred. Reload 🗙