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.
61 lines
1.2 KiB
61 lines
1.2 KiB
using Connected;
|
|
using Connected.Notifications.Events;
|
|
|
|
namespace Common.Documents;
|
|
public abstract class DocumentListener<TArgs, TDocument, TPrimaryKey> : EventListener<TArgs>
|
|
where TArgs : IDto
|
|
where TDocument : IDocument<TPrimaryKey>
|
|
where TPrimaryKey : notnull
|
|
{
|
|
public DocumentListener(IDocumentLocker<TDocument, TPrimaryKey> locker)
|
|
{
|
|
Locker = locker;
|
|
}
|
|
|
|
private IDocumentLocker<TDocument, TPrimaryKey> Locker { get; }
|
|
protected abstract TDocument Document { get; }
|
|
protected override async Task OnInvoke()
|
|
{
|
|
await OnPreparing();
|
|
|
|
if (Document is null)
|
|
return;
|
|
|
|
Locker.Expired += async (s, e) => { await OnException(new TimeoutException()); };
|
|
|
|
try
|
|
{
|
|
await Locker.Lock(Document);
|
|
await OnInvoking();
|
|
await Locker.Unlock();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
await Locker.Unlock();
|
|
await OnException(ex);
|
|
}
|
|
}
|
|
|
|
protected virtual async Task<bool> OnPreparing()
|
|
{
|
|
await Task.CompletedTask;
|
|
|
|
return false;
|
|
}
|
|
|
|
protected virtual async Task OnInvoking()
|
|
{
|
|
await Task.CompletedTask;
|
|
}
|
|
|
|
protected virtual async Task OnException(Exception ex)
|
|
{
|
|
await Task.CompletedTask;
|
|
}
|
|
|
|
protected override void OnDisposing()
|
|
{
|
|
Locker.Dispose();
|
|
}
|
|
}
|