You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
40 lines
1.4 KiB
40 lines
1.4 KiB
using Connected.Notifications;
|
|
using Connected.Notifications.Events;
|
|
using Connected.ServiceModel;
|
|
|
|
namespace Connected.Services;
|
|
|
|
public abstract class EntityService<TPrimaryKey> : Service, IServiceNotifications<TPrimaryKey>
|
|
{
|
|
public event ServiceEventHandler<PrimaryKeyEventArgs<TPrimaryKey>>? Inserted;
|
|
public event ServiceEventHandler<PrimaryKeyEventArgs<TPrimaryKey>>? Updated;
|
|
public event ServiceEventHandler<PrimaryKeyEventArgs<TPrimaryKey>>? Deleted;
|
|
|
|
protected EntityService(IContext context) : base(context)
|
|
{
|
|
if (context.GetService<IEventService>() 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<TPrimaryKey> iargs)
|
|
Inserted?.Invoke(sender, iargs);
|
|
else if (string.Equals(e.Event, nameof(Updated), StringComparison.Ordinal) && e.Arguments is PrimaryKeyEventArgs<TPrimaryKey> uargs)
|
|
Inserted?.Invoke(sender, uargs);
|
|
else if (string.Equals(e.Event, nameof(Deleted), StringComparison.Ordinal) && e.Arguments is PrimaryKeyEventArgs<TPrimaryKey> dargs)
|
|
Inserted?.Invoke(sender, dargs);
|
|
}
|
|
|
|
protected override void OnDisposing()
|
|
{
|
|
if (Context.GetService<IEventService>() is IEventService events)
|
|
events.Event -= OnEvent;
|
|
|
|
base.OnDisposing();
|
|
}
|
|
}
|