首页
/ ABP框架中实时通知功能的优化思路与实践

ABP框架中实时通知功能的优化思路与实践

2025-05-19 12:52:23作者:晏闻田Solitary

背景介绍

在ABP框架的实际应用中,我们经常会遇到需要向特定在线用户发送即时通知的场景。特别是在前后端分离的架构中,前端通常会缓存一些重要数据(如系统设置、本地化文本、权限信息、菜单配置等),当这些数据在后端发生变更时,如何高效地通知前端用户成为了一个常见的需求。

现有机制的局限性

ABP框架虽然提供了IRealTimeNotifier接口用于实时通知,但在某些特定场景下存在以下不足:

  1. 持久化问题:使用INotificationPublisher会进行持久化存储,但对于仅需即时通知的场景,这种持久化显得多余且浪费资源
  2. 目标筛选不便:现有API缺乏直接针对特定范围(应用级、租户级、用户级)在线用户发送通知的便捷方式
  3. API复杂度:使用UserNotification相关API显得过于重量级,对于简单通知场景不够简洁

解决方案设计

针对上述问题,我们可以设计一个轻量级的通知触发器,主要实现以下功能:

  1. 范围选择:支持应用级、租户级和用户级三种通知范围
  2. 即时性:直接通过IRealTimeNotifier发送,避免不必要的持久化
  3. 简洁API:提供基于泛型和直接事件名的多种调用方式

核心实现代码

public class UIEventTrigger : ITransientDependency
{
    private readonly IOnlineClientManager _onlineClientManager;
    private readonly IAbpSession _abpSession;
    private readonly IRealTimeNotifier _realTimeNotifier;

    public UIEventTrigger(
        IOnlineClientManager onlineClientManager, 
        IAbpSession session,
        IRealTimeNotifier realTimeNotifier)
    {
        _onlineClientManager = onlineClientManager;
        _abpSession = session;
        _realTimeNotifier = realTimeNotifier;
    }

    public async Task Trigger(string eventName, object param = default, int scope = 0, long? scopeId = default)
    {
        var connections = await _onlineClientManager.GetAllClientsAsync();
        
        switch (scope)
        {
            case 1: // 租户级
                if (!scopeId.HasValue) scopeId = _abpSession.TenantId;
                connections = connections.Where(x => x.TenantId == scopeId);
                break;
            case 2: // 用户级
                if (!scopeId.HasValue) scopeId = _abpSession.UserId;
                connections = connections.Where(x => x.UserId == scopeId);
                break;
        }

        var notifications = connections.Select(x => new UserNotification
        {
            TenantId = x.TenantId,
            UserId = x.UserId.Value,
            Id = Guid.NewGuid(),
            State = UserNotificationState.Unread,
            Notification = new NotificationInfo
            {
                CreationTime = DateTime.Now,
                Data = new NotificationData
                {
                    Properties = new Dictionary<string, object> { { "param", param } }
                },
                NotificationName = eventName,
                Severity = NotificationSeverity.Info,
                TenantId = x.TenantId,
                Id = Guid.NewGuid(),
            }
        }).ToArray();

        await _realTimeNotifier.SendNotificationsAsync(notifications);
    }

    // 提供多种便捷调用方式
    public Task Trigger<T>(object param = default, int scope = 0, long? scopeId = default)
        => Trigger(typeof(T).FullName, param, scope, scopeId);
    
    public Task TriggerApplication(string eventName, object param = default)
        => Trigger(eventName, param);
    
    public Task TriggerApplication<T>(object param = default)
        => TriggerApplication(typeof(T).FullName, param);
    
    public Task TriggerTenant(string eventName, object param = default, int? tenantId = default)
        => Trigger(eventName, param, 1, tenantId);
    
    public Task TriggerTenant<T>(object param = default, int? tenantId = default)
        => TriggerTenant(typeof(T).FullName, param, tenantId);
    
    public Task TriggerUser(string eventName, object param = default, long? userId = default)
        => Trigger(eventName, param, 2, userId);
    
    public Task TriggerUser<T>(object param = default, long? userId = default)
        => TriggerUser(typeof(T).FullName, param, userId);
}

使用场景示例

  1. 应用级通知:当系统全局设置变更时,通知所有在线用户
await _uiEventTrigger.TriggerApplication("GlobalSettingsChanged");
  1. 租户级通知:当特定租户配置变更时,通知该租户下的所有在线用户
await _uiEventTrigger.TriggerTenant("TenantSettingsChanged", tenantId: 42);
  1. 用户级通知:当用户个人设置变更时,仅通知该用户
await _uiEventTrigger.TriggerUser("UserSettingsChanged", userId: 123);

设计优势

  1. 灵活性:支持多种粒度的通知范围选择
  2. 轻量级:避免不必要的持久化操作
  3. 易用性:提供多种便捷的调用方式,包括基于泛型的事件名推断
  4. 一致性:与ABP现有通知系统保持兼容

总结

在ABP框架中,通过扩展实时通知功能,我们可以更高效地处理特定场景下的即时通知需求。这种方案特别适合前后端分离架构中需要即时同步数据变更的场景,既保持了ABP框架的原有优势,又弥补了特定场景下的功能不足。开发者可以根据实际需求选择使用官方提供的INotificationPublisher或这种轻量级的UIEventTrigger方案。

登录后查看全文
热门项目推荐
相关项目推荐