【工业自动化平台】 工程设计-依赖注入控制反转(2)

结合场景(Host + WPF + 动态插件 + PLC/SECS/GEM设备实例 + EventBus ),Autofac 子容器推荐采用 Root Container + Plugin LifetimeScope 架构。

核心思想:

Root Container 管平台级服务;每个插件/设备创建一个独立 LifetimeScope,管理自己的服务和资源。


1. 总体架构

text 复制代码
                Host.exe
                   |
                   |
          Autofac Root Container
             (IContainer)
                   |
     +-------------+--------------+
     |             |              |
  Logger        EventBus     Configuration
  SQLite        MessageBus   SystemService


                   |
              PluginManager
                   |
        BeginLifetimeScope()
                   |
     +-------------+--------------+
     |                            |
 DeviceA Scope               DeviceB Scope
 (ILifetimeScope)            (ILifetimeScope)

     |                            |
 DeviceA Service              DeviceB Service
 PLC Driver                   PLC Driver
 SECS Driver                  SECS Driver
 Alarm                        Vision
 Recipe                       Recipe

2. 项目分层设计

推荐:

复制代码
Platform
│
├── Platform.Core
│      │
│      ├── IServiceContainer
│      ├── IPlugin
│      ├── IPluginManager
│      ├── IEventBus
│      └── DTO
│
├── Platform.Application
│      │
│      ├── DeviceManager
│      └── PluginApplicationService
│
├── Platform.Infrastructure
│      │
│      ├── Autofac
│      │      ├── AutofacServiceContainer
│      │      └── PluginLifetimeManager
│      │
│      ├── SQLite
│      ├── Logging
│      └── Communication
│
├── Platform.Host
│      │
│      └── Program.cs
│
├── Platform.UI.WPF
│      │
│      ├── View
│      ├── ViewModel
│      └── UIService
│
└── Plugins
       |
       ├── Siemens.Plugin
       |
       ├── SecsGem.Plugin
       |
       └── Vision.Plugin

3. Root Container设计

Host启动:

csharp 复制代码
var builder = new ContainerBuilder();


// 平台服务

builder.RegisterType<EventBus>()
       .As<IEventBus>()
       .SingleInstance();


builder.RegisterType<Logger>()
       .As<ILogger>()
       .SingleInstance();


builder.RegisterType<PluginManager>()
       .As<IPluginManager>()
       .SingleInstance();


var container = builder.Build();

Root负责:

复制代码
生命周期:
Application级

对象:
Logger
EventBus
配置
数据库
插件管理器

4. PluginManager设计

不要暴露 Autofac:

接口:

csharp 复制代码
public interface IPluginManager
{

    void Load(string path);


    void Unload(string name);

}

实现:

csharp 复制代码
public class AutofacPluginManager 
    : IPluginManager
{

    private readonly ILifetimeScope _root;


    private readonly Dictionary<string,ILifetimeScope>
        _scopes = new();


    public AutofacPluginManager(
        ILifetimeScope root)
    {
        _root=root;
    }


    public void Load(string path)
    {

        var assembly =
            Assembly.LoadFrom(path);


        var scope =
        _root.BeginLifetimeScope(
        builder =>
        {

            builder.RegisterAssemblyModules(
                assembly);

        });


        _scopes[path]=scope;

    }


    public void Unload(string path)
    {

        if(_scopes.TryGetValue(path,out var scope))
        {
            scope.Dispose();

            _scopes.Remove(path);
        }

    }

}

5. 插件注册方式

插件:

复制代码
Siemens.Plugin.dll

定义:

csharp 复制代码
public class SiemensModule 
        : Module
{

protected override void Load(
ContainerBuilder builder)
{

    builder.RegisterType<SiemensDriver>()
           .As<IPlcDriver>()
           .SingleInstance();


    builder.RegisterType<DeviceService>()
           .As<IDeviceService>()
           .SingleInstance();

}

}

加载:

csharp 复制代码
builder.RegisterAssemblyModules(
    assembly);

自动注册:

复制代码
DeviceService
SiemensDriver
AlarmService
RecipeService

6. 子容器服务依赖父容器

例如:

