Events

Event handling allows you to react to events that are invoked/triggered in the core modules. You could optionally invoke your own events but unless you were going to share your module across multiple sites then it is unnecessary. This is outside the scope of this tutorial series but if this functionality is desired then please refer to one of the existing KIT modules for an example.

Instead we will show how you can handle an event triggered when a user is deleted. Whenever we try to delete a user, a "Deleting" event is invoked which allows us to clean up our data before the user is deleted. This allows us to obfuscate or remove any personal data as well as nullify any references to the user. It is important our module doesn't break the delete user functionality so that we can satisfy GDPR requirements.

To begin create a folder called "Handlers" within our module to host our event handlers. Now add the following file called "UserEventHandler.cs" in that folder:

using System.Threading.Tasks;
using Kit.Data.Handlers;
using Kit.Security;
using Kit.Tasks;

namespace DemoShop.Shop.Handlers;

public class UserEventHandler : IDeletingEventHandler<IUser> {
    public virtual Task<Result> DeletingAsync(IUser user) {
        // Nullify references to the user.

        return Task.FromResult(new Result());
    }
}

Now we just have to register this inside our container. Open the project's “HostingStartup.cs” file and add the following within the “ConfigureServices” method (adding any missing namespaces):

services.AddTransient<IEventHandler, UserEventHandler>();

Settings »