From a3af5dd0ccad01bf5128373cff9d1bb916645b02 Mon Sep 17 00:00:00 2001 From: "David P.G" Date: Mon, 6 Oct 2025 17:25:05 +0800 Subject: [PATCH] =?UTF-8?q?=20refactor:=20=E7=A7=BB=E9=99=A4=20VariableMqt?= =?UTF-8?q?tAliasDto=20=E5=B9=B6=E6=9B=BF=E6=8D=A2=E4=B8=BA=20VariableMqtt?= =?UTF-8?q?Alias?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 本次重构移除了 VariableMqttAliasDto,并将其所有用法替换为 VariableMqttAlias 模型。 主要变更: - 在 DTO、服务和接口中将 VariableMqttAliasDto 替换为 VariableMqttAlias。 - 删除了 VariableMqttAliasDto 的 AutoMapper 映射。 - 删除了 VariableMqttAliasDto.cs 文件。 --- DMS.Application/DTOs/MqttServerDto.cs | 3 +- DMS.Application/DTOs/VariableDto.cs | 4 ++- DMS.Application/DTOs/VariableMqttAliasDto.cs | 25 ------------- .../Database/IMqttAliasAppService.cs | 6 ++-- .../Interfaces/IAppDataStorageService.cs | 3 +- DMS.Application/Profiles/MappingProfile.cs | 5 --- .../Services/AppDataStorageService.cs | 3 +- DMS.Application/Services/DataLoaderService.cs | 36 +++++++++---------- .../Services/Database/MqttAliasAppService.cs | 9 +++-- .../Processors/MqttPublishProcessor.cs | 5 ++- DMS.WPF/Profiles/MappingProfile.cs | 2 -- 11 files changed, 37 insertions(+), 64 deletions(-) delete mode 100644 DMS.Application/DTOs/VariableMqttAliasDto.cs diff --git a/DMS.Application/DTOs/MqttServerDto.cs b/DMS.Application/DTOs/MqttServerDto.cs index e09866c..8398d3c 100644 --- a/DMS.Application/DTOs/MqttServerDto.cs +++ b/DMS.Application/DTOs/MqttServerDto.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using DMS.Core.Models; namespace DMS.Application.DTOs; @@ -26,5 +27,5 @@ public class MqttServerDto public string MessageHeader { get; set; } public string MessageContent { get; set; } public string MessageFooter { get; set; } - public List VariableAliases { get; set; } = new(); + public List VariableAliases { get; set; } = new(); } \ No newline at end of file diff --git a/DMS.Application/DTOs/VariableDto.cs b/DMS.Application/DTOs/VariableDto.cs index 91085e1..06104d6 100644 --- a/DMS.Application/DTOs/VariableDto.cs +++ b/DMS.Application/DTOs/VariableDto.cs @@ -1,5 +1,7 @@ using DMS.Core.Enums; +using DMS.Core.Models; using System; +using System.Collections.Generic; namespace DMS.Application.DTOs; @@ -15,7 +17,7 @@ public class VariableDto public double NumericValue { get; set; } public string DisplayValue { get; set; } public VariableTableDto? VariableTable { get; set; } - public List? MqttAliases { get; set; } = new List(); + public List? MqttAliases { get; set; } = new List(); public SignalType SignalType { get; set; } public int PollingInterval { get; set; } public bool IsActive { get; set; } diff --git a/DMS.Application/DTOs/VariableMqttAliasDto.cs b/DMS.Application/DTOs/VariableMqttAliasDto.cs deleted file mode 100644 index 0627f2b..0000000 --- a/DMS.Application/DTOs/VariableMqttAliasDto.cs +++ /dev/null @@ -1,25 +0,0 @@ -using DMS.Core.Models; - -namespace DMS.Application.DTOs; - -/// -/// 用于在UI上显示和管理变量与MQTT服务器关联别名的DTO。 -/// -public class VariableMqttAliasDto -{ - public int Id { get; set; } - public int VariableId { get; set; } - public int MqttServerId { get; set; } - public string MqttServerName { get; set; } // 用于UI显示关联的服务器名称 - public string Alias { get; set; } - - /// - /// 关联的变量对象。 - /// - public Variable Variable { get; set; } - - /// - /// 关联的MQTT服务器对象。 - /// - public MqttServer MqttServer { get; set; } -} \ No newline at end of file diff --git a/DMS.Application/Interfaces/Database/IMqttAliasAppService.cs b/DMS.Application/Interfaces/Database/IMqttAliasAppService.cs index fbe8802..22767b6 100644 --- a/DMS.Application/Interfaces/Database/IMqttAliasAppService.cs +++ b/DMS.Application/Interfaces/Database/IMqttAliasAppService.cs @@ -1,4 +1,6 @@ -using DMS.Application.DTOs; +using DMS.Core.Models; +using System.Collections.Generic; +using System.Threading.Tasks; namespace DMS.Application.Interfaces.Database; @@ -10,7 +12,7 @@ public interface IMqttAliasAppService /// /// 异步获取指定变量的所有MQTT别名关联。 /// - Task> GetAliasesForVariableAsync(int variableId); + Task> GetAliasesForVariableAsync(int variableId); /// /// 异步为变量分配或更新一个MQTT别名。 diff --git a/DMS.Application/Interfaces/IAppDataStorageService.cs b/DMS.Application/Interfaces/IAppDataStorageService.cs index 32b157a..37f45f8 100644 --- a/DMS.Application/Interfaces/IAppDataStorageService.cs +++ b/DMS.Application/Interfaces/IAppDataStorageService.cs @@ -1,5 +1,6 @@ using System.Collections.Concurrent; using DMS.Application.DTOs; +using DMS.Core.Models; namespace DMS.Application.Interfaces; @@ -43,7 +44,7 @@ public interface IAppDataStorageService /// /// 安全字典,用于存储所有MQTT变量别名的数据 /// - ConcurrentDictionary VariableMqttAliases { get; } + ConcurrentDictionary VariableMqttAliases { get; } /// /// 安全字典,用于存储所有触发器定义数据 diff --git a/DMS.Application/Profiles/MappingProfile.cs b/DMS.Application/Profiles/MappingProfile.cs index c9091e0..81a8022 100644 --- a/DMS.Application/Profiles/MappingProfile.cs +++ b/DMS.Application/Profiles/MappingProfile.cs @@ -31,11 +31,6 @@ public class MappingProfile : Profile CreateMap().ReverseMap(); CreateMap().ReverseMap(); - // VariableMqttAlias 映射 - CreateMap() - .ForMember(dest => dest.MqttServerName, opt => opt.MapFrom(src => src.MqttServer.ServerName)) - .ReverseMap(); - // VariableHistory 映射 CreateMap() .ForMember(dest => dest.VariableName, opt => opt.MapFrom(src => src.Variable.Name)) diff --git a/DMS.Application/Services/AppDataStorageService.cs b/DMS.Application/Services/AppDataStorageService.cs index dc351d0..a703d70 100644 --- a/DMS.Application/Services/AppDataStorageService.cs +++ b/DMS.Application/Services/AppDataStorageService.cs @@ -1,6 +1,7 @@ using System.Collections.Concurrent; using DMS.Application.DTOs; using DMS.Application.Interfaces; +using DMS.Core.Models; namespace DMS.Application.Services; @@ -40,7 +41,7 @@ public class AppDataStorageService : IAppDataStorageService /// /// 安全字典,用于存储所有MQTT变量别名的数据 /// - public ConcurrentDictionary VariableMqttAliases { get; } = new(); + public ConcurrentDictionary VariableMqttAliases { get; } = new(); diff --git a/DMS.Application/Services/DataLoaderService.cs b/DMS.Application/Services/DataLoaderService.cs index c4c9396..a27745f 100644 --- a/DMS.Application/Services/DataLoaderService.cs +++ b/DMS.Application/Services/DataLoaderService.cs @@ -105,27 +105,23 @@ public class DataLoaderService : IDataLoaderService private async Task LoadAllVariableMqttAliases() { - - var variableMqttAliases = await _repositoryManager.VariableMqttAliases.GetAllAsync(); - var variableMqttAliasDtos = _mapper.Map>(variableMqttAliases); - foreach (var variableMqttAliasDto in variableMqttAliasDtos) - { - _appDataStorageService.VariableMqttAliases.TryAdd(variableMqttAliasDto.Id, variableMqttAliasDto); - if (_appDataStorageService.Variables.TryGetValue(variableMqttAliasDto.VariableId, out var variable)) - { - variableMqttAliasDto.Variable = _mapper.Map(variable); - variable.MqttAliases?.Add(variableMqttAliasDto); - } - if (_appDataStorageService.MqttServers.TryGetValue(variableMqttAliasDto.MqttServerId, out var mqttServer)) - { - variableMqttAliasDto.MqttServer = _mapper.Map(mqttServer); - variableMqttAliasDto.MqttServerName = variableMqttAliasDto.MqttServer.ServerName; - mqttServer.VariableAliases?.Add(variableMqttAliasDto); - } - - - } + var variableMqttAliases = await _repositoryManager.VariableMqttAliases.GetAllAsync(); + foreach (var variableMqttAlias in variableMqttAliases) + { + _appDataStorageService.VariableMqttAliases.TryAdd(variableMqttAlias.Id, variableMqttAlias); + if (_appDataStorageService.Variables.TryGetValue(variableMqttAlias.VariableId, out var variable)) + { + variableMqttAlias.Variable = _mapper.Map(variable); + variable.MqttAliases?.Add(variableMqttAlias); + } + + if (_appDataStorageService.MqttServers.TryGetValue(variableMqttAlias.MqttServerId, out var mqttServer)) + { + variableMqttAlias.MqttServer = _mapper.Map(mqttServer); + mqttServer.VariableAliases?.Add(variableMqttAlias); + } + } } /// diff --git a/DMS.Application/Services/Database/MqttAliasAppService.cs b/DMS.Application/Services/Database/MqttAliasAppService.cs index eaecd3d..c0706d6 100644 --- a/DMS.Application/Services/Database/MqttAliasAppService.cs +++ b/DMS.Application/Services/Database/MqttAliasAppService.cs @@ -1,8 +1,11 @@ using AutoMapper; -using DMS.Application.DTOs; using DMS.Application.Interfaces; using DMS.Application.Interfaces.Database; using DMS.Core.Interfaces; +using DMS.Core.Models; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; namespace DMS.Application.Services.Database; @@ -26,11 +29,11 @@ public class MqttAliasAppService : IMqttAliasAppService /// /// 异步获取指定变量的所有MQTT别名关联。 /// - public async Task> GetAliasesForVariableAsync(int variableId) + public async Task> GetAliasesForVariableAsync(int variableId) { // 从仓储获取别名,并确保加载了关联的MqttServer信息 var aliases = await _repoManager.VariableMqttAliases.GetAliasesForVariableAsync(variableId); - return _mapper.Map>(aliases); + return aliases.ToList(); } /// diff --git a/DMS.Application/Services/Processors/MqttPublishProcessor.cs b/DMS.Application/Services/Processors/MqttPublishProcessor.cs index e00a85e..ebd3584 100644 --- a/DMS.Application/Services/Processors/MqttPublishProcessor.cs +++ b/DMS.Application/Services/Processors/MqttPublishProcessor.cs @@ -34,15 +34,14 @@ public class MqttPublishProcessor : IVariableProcessor } // 遍历所有关联的MQTT配置,并将其推入发送队列 - foreach (var variableMqttAliasDto in variable.MqttAliases) + foreach (var variableMqttAlias in variable.MqttAliases) { - if (!variableMqttAliasDto.MqttServer.IsActive) + if (!variableMqttAlias.MqttServer.IsActive) { continue; } // 发布变量数据到MQTT服务器 - var variableMqttAlias = _mapper.Map(variableMqttAliasDto); variableMqttAlias.Variable.DisplayValue=variable.DisplayValue; await _mqttServiceManager.PublishVariableDataAsync(variableMqttAlias); } diff --git a/DMS.WPF/Profiles/MappingProfile.cs b/DMS.WPF/Profiles/MappingProfile.cs index 3019967..011cc12 100644 --- a/DMS.WPF/Profiles/MappingProfile.cs +++ b/DMS.WPF/Profiles/MappingProfile.cs @@ -17,7 +17,6 @@ namespace DMS.WPF.Profiles CreateMap() .ReverseMap(); CreateMap(); - CreateMap().ReverseMap(); CreateMap().ReverseMap(); @@ -32,7 +31,6 @@ namespace DMS.WPF.Profiles CreateMap().ReverseMap(); CreateMap() .ReverseMap(); - CreateMap().ReverseMap(); CreateMap().ReverseMap(); CreateMap().ReverseMap(); }