csharp 复制代码
public class DeviceService
{

private readonly ILogger _logger;

private readonly IEventBus _bus;


public DeviceService(
 ILogger logger,
 IEventBus bus)
{

}

}

解析:

复制代码
DeviceScope

DeviceService
       |
       |
       + ILogger
       |
       + EventBus


没有

向Root找

结果:

复制代码
Root Logger
Root EventBus

注入成功

7. 多设备实例设计

例如两个PLC:

text 复制代码
Root


Device001 Scope

 PLC Driver
 DeviceService


Device002 Scope

 PLC Driver
 DeviceService

代码:

csharp 复制代码
var device1 =
root.BeginLifetimeScope(builder =>
{
    builder.RegisterType<SiemensDriver>()
    .As<IPlcDriver>();
});


var device2 =
root.BeginLifetimeScope(builder =>
{
    builder.RegisterType<SiemensDriver>()
    .As<IPlcDriver>();
});

结果:

复制代码
device1 PlcDriver != device2 PlcDriver

每个设备独立:

  • TCP连接
  • 状态
  • Alarm
  • Thread
  • Timer

8. 生命周期设计

推荐:

对象 Scope 生命周期
ILogger Root SingleInstance
EventBus Root SingleInstance
Configuration Root SingleInstance
PluginManager Root SingleInstance
PLC Driver Device Scope SingleInstance
SECS Driver Device Scope SingleInstance
DeviceService Device Scope SingleInstance
ViewModel UI Scope InstancePerDependency

9. WPF结合

不要:

复制代码
WPF
 |
 Autofac Container

推荐:

复制代码
Host

Root Container

       |
       |
    UIService

       |
       |
 WPF MainWindow

注册:

csharp 复制代码
builder.RegisterType<WpfUIService>()
.As<IUIService>();

启动:

csharp 复制代码
var ui =
container.Resolve<IUIService>();

ui.ShowMainWindow();

10. 不暴露Autofac

最终依赖:

复制代码
业务插件

    |
    |
 Platform.Core


Platform.Infrastructure

    |
    |
 Autofac

插件不知道:

复制代码
Autofac
IContainer
ILifetimeScope

推荐最终模型

复制代码
                Host

          Autofac Root
          IContainer

              |
              |
       PluginManager

              |
    +---------+----------+
    |                    |

Device Scope          Device Scope

ILifetimeScope        ILifetimeScope

PLC                  SECS/GEM
Vision               Recipe
Alarm                Service

这个架构可以支持:

  • 动态加载 DLL
  • 动态卸载设备
  • 多设备并行
  • WPF/WinForms切换
  • PLC/SECS/GEM插件化
  • 服务生命周期隔离

非常适合工业上位机平台。

相关推荐
天远API12 小时前
零信任架构实战:基于天远单人婚姻查询构建自动化联合贷款审计网关
java·人工智能·架构·自动化
工业机器人视觉检测设备12 小时前
云汇智能金属网焊后剪切装置 非标自动化定制助力产线提质增效
自动化·自动化设备
2601_9639067812 小时前
桌面自动化工具|零代码录制自动化,无需联网本地离线用
运维·自动化
海宇AI12 小时前
零信任架构实战:基于海宇公安二要素认证即时版构建自动化灵活用工准入网关
运维·人工智能·架构·自动化
荆棘鸟智能12 小时前
AI算法持续学习与迭代怎么做?从数据闭环到灰度发布的MLOps工程实践
人工智能·算法·架构·边缘计算
️公子14 小时前
DeepSeek V4.1 Flash 今日接管 Pro 流量:552B MoE + 非对称架构,Agent 推理成本怎么砍?
架构·开源·大模型·api·agent·deepseek
这个DBA有点耶14 小时前
数据库数据同步解决方案怎么选?6款主流工具横向对比与信创选型指南
数据库·架构·dba
小羊没烦恼!14 小时前
Memory 记忆设计讨论:Agent Memory 的安全边界:权限、删除、版本与回执
java·大数据·word·powerpoint·.net
科技研学社14 小时前
一次性内裤制造的工艺痛点与自动化工艺改良方案
人工智能·自动化
微三云 - 廖会灵 (私域系统开发)14 小时前
存量竞争下平台裂变的合规路径研究 —— 众薪广告积分模式机制、边界与落地风险
重构·自动化·零售