using Connected.Notifications; using Connected.Notifications.Events; using Connected.ServiceModel; namespace Connected.Services; public abstract class EntityService : Service, IServiceNotifications { public event ServiceEventHandler>? Inserted; public event ServiceEventHandler>? Updated; public event ServiceEventHandler>? Deleted; protected EntityService(IContext context) : base(context) { if (context.GetService() is IEventService events) events.Event += OnEvent; } private void OnEvent(IOperationState? sender, EventServiceArgs? e) { if (!e.Service.GetType().IsAssignableTo(GetType())) return; if (string.Equals(e.Event, nameof(Inserted), StringComparison.Ordinal) && e.Arguments is PrimaryKeyEventArgs iargs) Inserted?.Invoke(sender, iargs); else if (string.Equals(e.Event, nameof(Updated), StringComparison.Ordinal) && e.Arguments is PrimaryKeyEventArgs uargs) Inserted?.Invoke(sender, uargs); else if (string.Equals(e.Event, nameof(Deleted), StringComparison.Ordinal) && e.Arguments is PrimaryKeyEventArgs dargs) Inserted?.Invoke(sender, dargs); } protected override void OnDisposing() { if (Context.GetService() is IEventService events) events.Event -= OnEvent; base.OnDisposing(); } }