将NotificationService抽取成接口,并将所有使用NotificationService全部替换为接口INotificationService

This commit is contained in:
2025-09-04 19:59:35 +08:00
parent bb650e2682
commit 9d446c370e
12 changed files with 95 additions and 31 deletions

View File

@@ -75,7 +75,7 @@ public partial class App : System.Windows.Application
}
catch (Exception exception)
{
var notificationService = Host.Services.GetRequiredService<NotificationService>();
var notificationService = Host.Services.GetRequiredService<INotificationService>();
notificationService.ShowError($"加载数据时发生错误,如果是连接字符串不正确,可以在设置界面更改:{exception.Message}", exception);
}
@@ -109,7 +109,7 @@ public partial class App : System.Windows.Application
services.AddSingleton<ILoggerFactory, NLogLoggerFactory>();
//
services.AddSingleton<GrowlNotificationService>();
services.AddSingleton<NotificationService>();
services.AddSingleton<INotificationService, NotificationService>();
// services.AddHostedService<S7BackgroundService>();
// services.AddHostedService<OpcUaBackgroundService>();
// services.AddHostedService<DMS.Infrastructure.Services.MqttBackgroundService>();

View File

@@ -1,7 +1,73 @@
namespace DMS.WPF.Interfaces;
using System;
using System.Runtime.CompilerServices;
using DMS.Core.Enums;
namespace DMS.WPF.Interfaces;
public interface INotificationService
{
// void Show(Notification notification);
// void Show(string message, NotificationType type = NotificationType.Info, bool IsGlobal = true);
/// <summary>
/// 显示一个通用通知消息,并根据通知类型记录日志。支持节流。
/// </summary>
/// <param name="msg">通知消息内容。</param>
/// <param name="notificationType">通知类型(如信息、错误、成功等)。</param>
/// <param name="throttle">是否启用通知节流。</param>
/// <param name="callerFilePath">自动捕获:调用此方法的源文件完整路径。</param>
/// <param name="callerLineNumber">自动捕获:调用此方法的行号。</param>
void ShowMessage(string msg, NotificationType notificationType = NotificationType.Info, bool throttle = true,
[CallerFilePath] string callerFilePath = "",
[CallerLineNumber] int callerLineNumber = 0);
/// <summary>
/// 显示一个错误通知消息,并记录错误日志。支持节流。
/// </summary>
/// <param name="msg">错误消息内容。</param>
/// <param name="exception">可选:要记录的异常对象。</param>
/// <param name="throttle">是否启用通知和日志节流。</param>
/// <param name="callerFilePath">自动捕获:调用此方法的源文件完整路径。</param>
/// <param name="callerMember">自动捕获:调用此方法的成员或属性名称。</param>
/// <param name="callerLineNumber">自动捕获:调用此方法的行号。</param>
void ShowError(string msg, Exception exception = null, bool throttle = true,
[CallerFilePath] string callerFilePath = "",
[CallerMemberName] string callerMember = "",
[CallerLineNumber] int callerLineNumber = 0);
/// <summary>
/// 显示一个成功通知消息,并记录信息日志。支持节流。
/// </summary>
/// <param name="msg">成功消息内容。</param>
/// <param name="throttle">是否启用通知和日志节流。</param>
/// <param name="callerFilePath">自动捕获:调用此方法的源文件完整路径。</param>
/// <param name="callerMember">自动捕获:调用此方法的成员或属性名称。</param>
/// <param name="callerLineNumber">自动捕获:调用此方法的行号。</param>
void ShowSuccess(string msg, bool throttle = true,
[CallerFilePath] string callerFilePath = "",
[CallerMemberName] string callerMember = "",
[CallerLineNumber] int callerLineNumber = 0);
/// <summary>
/// 显示一个信息通知消息,并记录信息日志。支持节流。
/// </summary>
/// <param name="msg">信息消息内容。</param>
/// <param name="throttle">是否启用通知和日志节流。</param>
/// <param name="callerFilePath">自动捕获:调用此方法的源文件完整路径。</param>
/// <param name="callerMember">自动捕获:调用此方法的成员或属性名称。</param>
/// <param name="callerLineNumber">自动捕获:调用此方法的行号。</param>
void ShowInfo(string msg, bool throttle = true,
[CallerFilePath] string callerFilePath = "",
[CallerMemberName] string callerMember = "",
[CallerLineNumber] int callerLineNumber = 0);
/// <summary>
/// 显示一个警告通知消息,并记录警告日志。支持节流。
/// </summary>
/// <param name="msg">警告消息内容。</param>
/// <param name="throttle">是否启用通知和日志节流。</param>
/// <param name="callerFilePath">自动捕获:调用此方法的源文件完整路径。</param>
/// <param name="callerMember">自动捕获:调用此方法的成员或属性名称。</param>
/// <param name="callerLineNumber">自动捕获:调用此方法的行号。</param>
void ShowWarn(string msg, bool throttle = true,
[CallerFilePath] string callerFilePath = "",
[CallerMemberName] string callerMember = "",
[CallerLineNumber] int callerLineNumber = 0);
}

