加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 大师们真正了解他们的奴隶是什么?

发布时间:2020-12-15 22:11:51 所属栏目:百科 来源:网络整理
导读:我有我的设备在主/从配置,我正在开发 WPF / MVVM应用程序. 我有一个COM对象(所有这些都实现IDevice),它代表外部设备/ Modbus网络的状态,并附加到SerialPort或Socket之类的东西.这意味着,在通过其版本和修订版识别设备之后,我调用IDevice device = DeviceMana
我有我的设备在主/从配置,我正在开发 WPF / MVVM应用程序.

我有一个COM对象(所有这些都实现IDevice),它代表外部设备/ Modbus网络的状态,并附加到SerialPort或Socket之类的东西.这意味着,在通过其版本和修订版识别设备之后,我调用IDevice device = DeviceManager.GetDevice(version,revision);获取表示出厂默认设备状态的对象.

既然我有一个IDevice,我可以调用它的API来获取List< ushort>之类的内容. GetCoreRegisters()和List< ushort> GetAllRegisters().话虽如此,在读取寄存器值后,我必须调用IDevice的API来设置值:device.SetItemValue(registerAddress,registerValue),以便更新网络另一端的设备状态.

我创建了一个处理通信层(Socket与SerialPort)的Master类型.

在我的应用程序的当前状态中,我在我的一个视图模型中调用类似下面的伪代码(在单击按钮之后):

IDevice device = null;
profile = SelectedProfile
master = MasterFactory.GetMaster(profile.Name)
master.Open() //Connects or Opens SerialPort/Socket
if(master.DeviceCheck(profile.SlaveAddress))
{
    KeyValuePair<ushort,ushort> info = await master.IdentifyDeviceAsync(profile.SlaveAddress);
    device = DeviceManager.GetDevice(info.Key,info.Value)
    initList = device.GetInitializationRegisters()
    initValues = await master.ReadRegisters(profile.SlaveAddress,initList)
    for(int i = 0; i < initList; i++)
        device.SetRegisterValue(initList[i],initValues[i]);

    allRegisters = device.GetAllRegisters();
    allValues = await master.ReadRegisters(profileSlaveAddress,allRegisters)
    for ... repeat
}
if device != null,DevicesInViewModel.Add(device)
master.Close()

我的问题是,这是正确的设计,还是我应该有一个List< IDevice> Master中的设备和识别设备后,我会做更多的事情:

device = DeviceManager.GetDevice(info.Key,info.Value);
master.Add(device);
// possibly add more devices if needed
List<IDevice> devices = master.ReadDevices()
if devices != null,DevicesInViewModel.AddRange(devices);

所有GetRegister和SetRegisterValue逻辑都在Master中 – 这意味着Master知道IDevice的所有内容并处理配置Slave状态的逻辑.

解决方法

在理想的世界中查看模型代码非常简单.你当然不希望在其中运行长操作和循环.视图模型包含用于处理来自视图的命令的逻辑,就是这样.

您的第一个示例似乎有相当多的领域知识和业务逻辑.这应该在模型中的某个地方.从我所看到的,你的大师班似乎是一个合理的地方.

要回答这个名义上的问题:大师们对他们的奴隶有很多了解,当然足以“驱使”或“使用”他们.因此,它知道IDevice中的所有内容都可以.确保它是通用的,主人不应该知道他正在处理什么类型的奴隶.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读