最佳实践:处理代表硬件的类中的硬件断开连接
我正在使用 VB.Net 开发数据记录软件。我开发了一些实现通用接口的类,该接口可与我公司生产的许多数据记录器配合使用。这些类处理低级硬件接口并公开用于访问数据记录器记录的通用方法。
我的问题是,处理某人突然断开硬件连接的良好做法是什么?我应该抛出异常、引发事件还是在某种状态数据包中返回 false?
我的基本想法是,我希望对象在它代表的设备断开连接并失败时通知发送者,然后以某种方式与 DeviceLocator 类(自动检测设备并维护集合)进行通信,告知它已断开连接并删除将其从设备中收集并处理。
有什么建议吗?
I'm working in VB.Net developing data logging software. I've developed classes implementing a common interface that works with many of the data loggers my company produces. These classes handle the low-level hardware interfacing and expose common methods for accessing the data logger's records.
My issue is what would be a good practice to handle someone suddenly disconnecting the hardware? Should I throw an exception, raise an event, or return false in some sort of status packet?
My basic thought is that I want the object to notify the sender when the device it represents is disconnected and to fail and then somehow communicate to the DeviceLocator class (which auto-detects devices and maintains a collection) that it has been disconnected and to remove it from the collection of devices and dispose.
Any tips?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在较高的层次上,我认为您应该从“驱动程序”层返回结果代码或错误消息。接下来,您可能希望将数据包公开给不同类型的客户端。如果您有 WCF 客户端或调用 Web 服务的移动客户端,则抛出异常或引发事件是没有用的。您必须创建另一个层来捕获这些条件并为每个客户端转换它们,那么为什么不在尽可能低的级别上执行此操作呢?
更详细的答案取决于您的实际架构。例如,您是否使用委托来通知消费者新数据?为什么不让一个委托在出现错误情况时被调用。
At a high level, I think you should return a result code or an error message from your "driver" layer. Down the road you may want to expose the data packets to different types of clients. If you have a WCF client, or a mobile client calling web services, throwing an exception or raising an event is of no use. You would have to create another layer that catches these conditions and translates them for each client, so why no do this at the lowest possible level.
A more detailed answer depends on your actual architecture. Do you use delegates, for instance, to notify a consumer of new data? Why not have a delegate that is being called when there is an error condition.