2015-02-24 27 views
4

我有我的设备在主/从配置,我正在开发一个WPF/MVVM应用程序。主人真的知道他们的奴隶是什么?

我有一个COM对象(所有这些都实现IDevice)代表外部设备/ Modbus网络的状态,并附加到SerialPortSocket之类的东西上。这意味着,在通过VersionRevision识别设备后,我会拨打IDevice device = DeviceManager.GetDevice(version, revision);来获取表示出厂默认设备状态的对象。

现在我有一个IDevice,我可以调用它的API来获取诸如List<ushort> GetCoreRegisters()List<ushort> GetAllRegisters()之类的东西。据说,在读取寄存器值之后,我必须调用IDevice的API来设置值:device.SetItemValue(registerAddress, registerValue)以便更新网络另一端的设备状态。

我创建了Master类型,它处理通信层(SocketSerialPort)。

在我的应用程序的当前状态,我呼吁像我认为的车型之一下面的伪代码(单击一个按钮后):

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() 

我的问题是,这是正确的设计,或者我应该在Master和识别装置(S)后有List<IDevice> Devices,我会做更多的东西一样:

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); 

,所有的GetRegisterSetRegisterValue逻辑里面Master - 意思大师知道关于IDevice的所有信息,并处理配置从属状态的逻辑。

回答

1

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

你的第一个例子似乎有相当多的领域知识和业务逻辑。这应该去模型中的某处。从我所看到的,您的Master类似乎是一个合理的地方。

要回答这个问题:大师们对他们的奴隶非常了解,他们当然足以“驱动”或“使用”他们。因此,它知道IDevice中的所有内容都可以。确定其通用但主人不应该知道什么类型的他正在处理的奴隶。

相关问题