按照软件设计文档开始重构代码01

This commit is contained in:
2025-07-21 14:35:17 +08:00
parent a7b0a5d108
commit 29a2d44319
127 changed files with 12265 additions and 1586 deletions

View File

@@ -0,0 +1,133 @@
using AutoMapper;
using DMS.Core.Interfaces;
using DMS.Core.Models;
using DMS.Application.DTOs;
using DMS.Application.Interfaces;
using DMS.Core.Enums;
namespace DMS.Application.Services;
/// <summary>
/// 实现设备管理的应用服务。
/// </summary>
public class DeviceAppService : IDeviceAppService
{
private readonly IRepositoryManager _repoManager;
private readonly IMapper _mapper;
/// <summary>
/// 构造函数通过依赖注入获取仓储管理器和AutoMapper实例。
/// </summary>
public DeviceAppService(IRepositoryManager repoManager, IMapper mapper)
{
_repoManager = repoManager;
_mapper = mapper;
}
/// <summary>
/// 异步根据ID获取设备DTO。
/// </summary>
public async Task<DeviceDto> GetDeviceByIdAsync(int id)
{
var device = await _repoManager.Devices.GetByIdAsync(id);
return _mapper.Map<DeviceDto>(device);
}
/// <summary>
/// 异步获取所有设备DTO列表。
/// </summary>
public async Task<List<DeviceDto>> GetAllDevicesAsync()
{
var devices = await _repoManager.Devices.GetAllAsync();
return _mapper.Map<List<DeviceDto>>(devices);
}
/// <summary>
/// 异步创建一个新设备及其关联的变量表和菜单(事务性操作)。
/// </summary>
public async Task<int> CreateDeviceWithDetailsAsync(CreateDeviceWithDetailsDto dto)
{
try
{
_repoManager.BeginTransaction();
var device = _mapper.Map<Device>(dto.Device);
device.IsActive = true; // 默认激活
await _repoManager.Devices.AddAsync(device);
// 假设 CreateDeviceWithDetailsDto 包含了变量表和菜单信息
if (dto.VariableTable != null)
{
var variableTable = _mapper.Map<VariableTable>(dto.VariableTable);
variableTable.DeviceId = device.Id; // 关联新设备ID
await _repoManager.VariableTables.AddAsync(variableTable);
}
// 假设有菜单服务或仓储
// if (dto.Menu != null)
// {
// var menu = _mapper.Map<Menu>(dto.Menu);
// menu.TargetId = device.Id;
// await _repoManager.Menus.AddAsync(menu);
// }
await _repoManager.CommitAsync();
return device.Id;
}
catch (Exception ex)
{
await _repoManager.RollbackAsync();
// 可以在此记录日志
throw new ApplicationException("创建设备时发生错误,操作已回滚。", ex);
}
}
/// <summary>
/// 异步更新一个已存在的设备。
/// </summary>
public async Task UpdateDeviceAsync(UpdateDeviceDto deviceDto)
{
var device = await _repoManager.Devices.GetByIdAsync(deviceDto.Id);
if (device == null)
{
throw new ApplicationException($"Device with ID {deviceDto.Id} not found.");
}
_mapper.Map(deviceDto, device);
await _repoManager.Devices.UpdateAsync(device);
await _repoManager.CommitAsync();
}
/// <summary>
/// 异步删除一个设备。
/// </summary>
public async Task DeleteDeviceAsync(int id)
{
await _repoManager.Devices.DeleteAsync(id);
await _repoManager.CommitAsync();
}
/// <summary>
/// 异步切换设备的激活状态。
/// </summary>
public async Task ToggleDeviceActiveStateAsync(int id)
{
var device = await _repoManager.Devices.GetByIdAsync(id);
if (device == null)
{
throw new ApplicationException($"Device with ID {id} not found.");
}
device.IsActive = !device.IsActive;
await _repoManager.Devices.UpdateAsync(device);
await _repoManager.CommitAsync();
}
/// <summary>
/// 异步获取指定协议类型的设备列表。
/// </summary>
public async Task<List<DeviceDto>> GetDevicesByProtocolAsync(ProtocolType protocol)
{
var devices = await _repoManager.Devices.GetActiveDevicesWithDetailsAsync(protocol);
return _mapper.Map<List<DeviceDto>>(devices);
}
}

