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

@@ -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