jbogard / MediatR

Simple, unambitious mediator implementation in .NET
Apache License 2.0
11.02k stars 1.16k forks source link

How could I abstract from INotificationHandler<T>? #824

Closed arakakivl closed 1 year ago

arakakivl commented 1 year ago

Something like this:

public interface IDomainNotificationHandler : INotificationHandler<DomainNotification>
{
    public bool HasNotifications();
    public DomainNotification? GetFirstPublishedNotification();
}

public class DomainNotificationHandler : IDomainNotificationHandler
{
    private readonly List<DomainNotification> _notifications;
    public IReadOnlyCollection<DomainNotification> Notifications => _notifications;

    public DomainNotificationHandler()
    {
        _notifications = new();
    }

    public async Task Handle(DomainNotification notification, CancellationToken cancellationToken)
    {
        _notifications.Add(notification);
        await Task.CompletedTask;
    }

    public bool HasNotifications() =>
        _notifications.Any();

    public DomainNotification? GetFirstPublishedNotification() =>
        _notifications.FirstOrDefault();
}

Is it possible?