View File

@@ -0,0 +1,60 @@
using AutoMapper;
using DMS.Core.Interfaces;
using DMS.Core.Models;
using DMS.Application.DTOs;
using DMS.Application.Interfaces;
namespace DMS.Application.Services;
/// <summary>
/// 实现菜单管理的应用服务。
/// </summary>
public class MenuService : IMenuService
{
private readonly IRepositoryManager _repoManager;
private readonly IMapper _mapper;
public MenuService(IRepositoryManager repoManager, IMapper mapper)
{
_repoManager = repoManager;
_mapper = mapper;
}
public async Task<MenuBeanDto> GetMenuByIdAsync(int id)
{
var menu = await _repoManager.Menus.GetByIdAsync(id);
return _mapper.Map<MenuBeanDto>(menu);
}
public async Task<List<MenuBeanDto>> GetAllMenusAsync()
{
var menus = await _repoManager.Menus.GetAllAsync();
return _mapper.Map<List<MenuBeanDto>>(menus);
}
public async Task<int> CreateMenuAsync(MenuBeanDto menuDto)
{
var menu = _mapper.Map<MenuBean>(menuDto);
await _repoManager.Menus.AddAsync(menu);
await _repoManager.CommitAsync();
return menu.Id;
}
public async Task UpdateMenuAsync(MenuBeanDto menuDto)
{
var menu = await _repoManager.Menus.GetByIdAsync(menuDto.Id);
if (menu == null)
{
throw new ApplicationException($"Menu with ID {menuDto.Id} not found.");
}
_mapper.Map(menuDto, menu);
await _repoManager.Menus.UpdateAsync(menu);
await _repoManager.CommitAsync();
}
public async Task DeleteMenuAsync(int id)
{
await _repoManager.Menus.DeleteAsync(id);
await _repoManager.CommitAsync();
}
}

View File

@@ -0,0 +1,60 @@
using AutoMapper;
using DMS.Core.Interfaces;
using DMS.Core.Models;
using DMS.Application.DTOs;
using DMS.Application.Interfaces;
namespace DMS.Application.Services;
/// <summary>
/// 实现MQTT别名管理的应用服务。
/// </summary>
public class MqttAliasAppService : IMqttAliasAppService
{
private readonly IRepositoryManager _repoManager;
private readonly IMapper _mapper;
public MqttAliasAppService(IRepositoryManager repoManager, IMapper mapper)
{
_repoManager = repoManager;
_mapper = mapper;
}
public async Task<VariableMqttAliasDto> GetMqttAliasByIdAsync(int id)
{
var mqttAlias = await _repoManager.VariableMqttAliases.GetByIdAsync(id);
return _mapper.Map<VariableMqttAliasDto>(mqttAlias);
}
public async Task<List<VariableMqttAliasDto>> GetAllMqttAliasesAsync()
{
var mqttAliases = await _repoManager.VariableMqttAliases.GetAllAsync();
return _mapper.Map<List<VariableMqttAliasDto>>(mqttAliases);
}
public async Task<int> CreateMqttAliasAsync(VariableMqttAliasDto mqttAliasDto)
{
var mqttAlias = _mapper.Map<VariableMqttAlias>(mqttAliasDto);
await _repoManager.VariableMqttAliases.AddAsync(mqttAlias);
await _repoManager.CommitAsync();
return mqttAlias.Id;
}
public async Task UpdateMqttAliasAsync(VariableMqttAliasDto mqttAliasDto)
{
var mqttAlias = await _repoManager.VariableMqttAliases.GetByIdAsync(mqttAliasDto.Id);
if (mqttAlias == null)
{
throw new ApplicationException($"MQTT Alias with ID {mqttAliasDto.Id} not found.");
}
_mapper.Map(mqttAliasDto, mqttAlias);
await _repoManager.VariableMqttAliases.UpdateAsync(mqttAlias);
await _repoManager.CommitAsync();
}
public async Task DeleteMqttAliasAsync(int id)
{
await _repoManager.VariableMqttAliases.DeleteAsync(id);
await _repoManager.CommitAsync();
}
}

