微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我有我的设备在主/从配置,我正在开发 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中的所有内容都可以.确保它是通用的,主人不应该知道他正在处理什么类型的奴隶.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