初步重构WPF项目中的DataService和App项目中的DataCenterServicce

This commit is contained in:
2025-09-09 13:35:16 +08:00
parent 84720f1ac4
commit 94ef1dec84
44 changed files with 1618 additions and 307 deletions

View File

@@ -18,8 +18,9 @@ public partial class DeviceDetailViewModel : ViewModelBase, INavigatable
{
private readonly IMapper _mapper;
private readonly IDialogService _dialogService;
private readonly IDataStorageService _dataStorageService;
private readonly INavigationService _navigationService;
public DataServices DataServices { get; }
private readonly IWPFDataService _wpfDataService;
[ObservableProperty]
private DeviceItemViewModel _currentDevice;
@@ -28,15 +29,18 @@ public partial class DeviceDetailViewModel : ViewModelBase, INavigatable
private VariableTableItemViewModel _selectedVariableTable;
private readonly INotificationService _notificationService;
public DeviceDetailViewModel(IMapper mapper, IDialogService dialogService, INavigationService navigationService,
DataServices dataServices, INotificationService notificationService)
public DeviceDetailViewModel(IMapper mapper, IDialogService dialogService,IDataStorageService dataStorageService ,INavigationService navigationService,
IWPFDataService wpfDataService, INotificationService notificationService)
{
_mapper = mapper;
_dialogService = dialogService;
_dataStorageService = dataStorageService;
_navigationService = navigationService;
_notificationService = notificationService;
DataServices = dataServices;
_wpfDataService = wpfDataService;
}
[RelayCommand]
@@ -64,8 +68,8 @@ public partial class DeviceDetailViewModel : ViewModelBase, INavigatable
TargetViewKey = "VariableTableView"
};
if (await DataServices.AddVariableTable(_mapper.Map<VariableTableDto>(variableTableItemViewModel),
tableMenu, true))
if (await _wpfDataService.VariableTableDataService.AddVariableTable(_mapper.Map<VariableTableDto>(variableTableItemViewModel),
tableMenu, true))
{
_notificationService.ShowSuccess($"添加变量表成功:{variableTableItemViewModel.Name}");
}
@@ -104,7 +108,7 @@ public partial class DeviceDetailViewModel : ViewModelBase, INavigatable
return;
}
if (await DataServices.UpdateVariableTable(variableTable))
if (await _wpfDataService.VariableDataService.UpdateVariableTable(variableTable))
{
_notificationService.ShowSuccess($"编辑变量表成功:{variableTable.Name}");
}
@@ -135,7 +139,7 @@ public partial class DeviceDetailViewModel : ViewModelBase, INavigatable
if (await _dialogService.ShowDialogAsync(viewModel))
{
var tableName = SelectedVariableTable.Name;
if (await DataServices.DeleteVariableTable(SelectedVariableTable,true))
if (await _wpfDataService.VariableDataService.DeleteVariableTable(SelectedVariableTable,true))
{
_notificationService.ShowSuccess($"变量表:{tableName},删除成功。");
}
@@ -155,7 +159,7 @@ public partial class DeviceDetailViewModel : ViewModelBase, INavigatable
public async Task OnNavigatedToAsync(MenuItemViewModel menu)
{
var device = DataServices.Devices.FirstOrDefault(d => d.Id == menu.TargetId);
var device = _dataStorageService.Devices.FirstOrDefault(d => d.Id == menu.TargetId);
if (device != null)
{
CurrentDevice = device;
@@ -166,8 +170,8 @@ public partial class DeviceDetailViewModel : ViewModelBase, INavigatable
public void NavigateToVariableTable()
{
if (SelectedVariableTable == null) return;
var menu = DataServices.Menus.FirstOrDefault(m => m.MenuType == MenuType.VariableTableMenu &&
m.TargetId == SelectedVariableTable.Id);
var menu = _dataStorageService.Menus.FirstOrDefault(m => m.MenuType == MenuType.VariableTableMenu &&
m.TargetId == SelectedVariableTable.Id);
if (menu == null) return;
_navigationService.NavigateToAsync(menu);
}

View File

@@ -18,9 +18,10 @@ namespace DMS.WPF.ViewModels;
/// </summary>
public partial class DevicesViewModel : ViewModelBase, INavigatable
{
public DataServices DataServices { get; }
private readonly IWPFDataService _wpfDataService;
private readonly IDeviceAppService _deviceAppService;
private readonly IMapper _mapper;
private readonly IDataStorageService _dataStorageService;
private readonly IDialogService _dialogService;
private readonly INavigationService _navigationService;
@@ -45,20 +46,20 @@ public partial class DevicesViewModel : ViewModelBase, INavigatable
/// </summary>
/// <param name="logger">日志记录器。</param>
/// <param name="dialogService">对话框服务。</param>
/// <param name="dataServices">数据服务。</param>
public DevicesViewModel(IMapper mapper,
/// <param name="wpfDataService">数据服务。</param>
public DevicesViewModel(IMapper mapper,IDataStorageService dataStorageService,
IDialogService dialogService, INavigationService navigationService,
DataServices dataServices, IDeviceAppService deviceAppService,
IWPFDataService wpfDataService, IDeviceAppService deviceAppService,
INotificationService notificationService)
{
_mapper = mapper;
_dataStorageService = dataStorageService;
_dialogService = dialogService;
_navigationService = navigationService;
DataServices = dataServices;
_wpfDataService = wpfDataService;
_deviceAppService = deviceAppService;
_notificationService = notificationService;
Devices = new ObservableCollection<DeviceItemViewModel>();
DataServices.OnDeviceListChanged += (devices) => { };
Devices = _dataStorageService.Devices;
}
@@ -111,7 +112,7 @@ public partial class DevicesViewModel : ViewModelBase, INavigatable
// 添加设备
var addDto = await DataServices.AddDevice(dto);
var addDto = await _wpfDataService.DeviceDataService.AddDevice(dto);
_notificationService.ShowSuccess($"设备添加成功:{addDto.Device.Name}");
}
@@ -140,7 +141,7 @@ public partial class DevicesViewModel : ViewModelBase, INavigatable
if (await _dialogService.ShowDialogAsync(viewModel))
{
var deviceName = SelectedDevice.Name;
if (await DataServices.DeleteDevice(SelectedDevice))
if (await _wpfDataService.DeviceDataService.DeleteDevice(SelectedDevice))
{
_notificationService.ShowSuccess($"删除设备成功,设备名:{deviceName}");
}
@@ -178,7 +179,7 @@ public partial class DevicesViewModel : ViewModelBase, INavigatable
return;
}
if (await DataServices.UpdateDevice(device))
if (await _wpfDataService.DeviceDataService.UpdateDevice(device))
{
_notificationService.ShowSuccess($"编辑设备成功:{device.Name}");
}
@@ -194,8 +195,8 @@ public partial class DevicesViewModel : ViewModelBase, INavigatable
{
if (SelectedDevice == null) return;
var menu = DataServices.Menus.FirstOrDefault(m => m.MenuType == MenuType.DeviceMenu &&
m.TargetId == SelectedDevice.Id);
var menu = _dataStorageService.Menus.FirstOrDefault(m => m.MenuType == MenuType.DeviceMenu &&
m.TargetId == SelectedDevice.Id);
if (menu == null) return;
_navigationService.NavigateToAsync(menu);

View File

@@ -4,6 +4,7 @@ using CommunityToolkit.Mvvm.Input;
using DMS.Application.DTOs;
using DMS.Application.Interfaces;
using DMS.Core.Enums;
using DMS.WPF.Interfaces;
using DMS.WPF.Services;
using DMS.WPF.ViewModels.Items;
@@ -22,16 +23,19 @@ public partial class VariableDialogViewModel : DialogViewModelBase<VariableItemV
[ObservableProperty]
private bool _hasError;
private readonly DataServices _dataServices;
private readonly IWPFDataService _wpfDataService;
private readonly IDataStorageService _dataStorageService;
private readonly IVariableAppService _variableAppService;
private readonly IMapper _mapper;
public VariableDialogViewModel(DataServices dataServices, IVariableAppService variableAppService, IMapper mapper)
public VariableDialogViewModel(IWPFDataService wpfDataService,IDataStorageService dataStorageService, IVariableAppService variableAppService, IMapper mapper)
{
Variable = new VariableItemViewModel();
this._dataServices = dataServices;
_wpfDataService = wpfDataService;
_dataStorageService = dataStorageService;
this._variableAppService = variableAppService;
this._mapper = mapper;
}
@@ -78,7 +82,7 @@ public partial class VariableDialogViewModel : DialogViewModelBase<VariableItemV
return false;
}
//检查变量是否存在
var existVariables = _dataServices.Variables.Where(v => v.Name == Variable.Name || (v.Protocol == ProtocolType.S7 && v.S7Address == Variable.S7Address) || (v.Protocol == ProtocolType.OpcUa && v.OpcUaNodeId == Variable.OpcUaNodeId)).ToList();
var existVariables = _dataStorageService.Variables.Where(v => v.Name == Variable.Name || (v.Protocol == ProtocolType.S7 && v.S7Address == Variable.S7Address) || (v.Protocol == ProtocolType.OpcUa && v.OpcUaNodeId == Variable.OpcUaNodeId)).ToList();
VariableItemViewModel existVariable = null;
if (IsAddModel)
{

View File

@@ -22,12 +22,13 @@ namespace DMS.WPF.ViewModels;
partial class LogHistoryViewModel : ViewModelBase,IDisposable
{
public DataServices DataServices { get; }
private readonly IWPFDataService _wpfDataService ;
private readonly IMapper _mapper;
private readonly INlogAppService _nlogAppService;
private readonly IDialogService _dialogService;
private readonly IDataStorageService _dataStorageService;
private readonly INotificationService _notificationService;
private readonly IDataCenterService _dataCenterService;
private readonly IAppDataCenterService _appDataCenterService;
[ObservableProperty]
private NlogItemViewModel _selectedLog;
@@ -47,23 +48,24 @@ partial class LogHistoryViewModel : ViewModelBase,IDisposable
public ObservableCollection<string> LogLevels { get; } = new ObservableCollection<string> { "Trace", "Debug", "Info", "Warn", "Error", "Fatal" };
public LogHistoryViewModel(IMapper mapper, INlogAppService nlogAppService, IDialogService dialogService,
INotificationService notificationService, DataServices dataServices, IDataCenterService dataCenterService)
public LogHistoryViewModel(IMapper mapper, INlogAppService nlogAppService, IDialogService dialogService, IDataStorageService dataStorageService
, INotificationService notificationService, IWPFDataService wpfDataService, IAppDataCenterService appDataCenterService)
{
_mapper = mapper;
_nlogAppService = nlogAppService;
_dialogService = dialogService;
_dataStorageService = dataStorageService;
_notificationService = notificationService;
DataServices = dataServices;
_dataCenterService = dataCenterService;
_wpfDataService = wpfDataService;
_appDataCenterService = appDataCenterService;
_logItemList = new ObservableList<NlogItemViewModel>(DataServices.Nlogs);
_logItemList = new ObservableList<NlogItemViewModel>(_dataStorageService.Nlogs);
_synchronizedView = _logItemList.CreateView(v => v);
LogItemListView = _synchronizedView.ToNotifyCollectionChanged();
// 订阅日志变更事件
_dataCenterService.NlogChanged += OnNlogChanged;
_appDataCenterService.NlogChanged += _wpfDataService.LogDataService.OnNlogChanged;
}
/// <summary>
@@ -242,7 +244,7 @@ partial class LogHistoryViewModel : ViewModelBase,IDisposable
public void Dispose()
{
// 取消订阅事件
_dataCenterService.NlogChanged -= OnNlogChanged;
_appDataCenterService.NlogChanged -= OnNlogChanged;
}
}

View File

@@ -20,8 +20,9 @@ namespace DMS.WPF.ViewModels;
/// </summary>
public partial class MainViewModel : ViewModelBase
{
public DataServices DataServices { get; }
private readonly IDialogService _dialogService;
private readonly IWPFDataService _wpfDataService;
private readonly IDataStorageService _dataStorageService;
private readonly INavigationService _navigationService;
private readonly ILogger<MainViewModel> _logger;
@@ -35,7 +36,7 @@ public partial class MainViewModel : ViewModelBase
/// 应用程序的菜单列表。
/// </summary>
[ObservableProperty]
private ObservableCollection<MenuBean> _menus;
private ObservableCollection<MenuItemViewModel> _menuTrees;
/// <summary>
/// 初始化 <see cref="MainViewModel"/> 类的新实例。
@@ -44,12 +45,15 @@ public partial class MainViewModel : ViewModelBase
/// <param name="dataServices">数据服务。</param>
/// <param name="dialogService">对话框服务。</param>
/// <param name="logger">日志记录器。</param>
public MainViewModel(DataServices dataServices,INavigationService navigationService,
/// <param name="wpfDataService"></param>
public MainViewModel(IWPFDataService wpfDataService ,IDataStorageService dataStorageService,INavigationService navigationService,
ILogger<MainViewModel> logger)
{
DataServices = dataServices;
_wpfDataService = wpfDataService;
_dataStorageService = dataStorageService;
_navigationService = navigationService;
_logger = logger;
MenuTrees = _dataStorageService.MenuTrees;
CurrentViewModel = new HomeViewModel();
CurrentViewModel.OnLoaded();

View File

@@ -18,7 +18,6 @@ namespace DMS.WPF.ViewModels
public partial class MqttServerDetailViewModel : ViewModelBase
{
private readonly ILogger<MqttServerDetailViewModel> _logger;
private readonly DataServices _dataServices;
private readonly IDialogService _dialogService;
private readonly INotificationService _notificationService;
@@ -42,11 +41,10 @@ namespace DMS.WPF.ViewModels
/// <param name="dataServices">数据服务。</param>
/// <param name="dialogService">对话框服务。</param>
/// <param name="notificationService">通知服务。</param>
public MqttServerDetailViewModel(ILogger<MqttServerDetailViewModel> logger, DataServices dataServices,
public MqttServerDetailViewModel(ILogger<MqttServerDetailViewModel> logger,
IDialogService dialogService, INotificationService notificationService)
{
_logger = logger;
_dataServices = dataServices;
_dialogService = dialogService;
_notificationService = notificationService;
}

View File

@@ -18,8 +18,9 @@ namespace DMS.WPF.ViewModels;
/// </summary>
public partial class MqttsViewModel : ViewModelBase
{
private readonly DataServices _dataServices;
private readonly IWPFDataService _wpfDataService;
private readonly IMqttAppService _mqttAppService;
private readonly IDataStorageService _dataStorageService;
private readonly IMapper _mapper;
private readonly IDialogService _dialogService;
private readonly INavigationService _navigationService;
@@ -42,8 +43,9 @@ public partial class MqttsViewModel : ViewModelBase
public MqttsViewModel(
ILogger<MqttsViewModel> logger,
IDialogService dialogService,
DataServices dataServices,
IWPFDataService wpfDataService,
IMqttAppService mqttAppService,
IDataStorageService dataStorageService,
IMapper mapper,
INavigationService navigationService,
INotificationService notificationService
@@ -51,13 +53,14 @@ public partial class MqttsViewModel : ViewModelBase
{
_logger = logger;
_dialogService = dialogService;
_dataServices = dataServices;
_wpfDataService = wpfDataService;
_mqttAppService = mqttAppService;
_dataStorageService = dataStorageService;
_mapper = mapper;
_navigationService = navigationService;
_notificationService = notificationService;
Mqtts = _dataServices.MqttServers;
Mqtts = _dataStorageService.MqttServers;
}
/// <summary>
@@ -80,7 +83,7 @@ public partial class MqttsViewModel : ViewModelBase
return;
}
var mqttItem = await _dataServices.AddMqttServer(_mqttAppService, mqtt);
var mqttItem = await _wpfDataService.MqttDataService.AddMqttServer(mqtt);
_notificationService.ShowSuccess($"MQTT服务器添加成功{mqttItem.ServerName}");
}
catch (Exception e)
@@ -108,7 +111,7 @@ public partial class MqttsViewModel : ViewModelBase
if (await _dialogService.ShowDialogAsync(viewModel))
{
var mqttName = SelectedMqtt.ServerName;
await _dataServices.DeleteMqttServer(_mqttAppService, SelectedMqtt);
await _wpfDataService.MqttDataService.DeleteMqttServer(SelectedMqtt);
_notificationService.ShowSuccess($"删除MQTT服务器成功,服务器名:{mqttName}");
}
}
@@ -146,7 +149,7 @@ public partial class MqttsViewModel : ViewModelBase
return;
}
await _dataServices.UpdateMqttServer(_mqttAppService, mqtt);
await _wpfDataService.MqttDataService.UpdateMqttServer(mqtt);
// 更新UI
_mapper.Map(mqtt, SelectedMqtt);

View File

@@ -8,6 +8,7 @@ using System;
using System.Threading.Tasks;
using DMS.Application.Services;
using DMS.WPF.Helper;
using DMS.WPF.Interfaces;
using DMS.WPF.Views;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
@@ -22,20 +23,20 @@ public partial class SplashViewModel : ObservableObject
private readonly ILogger<SplashViewModel> _logger;
private readonly IServiceProvider _serviceProvider;
private readonly IInitializeService _initializeService;
private readonly IDataCenterService _dataCenterService;
private readonly DataServices _dataServices;
private readonly IDataEventService _dataEventService;
private readonly IAppDataCenterService _appDataCenterService;
[ObservableProperty]
private string _loadingMessage = "正在加载...";
public SplashViewModel(ILogger<SplashViewModel> logger,IServiceProvider serviceProvider, IInitializeService initializeService,
IDataCenterService dataCenterService, DataServices dataServices)
public SplashViewModel(ILogger<SplashViewModel> logger,IServiceProvider serviceProvider, IInitializeService initializeService,IDataEventService dataEventService,
IAppDataCenterService appDataCenterService)
{
_logger = logger;
_serviceProvider = serviceProvider;
_initializeService = initializeService;
this._dataCenterService = dataCenterService;
_dataServices = dataServices;
_dataEventService = dataEventService;
this._appDataCenterService = appDataCenterService;
}
/// <summary>
@@ -51,7 +52,7 @@ public partial class SplashViewModel : ObservableObject
_initializeService.InitializeTables();
_initializeService.InitializeMenus();
LoadingMessage = "正在加载系统配置...";
await _dataCenterService.LoadAllDataToMemoryAsync();
await _appDataCenterService.LoadAllDataToMemoryAsync();
// 可以在这里添加加载配置的逻辑
await Task.Delay(500); // 模拟耗时

View File

@@ -1,3 +1,4 @@
using System.Collections;
using AutoMapper;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
@@ -6,14 +7,10 @@ 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;
using Microsoft.Extensions.DependencyInjection;
using ObservableCollections;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace DMS.WPF.ViewModels;
@@ -82,7 +79,9 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable
/// 初始化服务、数据仓库和变量数据集合视图。
/// </summary>
/// <param name="dialogService">对话服务接口的实例。</param>
private readonly DataServices _dataServices;
private readonly IWPFDataService _wpfDataService;
private readonly IDataStorageService _dataStorageService;
private readonly ObservableList<VariableItemViewModel> _variableItemList;
private readonly ISynchronizedView<VariableItemViewModel, VariableItemViewModel> _synchronizedView;
@@ -92,14 +91,15 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable
public VariableTableViewModel(IMapper mapper, IDialogService dialogService, IVariableAppService variableAppService,
IMqttAliasAppService mqttAliasAppService, IMqttAppService mqttAppService,
DataServices dataServices, INotificationService notificationService)
IWPFDataService wpfDataService,IDataStorageService dataStorageService, INotificationService notificationService)
{
_mapper = mapper;
_dialogService = dialogService;
_variableAppService = variableAppService;
_mqttAliasAppService = mqttAliasAppService;
_mqttAppService = mqttAppService;
_dataServices = dataServices;
_wpfDataService = wpfDataService;
_dataStorageService = dataStorageService;
_notificationService = notificationService;
IsLoadCompletion = false; // 初始设置为 false表示未完成加载
@@ -402,7 +402,7 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable
_mapper.Map(addVariable, variableItemViewModel);
// // 更新当前页面显示的数据:将新变量添加到集合中
_variableItemList.Add(variableItemViewModel);
_dataServices.AddVariable(variableItemViewModel);
_wpfDataService.VariableDataService.AddVariable(variableItemViewModel);
//
// // 显示成功通知
_notificationService.ShowSuccess($"添加变量成功:{variableItemViewModel.Name}");
@@ -452,7 +452,7 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable
foreach (var variable in variablesToDelete)
{
_variableItemList.Remove(variable);
_dataServices.DeleteVariable(variable.Id);
_wpfDataService.VariableDataService.DeleteVariable(variable.Id);
}
// 显示成功通知
@@ -592,7 +592,7 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable
}
int totalAffectedCount = 0;
// 为每个变量分配MQTT别名
foreach (var editedVariableMqtt in editedVariableMqtts)
{
@@ -602,7 +602,7 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable
editedVariableMqtt.Alias);
totalAffectedCount++;
// 更新内存中的 Variable 对象
var originalVariable = validVariables.FirstOrDefault(v => v.Id == editedVariableMqtt.VariableId);
if (originalVariable == null)
@@ -612,23 +612,24 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable
if (originalVariable.MqttAliases == null)
{
originalVariable.MqttAliases = new ();
originalVariable.MqttAliases = new();
}
// 检查是否已存在该变量与该MQTT服务器的关联
var existingVariableMqtt = originalVariable.MqttAliases.FirstOrDefault(vm => vm.MqttServerId == selectedMqtt.Id);
var existingVariableMqtt
= originalVariable.MqttAliases.FirstOrDefault(vm => vm.MqttServerId == selectedMqtt.Id);
if (existingVariableMqtt == null)
{
// 如果不存在,则添加新的关联
var variableMqtt = new VariableMqttAliasItemViewModel
{
VariableId = originalVariable.Id,
MqttServerId = selectedMqtt.Id,
Alias = editedVariableMqtt.Alias,
MqttServer = selectedMqtt,
Variable = originalVariable
};
{
VariableId = originalVariable.Id,
MqttServerId = selectedMqtt.Id,
Alias = editedVariableMqtt.Alias,
MqttServer = selectedMqtt,
Variable = originalVariable
};
// originalVariable.MqttAliases.Add(variableMqtt);
}
else
@@ -640,7 +641,8 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable
if (totalAffectedCount > 0)
{
_notificationService.ShowSuccess($"已成功为 {totalAffectedCount} 个变量添加/更新MQTT服务器: {selectedMqtt.ServerName} 的别名。");
_notificationService.ShowSuccess(
$"已成功为 {totalAffectedCount} 个变量添加/更新MQTT服务器: {selectedMqtt.ServerName} 的别名。");
}
else
{
@@ -726,7 +728,7 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable
public async Task OnNavigatedToAsync(MenuItemViewModel menu)
{
var varTable = _dataServices.VariableTables.FirstOrDefault(v => v.Id == menu.TargetId);
var varTable =_dataStorageService.VariableTables.FirstOrDefault(v => v.Id == menu.TargetId);
if (varTable != null)
{