|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|