183 lines
5.0 KiB
Markdown
183 lines
5.0 KiB
Markdown
# 软件开发文档 - 03. 应用层与基础设施层设计
|
||
|
||
本文档定义了 `DMS.Application` 和 `DMS.Infrastructure` 两个项目的详细设计。
|
||
|
||
## 1. `DMS.Application` - 应用层设计
|
||
|
||
应用层是业务流程的协调者。
|
||
|
||
### 1.1. 应用服务接口 (`Interfaces/`)
|
||
|
||
```csharp
|
||
// 文件: DMS.Application/Interfaces/IDeviceAppService.cs
|
||
namespace DMS.Application.Interfaces;
|
||
|
||
public interface IDeviceAppService
|
||
{
|
||
Task<DeviceDto> GetDeviceByIdAsync(int id);
|
||
Task<List<DeviceDto>> GetAllDevicesAsync();
|
||
Task CreateDeviceAsync(CreateDeviceDto deviceDto);
|
||
Task UpdateDeviceAsync(UpdateDeviceDto deviceDto);
|
||
Task DeleteDeviceAsync(int id);
|
||
}
|
||
|
||
// 其他应用服务接口,如 IMqttAppService, IVariableAppService 等,结构类似。
|
||
```
|
||
|
||
### 1.2. 数据传输对象 (`DTOs/`)
|
||
|
||
DTOs 用于封装数据,在各层之间传递,特别是用于UI。
|
||
|
||
```csharp
|
||
// 文件: DMS.Application/DTOs/DeviceDto.cs
|
||
namespace DMS.Application.DTOs;
|
||
|
||
public class DeviceDto
|
||
{
|
||
public int Id { get; set; }
|
||
public string Name { get; set; }
|
||
public string Protocol { get; set; }
|
||
public bool IsActive { get; set; }
|
||
public int OnlineStatus { get; set; } // 0: Offline, 1: Online
|
||
}
|
||
|
||
// 文件: DMS.Application/DTOs/CreateDeviceDto.cs
|
||
namespace DMS.Application.DTOs;
|
||
|
||
public class CreateDeviceDto
|
||
{
|
||
public string Name { get; set; }
|
||
public ProtocolType Protocol { get; set; }
|
||
public string IpAddress { get; set; }
|
||
public int Port { get; set; }
|
||
}
|
||
|
||
// 其他DTOs,如 UpdateDeviceDto, VariableDto, MqttServerDto 等。
|
||
```
|
||
|
||
### 1.3. 应用服务实现 (`Services/`)
|
||
|
||
应用服务实现了接口,并使用仓储库来执行操作。
|
||
|
||
```csharp
|
||
// 文件: DMS.Application/Services/DeviceAppService.cs
|
||
namespace DMS.Application.Services;
|
||
|
||
public class DeviceAppService : IDeviceAppService
|
||
{
|
||
private readonly IDeviceRepository _deviceRepository;
|
||
private readonly IMapper _mapper;
|
||
|
||
public DeviceAppService(IDeviceRepository deviceRepository, IMapper mapper)
|
||
{
|
||
_deviceRepository = deviceRepository;
|
||
_mapper = mapper;
|
||
}
|
||
|
||
public async Task CreateDeviceAsync(CreateDeviceDto deviceDto)
|
||
{
|
||
var device = _mapper.Map<Device>(deviceDto);
|
||
device.IsActive = true; // 默认激活
|
||
await _deviceRepository.AddAsync(device);
|
||
}
|
||
|
||
// ... 其他方法的实现
|
||
}
|
||
```
|
||
|
||
## 2. `DMS.Infrastructure` - 基础设施层设计
|
||
|
||
基础设施层提供具体的技术实现。
|
||
|
||
### 2.1. 仓储库实现 (`Repositories/`)
|
||
|
||
这里是 `DMS.Core` 中定义的仓储接口的具体实现。
|
||
|
||
```csharp
|
||
// 文件: DMS.Infrastructure/Repositories/DeviceRepository.cs
|
||
namespace DMS.Infrastructure.Repositories;
|
||
|
||
public class DeviceRepository : BaseRepository<Device, DbDevice>, IDeviceRepository
|
||
{
|
||
public DeviceRepository(ISqlSugarClient db, IMapper mapper) : base(db, mapper) { }
|
||
|
||
public async Task<List<Device>> GetActiveDevicesWithDetailsAsync()
|
||
{
|
||
// 使用 SqlSugar 的 Include/Navigate 功能来实现嵌套查询
|
||
var dbDevices = await _db.Queryable<DbDevice>()
|
||
.Where(d => d.IsActive)
|
||
.ToListAsync();
|
||
// ... 此处需要手动或通过AutoMapper的ProjectTo来加载关联数据
|
||
return _mapper.Map<List<Device>>(dbDevices);
|
||
}
|
||
|
||
public async Task<List<Device>> GetDevicesByProtocolAsync(ProtocolType protocol)
|
||
{
|
||
var dbDevices = await _db.Queryable<DbDevice>().Where(d => d.Protocol == (int)protocol).ToListAsync();
|
||
return _mapper.Map<List<Device>>(dbDevices);
|
||
}
|
||
}
|
||
```
|
||
|
||
### 2.2. 外部服务 (`Services/`)
|
||
|
||
#### 2.2.1. S7通信服务
|
||
|
||
负责与Siemens S7 PLC进行通信。
|
||
|
||
```csharp
|
||
// 文件: DMS.Infrastructure/Services/S7CommunicationService.cs
|
||
namespace DMS.Infrastructure.Services;
|
||
|
||
public interface IS7CommunicationService
|
||
{
|
||
Task<object> ReadVariableAsync(Device device, Variable variable);
|
||
Task ConnectAsync(Device device);
|
||
Task DisconnectAsync(Device device);
|
||
}
|
||
|
||
public class S7CommunicationService : IS7CommunicationService
|
||
{
|
||
// 使用第三方库,如 S7.Net Plus
|
||
private readonly ConcurrentDictionary<int, S7.Net.Plc> _clients = new();
|
||
|
||
public async Task ConnectAsync(Device device)
|
||
{
|
||
// ... 实现连接逻辑
|
||
}
|
||
|
||
public async Task<object> ReadVariableAsync(Device device, Variable variable)
|
||
{
|
||
// ... 实现读变量逻辑
|
||
// 1. 检查连接是否存在
|
||
// 2. 解析 variable.Address
|
||
// 3. 调用 S7.Net 的 Read 方法
|
||
// 4. 返回读取到的值
|
||
}
|
||
// ... 其他方法
|
||
}
|
||
```
|
||
|
||
#### 2.2.2. MQTT发布服务
|
||
|
||
负责将数据发布到MQTT Broker。
|
||
|
||
```csharp
|
||
// 文件: DMS.Infrastructure/Services/MqttPublishService.cs
|
||
namespace DMS.Infrastructure.Services;
|
||
|
||
public interface IMqttPublishService
|
||
{
|
||
Task PublishAsync(MqttServer server, string topic, string payload);
|
||
}
|
||
|
||
public class MqttPublishService : IMqttPublishService
|
||
{
|
||
// 使用第三方库,如 MQTTnet
|
||
public async Task PublishAsync(MqttServer server, string topic, string payload)
|
||
{
|
||
// ... 实现MQTT发布逻辑
|
||
}
|
||
}
|
||
```
|