From 25f3ba6f83cd1ae33713c0283160f0dcc49d3268 Mon Sep 17 00:00:00 2001 From: "David P.G" Date: Tue, 15 Jul 2025 22:18:37 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8AutoMapper=E6=A1=86=E6=9E=B6?= =?UTF-8?q?=E6=9D=A5=E5=AE=9E=E7=8E=B0Entities=E5=92=8CModle=E4=B9=8B?= =?UTF-8?q?=E9=97=B4=E7=9A=84=E8=BD=AC=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- App.xaml.cs | 35 +++++++++++++++---- Data/Entities/DbUser.cs | 7 ++++ Data/Repositories/DeviceRepository.cs | 27 +++++++------- Data/Repositories/MenuRepository.cs | 17 +++++---- Data/Repositories/MqttRepository.cs | 25 +++++++------ Data/Repositories/VarDataRepository.cs | 23 +++++++----- Data/Repositories/VarTableRepository.cs | 17 ++++++--- Models/User.cs | 9 +++++ PMSWPF.Tests/MqttRepositoryTests.cs | 2 +- PMSWPF.csproj | 5 ++- Services/DataServices.cs | 19 ++++++---- Services/DialogService.cs | 9 +++-- ViewModels/DevicesViewModel.cs | 8 ++--- .../Dialogs/MqttSelectionDialogViewModel.cs | 4 +-- ViewModels/MainViewModel.cs | 6 ++-- ViewModels/MqttsViewModel.cs | 4 +-- ViewModels/VariableTableViewModel.cs | 13 ++++--- 17 files changed, 150 insertions(+), 80 deletions(-) diff --git a/App.xaml.cs b/App.xaml.cs index 7ee3d63..ca2dac6 100644 --- a/App.xaml.cs +++ b/App.xaml.cs @@ -14,7 +14,8 @@ using PMSWPF.ViewModels; using PMSWPF.Views; using Microsoft.Extensions.Hosting; using PMSWPF.Config; -using PMSWPF.ViewModels.Dialogs; +using PMSWPF.Data.Repositories; +using PMSWPF.Services.Processors; using SqlSugar; using LogLevel = Microsoft.Extensions.Logging.LogLevel; @@ -59,6 +60,10 @@ public partial class App : Application .Await((e) => { NotificationHelper.ShowError($"初始化主菜单失败:{e.Message}", e); }, () => { MessageHelper.SendLoadMessage(LoadTypes.Menu); }); Host.Services.GetRequiredService(); + + // 初始化数据处理链 + var dataProcessingService = Host.Services.GetRequiredService(); + dataProcessingService.AddProcessor(Host.Services.GetRequiredService()); } catch (Exception exception) { @@ -106,23 +111,41 @@ public partial class App : Application services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); + + // 注册 AutoMapper + services.AddAutoMapper(typeof(App).Assembly); + + // 注册数据处理服务和处理器 + services.AddSingleton(); + services.AddHostedService(provider => (DataProcessingService)provider.GetRequiredService()); + services.AddSingleton(); + + // 注册数据仓库 + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + // 注册视图模型 services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); + services.AddSingleton(); + services.AddTransient(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + //注册View视图 services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); - services.AddSingleton(); - services.AddTransient(); services.AddSingleton(); - services.AddScoped(); services.AddScoped(); - services.AddScoped(); services.AddScoped(); - services.AddScoped(); } private void ConfigureLogging(ILoggingBuilder loggingBuilder) diff --git a/Data/Entities/DbUser.cs b/Data/Entities/DbUser.cs index 47a35b7..d95edbc 100644 --- a/Data/Entities/DbUser.cs +++ b/Data/Entities/DbUser.cs @@ -13,4 +13,11 @@ public class DbUser /// [SugarColumn(IsPrimaryKey = true, IsIdentity = true)] //数据库是自增才配自增 public int Id { get; set; } + + public string Name { get; set; } + + public string Password { get; set; } + public string Email { get; set; } + public string PhoneNumber { get; set; } + public string Address { get; set; } } \ No newline at end of file diff --git a/Data/Repositories/DeviceRepository.cs b/Data/Repositories/DeviceRepository.cs index 8847a2f..9dac661 100644 --- a/Data/Repositories/DeviceRepository.cs +++ b/Data/Repositories/DeviceRepository.cs @@ -1,4 +1,5 @@ using System.Diagnostics; +using AutoMapper; using iNKORE.UI.WPF.Modern.Common.IconKeys; using PMSWPF.Data.Entities; using PMSWPF.Enums; @@ -11,13 +12,15 @@ namespace PMSWPF.Data.Repositories; public class DeviceRepository { + private readonly IMapper _mapper; private readonly MenuRepository _menuRepository; private readonly VarTableRepository _varTableRepository; - public DeviceRepository() + public DeviceRepository(IMapper mapper,MenuRepository menuRepository,VarTableRepository varTableRepository) { - _menuRepository = new MenuRepository(); - _varTableRepository = new VarTableRepository(); + _mapper = mapper; + _menuRepository = menuRepository; + _varTableRepository = varTableRepository; } @@ -48,7 +51,8 @@ public class DeviceRepository { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); - var result = await db.Updateable(device.CopyTo()) + + var result = await db.Updateable(_mapper.Map(device)) .ExecuteCommandAsync(); stopwatch.Stop(); NlogHelper.Info($"编辑设备 '{device.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); @@ -70,16 +74,11 @@ public class DeviceRepository .Includes(d => d.VariableTables, dv => dv.Device) .Includes(d => d.VariableTables, dvd => dvd.DataVariables, data => data.VariableTable) .ToListAsync(); - var devices = new List(); - foreach (var dbDevice in dlist) - { - var device = dbDevice.CopyTo(); - devices.Add(device); - } + stopwatch.Stop(); NlogHelper.Info($"加载设备列表总耗时:{stopwatch.ElapsedMilliseconds}ms"); - + var devices= _mapper.Map>(dlist); return devices; } } @@ -99,7 +98,7 @@ public class DeviceRepository .FirstAsync(p => p.Id == id); stopwatch.Stop(); NlogHelper.Info($"根据ID '{id}' 获取设备耗时:{stopwatch.ElapsedMilliseconds}ms"); - return result.CopyTo(); + return _mapper.Map(result); } } @@ -219,7 +218,7 @@ public class DeviceRepository { ; // 添加设备 - var addDevice = await db.Insertable(device.CopyTo()) + var addDevice = await db.Insertable(_mapper.Map(device)) .ExecuteReturnEntityAsync(); // 4. 为新设备添加菜单 @@ -250,7 +249,7 @@ public class DeviceRepository } await _menuRepository.AddVarTableMenu(addDevice, addDeviceMenuId, db); - return addDevice.CopyTo(); + return _mapper.Map(addDevice); } diff --git a/Data/Repositories/MenuRepository.cs b/Data/Repositories/MenuRepository.cs index 40eaf43..d07b739 100644 --- a/Data/Repositories/MenuRepository.cs +++ b/Data/Repositories/MenuRepository.cs @@ -6,6 +6,7 @@ using PMSWPF.Extensions; using PMSWPF.Helper; using PMSWPF.Models; using SqlSugar; +using AutoMapper; using PMSWPF.Helper; @@ -13,9 +14,11 @@ namespace PMSWPF.Data.Repositories; public class MenuRepository { + private readonly IMapper _mapper; - public MenuRepository() + public MenuRepository(IMapper mapper) { + _mapper = mapper; } public async Task DeleteMenu(MenuBean menu) @@ -53,7 +56,7 @@ public class MenuRepository .ToTreeAsync(dm => dm.Items, dm => dm.ParentId, 0); foreach (var dbMenu in dbMenuTree) - menuTree.Add(dbMenu.CopyTo()); + menuTree.Add(_mapper.Map(dbMenu)); stopwatch.Stop(); NlogHelper.Info($"获取菜单树耗时:{stopwatch.ElapsedMilliseconds}ms"); return menuTree; @@ -83,7 +86,7 @@ public class MenuRepository { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); - var result = await db.Insertable(menu.CopyTo()) + var result = await db.Insertable(_mapper.Map(menu)) .ExecuteCommandAsync(); stopwatch.Stop(); NlogHelper.Info($"添加菜单 '{menu.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); @@ -136,7 +139,7 @@ public class MenuRepository Icon = SegoeFluentIcons.Devices4.Glyph, }; menu.ParentId = deviceMainMenu.Id; - var addDeviceMenuId = await db.Insertable(menu.CopyTo()) + var addDeviceMenuId = await db.Insertable(_mapper.Map(menu)) .ExecuteReturnIdentityAsync(); stopwatch.Stop(); NlogHelper.Info($"添加设备菜单 '{device.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); @@ -170,7 +173,7 @@ public class MenuRepository { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); - var result = await db.Updateable(menu.CopyTo()) + var result = await db.Updateable(_mapper.Map(menu)) .ExecuteCommandAsync(); stopwatch.Stop(); NlogHelper.Info($"编辑菜单 '{menu.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); @@ -187,7 +190,7 @@ public class MenuRepository .FirstAsync(m => m.DataId == dataId && m.Type == menuType); stopwatch.Stop(); NlogHelper.Info($"根据DataId '{dataId}' 和 MenuType '{menuType}' 获取菜单耗时:{stopwatch.ElapsedMilliseconds}ms"); - return result?.CopyTo(); + return _mapper.Map(result); } } @@ -195,6 +198,6 @@ public class MenuRepository { using var db = DbContext.GetInstance(); var dbMenu= await db.Queryable().FirstAsync(m => m.Name == name && m.Type == MenuType.MainMenu); - return dbMenu.CopyTo(); + return _mapper.Map(dbMenu); } } \ No newline at end of file diff --git a/Data/Repositories/MqttRepository.cs b/Data/Repositories/MqttRepository.cs index 7f72584..28730c8 100644 --- a/Data/Repositories/MqttRepository.cs +++ b/Data/Repositories/MqttRepository.cs @@ -1,13 +1,10 @@ -using System.Collections.Generic; using System.Diagnostics; -using System.Threading.Tasks; -using PMSWPF.Data.Entities; -using PMSWPF.Models; -using PMSWPF.Extensions; -using SqlSugar; -using PMSWPF.Helper; +using AutoMapper; using iNKORE.UI.WPF.Modern.Common.IconKeys; +using PMSWPF.Data.Entities; using PMSWPF.Enums; +using PMSWPF.Helper; +using PMSWPF.Models; namespace PMSWPF.Data.Repositories; @@ -17,10 +14,12 @@ namespace PMSWPF.Data.Repositories; public class MqttRepository { private readonly MenuRepository _menuRepository; + private readonly IMapper _mapper; - public MqttRepository() + public MqttRepository(MenuRepository menuRepository, IMapper mapper) { - _menuRepository = new MenuRepository(); + _menuRepository = menuRepository; + _mapper = mapper; } /// @@ -39,7 +38,7 @@ public class MqttRepository .SingleAsync(); stopwatch.Stop(); NlogHelper.Info($"根据ID '{id}' 获取Mqtt配置耗时:{stopwatch.ElapsedMilliseconds}ms"); - return result.CopyTo(); + return _mapper.Map(result); } } @@ -57,7 +56,7 @@ public class MqttRepository .ToListAsync(); stopwatch.Stop(); NlogHelper.Info($"获取所有Mqtt配置耗时:{stopwatch.ElapsedMilliseconds}ms"); - return result.Select(m => m.CopyTo()) + return result.Select(m => _mapper.Map(m)) .ToList(); } } @@ -75,7 +74,7 @@ public class MqttRepository await db.BeginTranAsync(); try { - var result = await db.Insertable(mqtt.CopyTo()) + var result = await db.Insertable(_mapper.Map(mqtt)) .ExecuteReturnIdentityAsync(); var mqttMenu = await _menuRepository.GetMainMenuByName("Mqtt服务器"); // Add menu entry @@ -115,7 +114,7 @@ public class MqttRepository await db.BeginTranAsync(); try { - var result = await db.Updateable(mqtt.CopyTo()) + var result = await db.Updateable(_mapper.Map(mqtt)) .ExecuteCommandAsync(); // Update menu entry var menu = await _menuRepository.GetMenuByDataId(mqtt.Id, MenuType.MqttMenu); diff --git a/Data/Repositories/VarDataRepository.cs b/Data/Repositories/VarDataRepository.cs index 8ec83d1..b888928 100644 --- a/Data/Repositories/VarDataRepository.cs +++ b/Data/Repositories/VarDataRepository.cs @@ -6,6 +6,7 @@ using PMSWPF.Extensions; using PMSWPF.Helper; using PMSWPF.Models; using SqlSugar; +using AutoMapper; namespace PMSWPF.Data.Repositories; @@ -14,6 +15,12 @@ namespace PMSWPF.Data.Repositories; /// public class VarDataRepository { + private readonly IMapper _mapper; + + public VarDataRepository(IMapper mapper) + { + _mapper = mapper; + } /// /// 根据ID获取VariableData @@ -51,7 +58,7 @@ public class VarDataRepository .ToListAsync(); stopwatch.Stop(); NlogHelper.Info($"获取所有VariableData耗时:{stopwatch.ElapsedMilliseconds}ms"); - return result.Select(d => d.CopyTo()) + return result.Select(d => _mapper.Map(d)) .ToList(); } } @@ -71,7 +78,7 @@ public class VarDataRepository .ToListAsync(); stopwatch.Stop(); NlogHelper.Info($"获取变量表的所有变量{result.Count()}个耗时:{stopwatch.ElapsedMilliseconds}ms"); - return result.Select(d=>d.CopyTo()).ToList(); + return result.Select(d=>_mapper.Map(d)).ToList(); } } @@ -103,11 +110,11 @@ public class VarDataRepository { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); - var dbVarData = await db.Insertable(variableData.CopyTo()) + var dbVarData = await db.Insertable(_mapper.Map(variableData)) .ExecuteReturnEntityAsync(); stopwatch.Stop(); NlogHelper.Info($"新增VariableData '{variableData.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); - return dbVarData.CopyTo(); + return _mapper.Map(dbVarData); } /// @@ -140,7 +147,7 @@ public class VarDataRepository stopwatch.Start(); Stopwatch stopwatch2 = new Stopwatch(); stopwatch2.Start(); - var dbList = variableDatas.Select(vb => vb.CopyTo()) + var dbList = variableDatas.Select(vb => _mapper.Map(vb)) .ToList(); stopwatch2.Stop(); NlogHelper.Info($"复制 VariableData'{variableDatas.Count()}'个, 耗时:{stopwatch2.ElapsedMilliseconds}ms"); @@ -165,7 +172,7 @@ public class VarDataRepository stopwatch.Start(); using (var _db = DbContext.GetInstance()) { - var result = await _db.UpdateNav(variableData.CopyTo()) + var result = await _db.UpdateNav(_mapper.Map(variableData)) .Include(d => d.Mqtts) .ExecuteCommandAsync(); stopwatch.Stop(); @@ -195,7 +202,7 @@ public class VarDataRepository Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); - var dbVarDatas = variableDatas.Select(vd => vd.CopyTo()); + var dbVarDatas = variableDatas.Select(vd => _mapper.Map(vd)); var result = await db.Updateable(dbVarDatas.ToList()) .ExecuteCommandAsync(); @@ -268,7 +275,7 @@ public class VarDataRepository stopwatch.Start(); using var _db = DbContext.GetInstance(); - var dbList = variableDatas.Select(vd => vd.CopyTo()) + var dbList = variableDatas.Select(vd => _mapper.Map(vd)) .ToList(); var result = await _db.Deleteable(dbList) .ExecuteCommandAsync(); diff --git a/Data/Repositories/VarTableRepository.cs b/Data/Repositories/VarTableRepository.cs index 6347341..cd14225 100644 --- a/Data/Repositories/VarTableRepository.cs +++ b/Data/Repositories/VarTableRepository.cs @@ -6,11 +6,18 @@ using PMSWPF.Helper; using PMSWPF.Models; using SqlSugar; using System.Diagnostics; +using AutoMapper; namespace PMSWPF.Data.Repositories; public class VarTableRepository { + private readonly IMapper _mapper; + + public VarTableRepository(IMapper mapper) + { + _mapper = mapper; + } /// /// 添加变量表 @@ -43,11 +50,11 @@ public class VarTableRepository Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); - var addVarTabel = await db.Insertable(variableTable.CopyTo()) + var addVarTabel = await db.Insertable(_mapper.Map(variableTable)) .ExecuteReturnEntityAsync(); stopwatch.Stop(); NlogHelper.Info($"添加设备 '{addVarTabel.Name}' 的默认变量表耗时:{stopwatch.ElapsedMilliseconds}ms"); - return addVarTabel.CopyTo(); + return _mapper.Map(addVarTabel); } /// @@ -77,7 +84,7 @@ public class VarTableRepository { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); - var result = await db.Updateable(variableTable.CopyTo()) + var result = await db.Updateable(_mapper.Map(variableTable)) .ExecuteCommandAsync(); stopwatch.Stop(); NlogHelper.Info($"编辑变量表 '{variableTable.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); @@ -114,7 +121,7 @@ public class VarTableRepository Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); // 转换对象 - var res= await db.Deleteable(varTable.CopyTo()) + var res= await db.Deleteable(_mapper.Map(varTable)) .ExecuteCommandAsync(); stopwatch.Stop(); NlogHelper.Info($"删除变量表 '{varTable.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); @@ -131,7 +138,7 @@ public class VarTableRepository if (deviceVariableTables == null || deviceVariableTables.Count == 0) return; // 转换对象 - var dbList = deviceVariableTables.Select(v => v.CopyTo()) + var dbList = deviceVariableTables.Select(v => _mapper.Map(v)) .ToList(); await db.Deleteable(dbList) .ExecuteCommandAsync(); diff --git a/Models/User.cs b/Models/User.cs index b42c81c..1d46f5e 100644 --- a/Models/User.cs +++ b/Models/User.cs @@ -2,4 +2,13 @@ public class User { + + public int Id { get; set; } + + public string Name { get; set; } + + public string Password { get; set; } + public string Email { get; set; } + public string PhoneNumber { get; set; } + public string Address { get; set; } } \ No newline at end of file diff --git a/PMSWPF.Tests/MqttRepositoryTests.cs b/PMSWPF.Tests/MqttRepositoryTests.cs index 65b019d..371621a 100644 --- a/PMSWPF.Tests/MqttRepositoryTests.cs +++ b/PMSWPF.Tests/MqttRepositoryTests.cs @@ -37,7 +37,7 @@ namespace PMSWPF.Tests // For now, we'll assume DbContext.GetInstance() can be configured for testing. // In a real scenario, you'd typically inject ISqlSugarClient into MqttRepository. // For this example, we'll directly use the _db instance for setup and verification. - _mqttRepository = new MqttRepository(); // This will still use the static DbContext.GetInstance() + // _mqttRepository = new MqttRepository(); // This will still use the static DbContext.GetInstance() // To properly test, DbContext.GetInstance() needs to be mockable or configurable. // For demonstration, we'll simulate the DbContext behavior directly here. diff --git a/PMSWPF.csproj b/PMSWPF.csproj index e9bdeec..36c8d84 100644 --- a/PMSWPF.csproj +++ b/PMSWPF.csproj @@ -1,7 +1,7 @@  - false + WinExe net8.0-windows enable @@ -13,6 +13,9 @@ + + + diff --git a/Services/DataServices.cs b/Services/DataServices.cs index d32f8f3..9970aa0 100644 --- a/Services/DataServices.cs +++ b/Services/DataServices.cs @@ -1,4 +1,5 @@ -using CommunityToolkit.Mvvm.ComponentModel; +using AutoMapper; +using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Messaging; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; @@ -18,6 +19,7 @@ namespace PMSWPF.Services; /// public partial class DataServices : ObservableRecipient, IRecipient { + private readonly IMapper _mapper; // 设备列表,使用ObservableProperty特性,当值改变时会自动触发属性变更通知。 [ObservableProperty] private List _devices; @@ -78,14 +80,16 @@ public partial class DataServices : ObservableRecipient, IRecipient /// DataServices类的构造函数。 /// 注入ILogger,并初始化各个数据仓库。 /// - /// 日志记录器实例。 - public DataServices() + /// AutoMapper 实例。 + /// + public DataServices(IMapper mapper,DeviceRepository deviceRepository,MenuRepository menuRepository,MqttRepository mqttRepository,VarDataRepository varDataRepository) { + _mapper = mapper; IsActive = true; // 激活消息接收器 - _deviceRepository = new DeviceRepository(); - _menuRepository = new MenuRepository(); - _mqttRepository = new MqttRepository(); - _varDataRepository = new VarDataRepository(); + _deviceRepository = deviceRepository; + _menuRepository = menuRepository; + _mqttRepository = mqttRepository; + _varDataRepository = varDataRepository; _variableDatas = new List(); } @@ -198,4 +202,5 @@ public partial class DataServices : ObservableRecipient, IRecipient { await _varDataRepository.UpdateAsync(variableData); } + } \ No newline at end of file diff --git a/Services/DialogService.cs b/Services/DialogService.cs index 5700bc8..6ed7e9a 100644 --- a/Services/DialogService.cs +++ b/Services/DialogService.cs @@ -1,6 +1,7 @@ using HandyControl.Tools.Extension; using iNKORE.UI.WPF.Modern.Controls; using NPOI.SS.Formula.Functions; +using PMSWPF.Data.Repositories; using PMSWPF.Enums; using PMSWPF.Models; using PMSWPF.ViewModels.Dialogs; @@ -10,9 +11,11 @@ namespace PMSWPF.Services; public class DialogService :IDialogService { - public DialogService() - { + private readonly MqttRepository _mqttRepository; + public DialogService(MqttRepository mqttRepository) + { + _mqttRepository = mqttRepository; } public async Task ShowAddDeviceDialog() @@ -174,7 +177,7 @@ public class DialogService :IDialogService public async Task ShowMqttSelectionDialog() { - var vm = new MqttSelectionDialogViewModel(); + var vm = new MqttSelectionDialogViewModel(_mqttRepository); var dialog = new MqttSelectionDialog(vm); var result = await dialog.ShowAsync(); return result == ContentDialogResult.Primary ? vm.SelectedMqtt : null; diff --git a/ViewModels/DevicesViewModel.cs b/ViewModels/DevicesViewModel.cs index 70d4c3b..1d972f1 100644 --- a/ViewModels/DevicesViewModel.cs +++ b/ViewModels/DevicesViewModel.cs @@ -42,12 +42,12 @@ public partial class DevicesViewModel : ViewModelBase /// 对话框服务。 /// 数据服务。 public DevicesViewModel( - IDialogService dialogService, DataServices dataServices + IDialogService dialogService, DataServices dataServices,DeviceRepository deviceRepository,VarTableRepository varTableRepository,MenuRepository menuRepository ) { - _deviceRepository = new DeviceRepository(); - _varTableRepository = new VarTableRepository(); - _menuRepository = new MenuRepository(); + _deviceRepository = deviceRepository; + _varTableRepository = varTableRepository; + _menuRepository = menuRepository; _dialogService = dialogService; _dataServices = dataServices; diff --git a/ViewModels/Dialogs/MqttSelectionDialogViewModel.cs b/ViewModels/Dialogs/MqttSelectionDialogViewModel.cs index c540565..97a32a0 100644 --- a/ViewModels/Dialogs/MqttSelectionDialogViewModel.cs +++ b/ViewModels/Dialogs/MqttSelectionDialogViewModel.cs @@ -16,9 +16,9 @@ public partial class MqttSelectionDialogViewModel : ObservableObject [ObservableProperty] private Mqtt? selectedMqtt; - public MqttSelectionDialogViewModel() + public MqttSelectionDialogViewModel(MqttRepository mqttRepository) { - _mqttRepository = new MqttRepository(); + _mqttRepository = mqttRepository; LoadMqtts(); } diff --git a/ViewModels/MainViewModel.cs b/ViewModels/MainViewModel.cs index db64b48..61f82d5 100644 --- a/ViewModels/MainViewModel.cs +++ b/ViewModels/MainViewModel.cs @@ -49,14 +49,14 @@ public partial class MainViewModel : ViewModelBase /// 对话框服务。 /// 日志记录器。 public MainViewModel(NavgatorServices navgatorServices, DataServices dataServices, IDialogService dialogService, - ILogger logger) + ILogger logger,VarTableRepository varTableRepository,MenuRepository menuRepository) { _navgatorServices = navgatorServices; _dataServices = dataServices; _dialogService = dialogService; _logger = logger; - _varTableRepository = new VarTableRepository(); - _menuRepository = new MenuRepository(); + _varTableRepository = varTableRepository; + _menuRepository = menuRepository; _navgatorServices.OnViewModelChanged += () => { CurrentViewModel = _navgatorServices.CurrentViewModel; }; diff --git a/ViewModels/MqttsViewModel.cs b/ViewModels/MqttsViewModel.cs index 137edb3..0a020bd 100644 --- a/ViewModels/MqttsViewModel.cs +++ b/ViewModels/MqttsViewModel.cs @@ -51,10 +51,10 @@ public partial class MqttsViewModel : ViewModelBase private Mqtt _selectedMqtt; public MqttsViewModel( - ILogger logger, IDialogService dialogService, DataServices dataServices, NavgatorServices navgatorServices + ILogger logger, IDialogService dialogService, DataServices dataServices, NavgatorServices navgatorServices,MqttRepository mqttRepository ) { - _mqttRepository = new MqttRepository(); + _mqttRepository = mqttRepository; _logger = logger; _dialogService = dialogService; _dataServices = dataServices; diff --git a/ViewModels/VariableTableViewModel.cs b/ViewModels/VariableTableViewModel.cs index b7d63bb..9b00ef9 100644 --- a/ViewModels/VariableTableViewModel.cs +++ b/ViewModels/VariableTableViewModel.cs @@ -2,6 +2,7 @@ using System.Collections.ObjectModel; using System.ComponentModel; using System.Windows.Data; using System.Windows.Input; +using AutoMapper; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using iNKORE.UI.WPF.Modern.Controls; @@ -49,6 +50,8 @@ namespace PMSWPF.ViewModels; /// partial class VariableTableViewModel : ViewModelBase { + private readonly IMapper _mapper; + /// /// 对话服务接口,用于显示各种对话框(如确认、编辑、导入等)。 /// @@ -127,12 +130,13 @@ partial class VariableTableViewModel : ViewModelBase /// 初始化服务、数据仓库和变量数据集合视图。 /// /// 对话服务接口的实例。 - public VariableTableViewModel(IDialogService dialogService) + public VariableTableViewModel(IMapper mapper,IDialogService dialogService,VarTableRepository varTableRepository,VarDataRepository varDataRepository) { + _mapper = mapper; _dialogService = dialogService; IsLoadCompletion = false; // 初始设置为 false,表示未完成加载 - _varTableRepository = new VarTableRepository(); - _varDataRepository = new VarDataRepository(); + _varTableRepository = varTableRepository; + _varDataRepository = varDataRepository; _dataVariables = new ObservableCollection(); // 初始化集合 VariableDataView = CollectionViewSource.GetDefaultView(_dataVariables); // 获取集合视图 VariableDataView.Filter = FilterVariables; // 设置过滤方法 @@ -246,7 +250,8 @@ partial class VariableTableViewModel : ViewModelBase foreach (var modifiedData in modifiedDatas) { var oldData = _originalDataVariables.First(od => od.Id == modifiedData.Id); - oldData.CopyTo(modifiedData); // 将原始数据复制回当前数据 + // 将原始数据复制回当前数据 + _mapper.Map(oldData, modifiedData); modifiedData.IsModified = false; // 重置修改状态 }