Confinity Documentation
  • Latest Version
  • Latest Version
  • Getting Started

    • Introduction
    • Core Concepts
    • Create an Application
    • Glossary
  • Essentials

    • Authentication & SSO
    • Breaking Changes
    • Roslyn Source Analyzers
    • Changelog
    • ConfinityContent
    • ConfinitySelectable
    • Confinity Schedules
    • Data Seeding
    • Development guidelines [WIP]
    • Entity App
    • Entity Form
    • Entity Permissions
    • Frontend Configuration
    • Images
    • Known Issues
    • Localization
    • Migrations
    • Modules [WIP]
    • On-Site Editing
    • Settings
    • Cascade Delete
    • Replication
    • Infrastructure
  • Modules

    • Analytics Module
    • Assets Module
    • Blog Module
    • Cookie Consent Module
    • Forms Module
    • Friendly Captcha (Forms Module )
    • GeoIP Module
    • Htmx
    • Mail Module
    • Mailing Module
    • MediaPlayer Module
    • GoogleMyBusiness Module
    • OpenTelemetry Module
    • Pages Module [WIP]
    • Pattern Library Module
    • SIX Saferpay (worldline) Module
    • Products Module
    • Search Module
    • Wizard Module
  • Guides

    • Create a Custom Entity App Form Element
    • Date and Time
    • Entity Change Listener
    • File Upload / Temp File
    • HTTP security headers
    • conventions [WIP]
    • How to use Confinity with nginx
    • Notifications
    • Nullability
    • Rename Entity
    • Schedules
    • Useful snippets
    • Content Localization
  • Design Guidelines

    • Introduction
    • Page Components
    • Forms Module

Entity Change Listener

Sometimes you want your application to react when an entity changes, e.g. to update a cache. Confinity offers the IEntityChangeListener interface for this purpose.

Prerequisites

The entity must inherit from ConfinityEntity.


public class Foobar : ConfinityEntity
{
    public string Name { get; set; } = null!;
}

Implementation

Inherit from IEntityChangeListener and implement the Handle method.


public class FoobarChangeListenerSimple(IFoobarCacheService foobarCacheService) : IEntityChangeListener
{
    public async Task Handle(IEnumerable<IEntityChange> changes)
    {
        await foobarCacheService.RebuildAsync();
    }
}

The Handle method gets invoked with a bunch of IEntityChange instances. You might want to evaluate them as followed.


public class FoobarChangeListenerComplex(IFoobarCacheService foobarCacheService) : IEntityChangeListener
{
    public async Task Handle(IEnumerable<IEntityChange> changes)
    {
        var changesList = changes.ToList();
        var idsToInvalidate = changesList
            .Where(p => p.IsDeletion)
            .Select(p => p.EntityId);
        await foobarCacheService.InvalidateAsync(idsToInvalidate);

        var idsToCache = changesList
            .Where(p => !p.IsDeletion)
            .Select(p => p.EntityId);
        await foobarCacheService.RebuildAsync(idsToCache);
    }
}

Registration

Register the implementation in your module configuration together with one or more entity types you want to monitor.


public void AddModule(IModuleContext module)
{
    module.Configure.RegisterChangeListener<FoobarChangeListenerSimple>(typeof(Foobar));
}

Notes

  • Listeners are executed in parallel
  • On the author instance listeners are triggered by the DbContext
  • On public instances listeners are triggered by the replication
Prev
Date and Time
Next
File Upload / Temp File