using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Threading;
namespace Sanhe.Abp.Notifications;
///
/// 通知发布提供者抽象类
///
public abstract class NotificationPublishProvider : INotificationPublishProvider, ITransientDependency
{
public abstract string Name { get; }
protected IServiceProvider ServiceProvider { get; }
protected readonly object ServiceProviderLock = new();
public ILoggerFactory LoggerFactory => LazyGetRequiredService(ref _loggerFactory);
private ILoggerFactory _loggerFactory;
protected ILogger Logger => LazyLogger.Value;
private Lazy LazyLogger => new(() => LoggerFactory?.CreateLogger(GetType().FullName) ?? NullLogger.Instance, true);
protected TService LazyGetRequiredService(ref TService reference)
{
if (reference == null)
{
lock (ServiceProviderLock)
{
if (reference == null)
{
reference = ServiceProvider.GetRequiredService();
}
}
}
return reference;
}
public ICancellationTokenProvider CancellationTokenProvider { get; set; }
protected NotificationPublishProvider(IServiceProvider serviceProvider)
{
ServiceProvider = serviceProvider;
CancellationTokenProvider = NullCancellationTokenProvider.Instance;
}
public async Task PublishAsync(NotificationInfo notification, IEnumerable identifiers)
{
await PublishAsync(notification, identifiers, CancellationTokenProvider.Token);
}
///
/// 重写实现通知发布
///
///
///
///
///
protected abstract Task PublishAsync(NotificationInfo notification, IEnumerable identifiers, CancellationToken cancellationToken = default);
}