View File

@@ -0,0 +1,60 @@
using AutoMapper;
using DMS.Core.Interfaces;
using DMS.Core.Models;
using DMS.Application.DTOs;
using DMS.Application.Interfaces;
namespace DMS.Application.Services;
/// <summary>
/// 实现MQTT服务器管理的应用服务。
/// </summary>
public class MqttAppService : IMqttAppService
{
private readonly IRepositoryManager _repoManager;
private readonly IMapper _mapper;
public MqttAppService(IRepositoryManager repoManager, IMapper mapper)
{
_repoManager = repoManager;
_mapper = mapper;
}
public async Task<MqttServerDto> GetMqttServerByIdAsync(int id)
{
var mqttServer = await _repoManager.MqttServers.GetByIdAsync(id);
return _mapper.Map<MqttServerDto>(mqttServer);
}
public async Task<List<MqttServerDto>> GetAllMqttServersAsync()
{
var mqttServers = await _repoManager.MqttServers.GetAllAsync();
return _mapper.Map<List<MqttServerDto>>(mqttServers);
}
public async Task<int> CreateMqttServerAsync(MqttServerDto mqttServerDto)
{
var mqttServer = _mapper.Map<MqttServer>(mqttServerDto);
await _repoManager.MqttServers.AddAsync(mqttServer);
await _repoManager.CommitAsync();
return mqttServer.Id;
}
public async Task UpdateMqttServerAsync(MqttServerDto mqttServerDto)
{
var mqttServer = await _repoManager.MqttServers.GetByIdAsync(mqttServerDto.Id);
if (mqttServer == null)
{
throw new ApplicationException($"MQTT Server with ID {mqttServerDto.Id} not found.");
}
_mapper.Map(mqttServerDto, mqttServer);
await _repoManager.MqttServers.UpdateAsync(mqttServer);
await _repoManager.CommitAsync();
}
public async Task DeleteMqttServerAsync(int id)
{
await _repoManager.MqttServers.DeleteAsync(id);
await _repoManager.CommitAsync();
}
}

View File

@@ -0,0 +1,60 @@
using AutoMapper;
using DMS.Core.Interfaces;
using DMS.Core.Models;
using DMS.Application.DTOs;
using DMS.Application.Interfaces;
namespace DMS.Application.Services;
/// <summary>
/// 实现变量管理的应用服务。
/// </summary>
public class VariableAppService : IVariableAppService
{
private readonly IRepositoryManager _repoManager;
private readonly IMapper _mapper;
public VariableAppService(IRepositoryManager repoManager, IMapper mapper)
{
_repoManager = repoManager;
_mapper = mapper;
}
public async Task<VariableDto> GetVariableByIdAsync(int id)
{
var variable = await _repoManager.Variables.GetByIdAsync(id);
return _mapper.Map<VariableDto>(variable);
}
public async Task<List<VariableDto>> GetAllVariablesAsync()
{
var variables = await _repoManager.Variables.GetAllAsync();
return _mapper.Map<List<VariableDto>>(variables);
}
public async Task<int> CreateVariableAsync(VariableDto variableDto)
{
var variable = _mapper.Map<Variable>(variableDto);
await _repoManager.Variables.AddAsync(variable);
await _repoManager.CommitAsync();
return variable.Id;
}
public async Task UpdateVariableAsync(VariableDto variableDto)
{
var variable = await _repoManager.Variables.GetByIdAsync(variableDto.Id);
if (variable == null)
{
throw new ApplicationException($"Variable with ID {variableDto.Id} not found.");
}
_mapper.Map(variableDto, variable);
await _repoManager.Variables.UpdateAsync(variable);
await _repoManager.CommitAsync();
}
public async Task DeleteVariableAsync(int id)
{
await _repoManager.Variables.DeleteAsync(id);
await _repoManager.CommitAsync();
}
}