Events
Event handling allows you to react to events that are published across modules. To demonstrate we will show how you can handle an event published when a user is deleted. Whenever we try to delete a user, a "Deleting" event is published which allows us to clean up data before the user is deleted. This allows you to obfuscate or remove any personal data as well as nullify any references to the user. It is important the module doesn't break the delete user functionality so that we can satisfy GDPR requirements.
To begin create a folder called "Handlers" within the module to host the event handlers. Now add the following file called "UserDeletingEventHandler.cs" in that folder:
using System.Threading.Tasks;
using Kit.Data.Handlers;
using Kit.Security;
using Kit.Tasks;
namespace DemoShop.Shop.Handlers;
public class UserDeletingEventHandler : IEventHandler<DeletingEvent<IUser>> {
public virtual Task<Result> HandleAsync(DeletingEvent<IUser> @event) {
// Nullify references to the user.
return Task.FromResult(Result.Success());
}
}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, UserDeletingEventHandler>();