【工业自动化平台】 工程设计-项目分层目录设计(1)
总想做点什么
方向:
- UI(WPF/WinForms)
- MVVM
- Application只负责插件生命周期和应用协调
- 设备Domain按插件拆分
- Infrastructure基础服务
- Driver底层通信驱动
- EventBus / CommandBus / DataBus
- Core定义契约
- Common工具程序集
设计工程目录如下:
text
Industrial.Platform.sln
│
├── 01.Core 核心契约层
│ │
│ └── Platform.Core
│ │
│ ├── Interfaces 接口定义
│ │ ├── IPlugin.cs
│ │ ├── IPluginManager.cs
│ │ ├── IEventBus.cs
│ │ ├── ICommandBus.cs
│ │ ├── IDataBus.cs
│ │ ├── IRepository.cs
│ │ └── IDriver.cs
│ │
│ ├── Messages 消息契约
│ │ ├── EventMessage.cs
│ │ ├── CommandMessage.cs
│ │ └── DataMessage.cs
│ │
│ ├── Events 系统事件
│ │ ├── DeviceStateEvent.cs
│ │ ├── AlarmEvent.cs
│ │ └── PluginStateEvent.cs
│ │
│ ├── Models 基础模型
│ │ ├── DeviceInfo.cs
│ │ ├── PluginInfo.cs
│ │ └── StateInfo.cs
│ │
│ └── Exceptions
│
│
├── 02.Common 通用工具层
│ │
│ └── Platform.Common
│ │
│ ├── Extensions
│ │ ├── StringExtensions.cs
│ │ └── ObjectExtensions.cs
│ │
│ ├── Helpers
│ │ ├── JsonHelper.cs
│ │ ├── ReflectionHelper.cs
│ │ └── DateTimeHelper.cs
│ │
│ ├── Serialization
│ │
│ ├── Threading
│ │
│ └── Result
│
│
├── 03.Application 应用协调层
│ │
│ └── Platform.Application
│ │
│ ├── Services
│ │ ├── PluginApplicationService.cs
│ │ └── DeviceApplicationService.cs
│ │
│ ├── Commands
│ │
│ └── DTO
│
│
├── 04.Runtime 平台运行时
│ │
│ └── Platform.Runtime
│ │
│ ├── PluginManager
│ │ ├── PluginLoader.cs
│ │ └── PluginContainer.cs
│ │
│ ├── ServiceManager
│ │
│ ├── AssemblyLoader
│ │
│ └── Lifecycle
│
│
├── 05.Infrastructure 基础设施层
│ │
│ └── Platform.Infrastructure
│ │
│ ├── EventBus
│ │ ├── LocalEventBus.cs
│ │ └── RabbitMQEventBus.cs
│ │
│ ├── CommandBus
│ │
│ ├── DataBus
│ │
│ ├── Persistence
│ │ ├── SQLite
│ │ ├── EFCore
│ │ └── Repository
│ │
│ ├── Logging
│ │
│ ├── Configuration
│ │
│ ├── Cache
│ │
│ └── FileStorage
│
│
├── 06.Driver 底层驱动层
│ │
│ └── Platform.Driver
│ │
│ ├── Abstractions
│ │ ├── IPlcDriver.cs
│ │ ├── ICameraDriver.cs
│ │ └── IMotionDriver.cs
│ │
│ ├── PLC
│ │ ├── Siemens
│ │ ├── Mitsubishi
│ │ ├── Omron
│ │ └── Modbus
│ │
│ ├── Motion
│ │ ├── Servo
│ │ └── Robot
│ │
│ ├── Camera
│ │ ├── Basler
│ │ └── Hikvision
│ │
│ ├── SECS
│ │ ├── HSMS
│ │ ├── SECS-II
│ │ └── GEM
│ │
│ └── Network
│ ├── TCP
│ ├── UDP
│ └── SerialPort
│
│
├── 07.Domain.Plugin 设备领域插件层
│ │
│ ├── Tester.Plugin
│ │ │
│ │ ├── Domain
│ │ ├── StateMachine
│ │ ├── Process
│ │ ├── Recipe
│ │ ├── Alarm
│ │ └── PluginEntry.cs
│ │
│ │
│ ├── Robot.Plugin
│ │
│ ├── SMT.Plugin
│ │
│ ├── Vision.Plugin
│ │
│ └── SECS.Plugin
│
│
├── 08.UI 表现层
│ │
│ ├── Platform.UI.WPF
│ │ │
│ │ ├── Views
│ │ ├── Controls
│ │ ├── Themes
│ │ └── Resources
│ │
│ │
│ └── Platform.UI.WinForms
│
│
├── 09.ViewModels MVVM层
│ │
│ └── Platform.ViewModels
│ │
│ ├── MainViewModel.cs
│ ├── DeviceViewModel.cs
│ ├── AlarmViewModel.cs
│ └── PluginViewModel.cs
│
│
└── 10.Host 主程序
Platform.Client.exe
│
├── Startup
├── DI注册
├── Configuration
└── App.xaml
依赖方向
严格控制:
text
Host
|
↓
UI
|
↓
ViewModel
|
↓
Application
|
↓
Domain.Plugin
|
↓
Core
Infrastructure ─────→ Core
Driver ─────────────→ Core
Runtime ────────────→ Core
Common ← 所有层引用
各层最终职责
| 层 | 职责 | 示例 |
|---|---|---|
| Core | 定义规则 | IPlugin、IEventBus |
| Common | 通用工具 | Json、反射 |
| Application | 应用协调 | 启动插件 |
| Runtime | 运行管理 | 加载DLL |
| Infrastructure | 公共能力 | SQLite、日志 |
| Driver | 硬件通信 | PLC、SECS |
| Domain.Plugin | 设备大脑 | 状态机、工艺 |
| ViewModel | 界面逻辑 | Command、Binding |
| UI | 显示 | WPF/WinForms |
| Host | 组装启动 | DI、配置 |
这个结构更适合你的目标:
"工业设备上位机平台化开发"
后续增加:
- 新设备 → 新 Domain.Plugin
- 新PLC → 新 Driver
- 新通信 → 新 Infrastructure
- 新界面 → 新 UI
主程序无需修改。
下面对每一层进行详细说明。
整体定位:
Host负责组装系统,Runtime负责运行插件,Application负责协调,Domain Plugin负责设备逻辑,Driver负责硬件通信,Infrastructure提供平台能力,Core定义契约。
1. Core 核心契约层
定位
Core是整个系统的公共协议层。
它不包含任何业务实现,只定义:
- 谁和谁通信
- 模块之间遵循什么规则
- 数据如何传递
类似操作系统的 API 定义。
主要内容
项目:
Platform.Core
结构:
Core
├── Interfaces
├── Messages
├── Events
├── Commands
├── Models
└── Exceptions
1.1 Interfaces(接口)
定义系统能力。
例如插件:
csharp
public interface IPlugin
{
string Id { get; }
string Name { get; }
Task InitializeAsync();
Task StartAsync();
Task StopAsync();
}
所有设备插件实现:
PLC.Plugin
Robot.Plugin
Vision.Plugin
1.2 EventBus接口
csharp
public interface IEventBus
{
void Publish<T>(T message);
void Subscribe<T>(
Action<T> handler);
}
Core只定义:
"有消息机制"
不关心:
- 内存事件
- RabbitMQ
- MQTT
1.3 Driver接口
例如:
csharp
public interface IPlcDriver
{
Task Connect();
Task Disconnect();
Task Write(
string address,
object value);
}
实现:
SiemensDriver
MitsubishiDriver
1.4 消息模型
例如:
设备状态:
csharp
public class DeviceStateChangedEvent
{
public string DeviceId {get;set;}
public string State {get;set;}
}
用于:
PLC Plugin
发布
↓
UI显示
2. Common 通用工具层
定位
开发辅助库。
原则:
谁都可以使用,但不能包含业务。
项目:
Platform.Common
内容:
2.1 扩展方法
例如:
csharp
public static class StringExtensions
{
public static bool IsNull(
this string value)
{
return string.IsNullOrEmpty(value);
}
}
2.2 序列化
例如:
JsonHelper
XmlHelper
BinarySerializer
用途:
- 配置文件
- 通信数据
- 缓存
2.3 反射工具
插件加载:
csharp
AssemblyHelper.Load()
用于:
扫描Plugins目录
加载DLL
2.4 Result统一返回
csharp
Result<T>
统一:
成功
失败
错误信息
数据
3. Application 应用协调层
定位
Application不是设备业务。
你的设计:
只负责插件开启、关闭、协调。
项目:
Platform.Application
职责:
3.1 插件管理调用
例如:
用户:
点击启动设备
流程:
ViewModel
↓
ApplicationService
↓
PluginManager
↓
Plugin.Start()
代码:
csharp
public class PluginApplicationService
{
private readonly IPluginManager manager;
public Task Start(
string plugin)
{
return manager.Start(plugin);
}
}
3.2 应用状态管理
例如:
系统启动
插件加载
插件运行
插件停止
3.3 不包含:
❌ PLC逻辑
❌ Recipe流程
❌ 设备状态机
这些属于 Domain Plugin。
4. Runtime 运行时层
定位
整个软件的"操作系统"。
负责:
- DLL加载
- 插件生命周期
- DI容器
- 服务管理
项目:
Platform.Runtime
4.1 PluginManager
核心:
csharp
public class PluginManager
{
Load();
Start();
Stop();
}
流程:
启动程序
↓
扫描 Plugins
↓
找到 xxx.Plugin.dll
↓
反射创建对象
↓
注册DI
↓
启动插件
4.2 AssemblyLoader
负责:
Assembly.Load()
支持:
- 动态加载
- 插件隔离
- 升级
5. Infrastructure 基础设施层
定位
提供技术能力。
不包含设备业务。
项目:
Platform.Infrastructure
5.1 EventBus实现
Core:
IEventBus
Infrastructure:
LocalEventBus
RabbitMQEventBus
例如:
单机:
内存事件
分布式:
RabbitMQ
5.2 SQLite
负责:
- 数据保存
- 查询
- ORM
例如:
保存:
报警记录
运行日志
Recipe参数
结构:
Persistence
├── DbContext
├── Repository
└── Migration
5.3 Logger
例如:
Serilog
NLog
统一:
Debug
Info
Error
5.4 Configuration
负责:
设备配置
系统配置
插件配置
例如:
json
{
"PLC":
{
"IP":"192.168.1.10"
}
}
6. Driver 驱动层
定位
连接真实硬件。
属于:
软件到设备的最后一公里。
项目:
Platform.Driver
PLC
结构:
Driver.PLC
├── Siemens
├── Mitsubishi
├── Omron
└── Modbus
负责:
TCP
协议
数据转换
读写
Camera
例如:
Basler SDK
Hikvision SDK
Cognex SDK
Motion
例如:
Servo
Robot
Axis
SECS
拆分:
HSMS
SECS-II
GEM
7. Domain.Plugin 设备领域插件层
定位
这是设备的"大脑"。
每一种设备一个插件。
例如:
Tester.Plugin.dll
Robot.Plugin.dll
内部:
Tester.Plugin
├── Domain
├── StateMachine
├── Process
├── Recipe
├── Alarm
└── PluginEntry
Domain
定义:
设备对象:
Tester
Wafer
Carrier
StateMachine
例如:
Idle
↓
Ready
↓
Run
↓
Complete
↓
Error
Process
工艺流程:
例如:
Load
Align
Test
Unload
Recipe
参数:
温度
速度
时间
8. ViewModel层
定位
MVVM中的VM。
连接:
UI
↓
ViewModel
↓
Application
负责:
- 数据绑定
- Command
- UI状态
例如:
csharp
StartCommand
StopCommand
ResetCommand
不负责:
❌ PLC通信
❌ 数据库
❌ 设备控制
9. UI层
定位
显示。
支持:
WPF
WinForms
负责:
- 页面
- 控件
- 样式
- 用户操作
不包含:
业务代码
PLC代码
数据库代码
10. Host主程序
定位
系统入口。
负责:
启动所有模块。
流程:
Main()
↓
创建DI
↓
加载配置
↓
初始化Runtime
↓
加载插件
↓
显示UI
最终数据流
控制流
用户
↓
UI
↓
ViewModel
↓
Application
↓
Runtime
↓
Domain Plugin
↓
Driver
↓
设备
状态流
设备
↓
Driver
↓
Domain Plugin
↓
EventBus
↓
ViewModel
↓
UI
数据保存
设备
↓
Plugin
↓
Repository
↓
SQLite
最终架构定位总结
| 层 | 一句话 |
|---|---|
| Core | 定义规则 |
| Common | 提供工具 |
| UI | 显示界面 |
| ViewModel | 连接界面和系统 |
| Application | 协调应用 |
| Runtime | 管理插件运行 |
| Domain Plugin | 设备大脑 |
| Infrastructure | 提供平台能力 |
| Driver | 连接硬件 |
| Host | 启动系统 |
这个分层适合构建 EAP、SCADA、半导体设备控制软件、智能制造上位机平台。