IOC容器添加构造函数注入(DLL类中使用AutoRegisterAttribute特性标注的类,会在流程启动时自动注册),提高可读性。

This commit is contained in:
fengjiayi
2024-09-30 02:45:49 +08:00
parent 7a310ff1df
commit ccf539b90f
23 changed files with 615 additions and 280 deletions

View File

@@ -2,6 +2,7 @@
using Net461DllTest.Enums;
using Net461DllTest.Signal;
using Net461DllTest.Utils;
using Serein.Library.Attributes;
using Serein.Library.NodeFlow.Tool;
using System;
@@ -11,6 +12,7 @@ namespace Net461DllTest.Device
/// <summary>
/// 官方文档如果没有主动Open则会每次读写操作的时候自动打开自动和关闭连接这样会使读写效率大大减低。所以建议手动Open和Close。
/// </summary>
[AutoRegister]
public class SiemensPlcDevice : ChannelFlowTrigger<OrderSignal>
{
public SiemensClient Client { get; set; }

View File

@@ -21,23 +21,18 @@ namespace Net461DllTest.LogicControl
GetPparkingSpace,
}
[AutoRegister]
[DynamicFlow]
public class ParkingLogicControl
{
[AutoInjection]
public PrakingDevice PrakingDevice { get; set; }
private readonly PrakingDevice PrakingDevice;
[NodeAction(NodeType.Init)]
public void Init(IDynamicContext context)
public ParkingLogicControl(PrakingDevice PrakingDevice)
{
context.Env.IOC.Register<PrakingDevice>();
this.PrakingDevice = PrakingDevice;
}
[NodeAction(NodeType.Flipflop, "等待车位调取命令",ReturnType=typeof(string))]
public async Task<IFlipflopContext> GetPparkingSpace(ParkingCommand parkingCommand = ParkingCommand.GetPparkingSpace)
{

View File

@@ -19,21 +19,21 @@ using System.Threading.Tasks;
namespace Net461DllTest.LogicControl
{
[AutoRegister]
[DynamicFlow]
public class PlcLogicControl
{
[AutoInjection]
public SiemensPlcDevice MyPlc { get; set; }
private readonly SiemensPlcDevice MyPlc;
public PlcLogicControl(SiemensPlcDevice MyPlc)
{
this.MyPlc = MyPlc;
}
#region 退
[NodeAction(NodeType.Init)] // Init 初始化事件,流程启动时执行
public void Init(IDynamicContext context)
{
context.Env.IOC.Register<SiemensPlcDevice>(); // 注册Plc设备
context.Env.IOC.Register<WebServer>(); // 注册Web服务
// // 注册控制器
context.Env.IOC.Run<IRouter>(router => {
router.RegisterController(typeof(ApiController));
@@ -146,14 +146,14 @@ namespace Net461DllTest.LogicControl
if (MyPlc.State == PlcState.Runing)
{
if (!varInfo.IsProtected)
if (varInfo.IsProtected)
{
MyPlc.Write(varInfo, value);
Console.WriteLine($"PLC变量{varInfo}写入数据:{value}");
Console.WriteLine($"PLC变量{varInfo}当前禁止写入");
}
else
{
Console.WriteLine($"PLC变量{varInfo}当前禁止写入");
MyPlc.Write(varInfo, value);
Console.WriteLine($"PLC变量{varInfo}写入数据:{value}");
}
}
else

View File

@@ -1,4 +1,5 @@
using Net461DllTest.Signal;
using Net461DllTest.Device;
using Net461DllTest.Signal;
using Net461DllTest.ViewModel;
using Serein.Library.Api;
using Serein.Library.Attributes;
@@ -11,7 +12,7 @@ using System.Windows.Forms;
namespace Net461DllTest.LogicControl
{
[AutoRegister]
public class ViewManagement
{
@@ -46,8 +47,11 @@ namespace Net461DllTest.LogicControl
[DynamicFlow]
public class ViewLogicControl
{
[AutoInjection]
public ViewManagement ViewManagement { get; set; }
private readonly ViewManagement ViewManagement;
public ViewLogicControl(ViewManagement ViewManagement)
{
this.ViewManagement = ViewManagement;
}
[NodeAction(NodeType.Init)]
public void Init(IDynamicContext context)

View File

@@ -1,6 +1,7 @@
using Net461DllTest.Device;
using Net461DllTest.Signal;
using Net461DllTest.ViewModel;
using Serein.Library.Api;
using Serein.Library.Attributes;
using System;
using System.Collections.Generic;
@@ -16,11 +17,17 @@ namespace Net461DllTest
{
public partial class FromWorkBenchView : Form
{
[AutoInjection]
public FromWorkBenchViewModel ViewModel { get; set; }
public FromWorkBenchView()
private FromWorkBenchViewModel ViewModel;
public FromWorkBenchView(IFlowEnvironment env)
{
ViewModel = env.IOC.Instantiate<FromWorkBenchViewModel>();
InitializeComponent();
Init();
}
public void Init()
{
listBox1.Items.Clear();
var enumValues = Enum.GetValues(typeof(OrderSignal)).Cast<OrderSignal>();
foreach (var value in enumValues)

View File

@@ -11,8 +11,12 @@ namespace Net461DllTest.ViewModel
{
public class FromWorkBenchViewModel
{
[AutoInjection]
public SiemensPlcDevice Device { get; set; }
public FromWorkBenchViewModel(SiemensPlcDevice Device)
{
this.Device = Device;
}
private SiemensPlcDevice Device;
public string Name { get; set; }
public string GetDeviceInfo()
@@ -20,7 +24,6 @@ namespace Net461DllTest.ViewModel
return Device?.ToString();
}
public void Trigger(OrderSignal signal,string spcaeNumber)
{
_ = Task.Run(() =>