View File

@@ -1,16 +1,8 @@
// 文件: DMS.WPF/Services/NavigationService.cs
using DMS.ViewModels;
using DMS.WPF.Interfaces;
using DMS.WPF.ViewModels;
using DMS.WPF.ViewModels.Items;
using DMS.WPF.Views;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows;
using DMS.WPF.Interfaces;
using DMS.WPF.Services;
namespace DMS.WPF.Services;
@@ -43,7 +35,7 @@ public class NavigationService : INavigationService
var viewModel = GetViewModelByKey(menu.TargetViewKey);
if (viewModel == null)
{
var notificationService = App.Current.Services.GetRequiredService<NotificationService>();
var notificationService = App.Current.Services.GetRequiredService<INotificationService>();
notificationService.ShowError($"切换界面失败,没有找到界面:{menu.TargetViewKey}");
return;
}

View File

@@ -5,6 +5,7 @@ using System.Threading;
using CommunityToolkit.Mvvm.Messaging;
using DMS.Core.Enums;
using DMS.Message;
using DMS.WPF.Interfaces;
using Microsoft.Extensions.Logging;
namespace DMS.WPF.Services;
@@ -13,7 +14,7 @@ namespace DMS.WPF.Services;
/// 通知服务类,用于显示各种类型的通知消息,并集成日志记录功能。
/// 新增了通知节流功能,以防止在短时间内向用户发送大量重复的通知。
/// </summary>
public class NotificationService
public class NotificationService : INotificationService
{
private readonly ILogger<NotificationService> _logger;

View File

@@ -27,10 +27,10 @@ public partial class DeviceDetailViewModel : ViewModelBase, INavigatable
[ObservableProperty]
private VariableTableItemViewModel _selectedVariableTable;
private readonly NotificationService _notificationService;
private readonly INotificationService _notificationService;
public DeviceDetailViewModel(IMapper mapper, IDialogService dialogService, INavigationService navigationService,
DataServices dataServices, NotificationService notificationService)
DataServices dataServices, INotificationService notificationService)
{
_mapper = mapper;
_dialogService = dialogService;

View File

@@ -38,7 +38,7 @@ public partial class DevicesViewModel : ViewModelBase, INavigatable
[ObservableProperty]
private DeviceItemViewModel _selectedDevice;
private readonly NotificationService _notificationService;
private readonly INotificationService _notificationService;
/// <summary>
/// 初始化 <see cref="DevicesViewModel"/> 类的新实例。
@@ -49,7 +49,7 @@ public partial class DevicesViewModel : ViewModelBase, INavigatable
public DevicesViewModel(IMapper mapper,
IDialogService dialogService, INavigationService navigationService,
DataServices dataServices, IDeviceAppService deviceAppService,
NotificationService notificationService)
INotificationService notificationService)
{
_mapper = mapper;
_dialogService = dialogService;

View File

@@ -6,6 +6,7 @@ using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using DMS.Core.Interfaces.Services;
using DMS.Core.Models;
using DMS.WPF.Interfaces;
using DMS.WPF.Services;
using DMS.WPF.ViewModels.Items;
@@ -15,7 +16,7 @@ public partial class ImportExcelDialogViewModel : DialogViewModelBase<List<Varia
{
private readonly IMapper _mapper;
private readonly IExcelService _excelService;
private readonly NotificationService _notificationService;
private readonly INotificationService _notificationService;
[ObservableProperty]
private string? _filePath;
@@ -29,7 +30,7 @@ public partial class ImportExcelDialogViewModel : DialogViewModelBase<List<Varia
[ObservableProperty]
private IList _selectedVariables = new ArrayList();
public ImportExcelDialogViewModel(IMapper mapper,IExcelService excelService, NotificationService notificationService)
public ImportExcelDialogViewModel(IMapper mapper,IExcelService excelService, INotificationService notificationService)
{
_mapper = mapper;
_excelService = excelService;

View File

@@ -6,6 +6,7 @@ using CommunityToolkit.Mvvm.Input;
using DMS.Core.Enums;
using DMS.Infrastructure.Interfaces.Services;
using DMS.Infrastructure.Models;
using DMS.WPF.Interfaces;
using DMS.WPF.Services;
using DMS.WPF.ViewModels.Items;
using Opc.Ua;
@@ -92,7 +93,7 @@ public partial class ImportOpcUaDialogViewModel : DialogViewModelBase<List<Varia
/// <summary>
/// 通知服务实例
/// </summary>
private readonly NotificationService _notificationService;
private readonly INotificationService _notificationService;
/// <summary>
/// 构造函数
@@ -101,7 +102,7 @@ public partial class ImportOpcUaDialogViewModel : DialogViewModelBase<List<Varia
/// <param name="opcUaService">OPC UA服务接口实例</param>
/// <param name="mapper">对象映射器实例</param>
/// <param name="notificationService">通知服务实例</param>
public ImportOpcUaDialogViewModel(IOpcUaService opcUaService, IMapper mapper, NotificationService notificationService)
public ImportOpcUaDialogViewModel(IOpcUaService opcUaService, IMapper mapper, INotificationService notificationService)
{
_opcUaService = opcUaService;
_mapper = mapper;

View File

@@ -20,7 +20,7 @@ namespace DMS.ViewModels
private readonly ILogger<MqttServerDetailViewModel> _logger;
private readonly DataServices _dataServices;
private readonly IDialogService _dialogService;
private readonly NotificationService _notificationService;
private readonly INotificationService _notificationService;
/// <summary>
/// 当前正在编辑的MQTT服务器对象。
@@ -43,7 +43,7 @@ namespace DMS.ViewModels
/// <param name="dialogService">对话框服务。</param>
/// <param name="notificationService">通知服务。</param>
public MqttServerDetailViewModel(ILogger<MqttServerDetailViewModel> logger, DataServices dataServices,
IDialogService dialogService, NotificationService notificationService)
IDialogService dialogService, INotificationService notificationService)
{
_logger = logger;
_dataServices = dataServices;

View File

@@ -5,6 +5,7 @@ using DMS.Application.DTOs;
using DMS.Application.Interfaces;
using DMS.Core.Enums;
using DMS.Core.Models;
using DMS.WPF.Interfaces;
using DMS.WPF.Services;
using DMS.WPF.ViewModels.Dialogs;
using DMS.WPF.ViewModels.Items;
@@ -13,7 +14,6 @@ using ObservableCollections;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using DMS.WPF.Interfaces;
namespace DMS.WPF.ViewModels;
@@ -86,10 +86,10 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable
private readonly ISynchronizedView<VariableItemViewModel, VariableItemViewModel> _synchronizedView;
public NotifyCollectionChangedSynchronizedViewList<VariableItemViewModel> VariableItemListView { get; }
private readonly NotificationService _notificationService;
private readonly INotificationService _notificationService;
public VariableTableViewModel(IMapper mapper, IDialogService dialogService, IVariableAppService variableAppService,
DataServices dataServices, NotificationService notificationService)
DataServices dataServices, INotificationService notificationService)
{
_mapper = mapper;
_dialogService = dialogService;
@@ -198,6 +198,7 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable
// 更新数据库中的变量数据
var updateResult = await _variableAppService.UpdateVariableAsync(_mapper.Map<VariableDto>(editedVariable));
if (updateResult > 0)
{
// 更新当前页面显示的数据:找到原数据在集合中的索引并替换

View File

@@ -1,5 +1,6 @@
using DMS.Services;
using DMS.WPF.Helper;
using DMS.WPF.Interfaces;
using DMS.WPF.Services;
using DMS.WPF.ViewModels.Dialogs;
using DMS.WPF.ViewModels.Items;
@@ -52,7 +53,7 @@ public partial class ImportOpcUaDialog : ContentDialog
}
catch (Exception ex)
{
var notificationService = App.Current.Services.GetRequiredService<NotificationService>();
var notificationService = App.Current.Services.GetRequiredService<INotificationService>();
notificationService.ShowError($"选择节点时发生了错误:{ex.Message}");
}

View File

@@ -3,6 +3,7 @@ using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using DMS.WPF.ViewModels;
using DMS.WPF.Interfaces;
using DMS.WPF.Services;
using iNKORE.UI.WPF.Modern.Controls;
using Microsoft.Extensions.DependencyInjection;
@@ -39,7 +40,7 @@ public partial class VariableTableView : UserControl
}
catch (Exception exception)
{
var notificationService = App.Current.Services.GetRequiredService<NotificationService>();
var notificationService = App.Current.Services.GetRequiredService<INotificationService>();
notificationService.ShowError($"修改变量表启用,停用时发生了错误:{exception.Message}", exception);
}
}