简体   繁体   English

适当的编程设计问题

[英]Appropriate programming design questions

I have a few questions on good programming design. 我对好的程序设计有一些疑问。 I'm going to first describe the project I'm building so you are better equipped to help me out. 我将首先描述我正在构建的项目,以便您有更好的条件来帮助我。

I am coding a Remote Assistance Tool similar to TeamViewer, Microsoft Remote Desktop, CrossLoop. 我正在编写类似于TeamViewer,Microsoft远程桌面,CrossLoop的远程协助工具。 It will incorporate concepts like UDP networking (using Lidgren networking library), NAT traversal (since many computers are invisible behind routers nowadays), Mirror Drivers (using DFMirage's Mirror Driver ( http://www.demoforge.com/dfmirage.htm ) for realtime screen grabbing on the remote computer). 它将合并以下概念,例如UDP网络(使用Lidgren网络库),遍历NAT(由于如今许多计算机在路由器后面是不可见的),镜像驱动程序(使用DFMirage的镜像驱动程序( http://www.demoforge.com/dfmirage.htm ))远程计算机上的实时屏幕抓取)。

That being said, this program has a concept of being a client-server architecture, but I made only one program with both the functionality of client and server. 话虽这么说,该程序的概念是客户端-服务器体系结构,但是我只制作了一个兼具客户端服务器功能的程序。 That way, when the user runs my program, they can switch between giving assistance and receiving assistance without having to download a separate client or server module. 这样,当用户运行我的程序时,他们可以在提供帮助和接收帮助之间切换,而不必下载单独的客户端或服务器模块。

I have a Windows Form that allows the user to choose between giving assistance and receiving assistance. 我有一个Windows窗体,允许用户在提供帮助和接收帮助之间进行选择。 I have another Windows Form for a file explorer module. 我有另一个Windows窗体的文件浏览器模块。 I have another Windows Form for a chat module. 我还有一个用于聊天模块的Windows窗体。 I have another Windows Form form for a registry editor module. 我还有一个注册表编辑器模块的Windows窗体表单。 I have another Windows Form for the live control module. 我有另一个Windows窗体的实时控制模块。 So I've got a Form for each module, which raises the first question: 所以我为每个模块都有一个表格,这引发了第一个问题:

1. Should I process module-specific commands inside the code of the respective Windows Form? 1.我应该在相应Windows窗体的代码中处理特定于模块的命令吗? Meaning, let's say I get a command with some data that enumerates the remote user's files for a specific directory. 意思是说,我得到了一个包含一些数据的命令,该数据枚举了特定目录的远程用户文件。 Obviously, I would have to update this on the File Explorer Windows Form and add the entries to the ListView. 显然,我将不得不在文件资源管理器Windows窗体上对此进行更新,并将条目添加到ListView。 Should I be processing this code inside the Windows Form though? 我是否应该在Windows窗体中处理此代码? Or should I be handling this in another class (although I have to eventually pass the data to the Form to draw, of course). 还是应该在另一个类中处理这个问题(当然,尽管我最终必须将数据传递给Form进行绘制)。 Or is it like a hybrid in which I process most of the data in another class and pass the final result to the Form to draw? 还是像混合方法那样,我处理另一个类中的大多数数据并将最终结果传递给表单以进行绘制?

So I've got like 5-6 forms, one for each module. 所以我有5-6个表格,每个模块一个。 The user starts up my program, enters the remote machine's ID (not IP, ID, because we are registering with an intermediary server to enable NAT traversal), their password, and connects. 用户启动我的程序,输入远程计算机的ID(不是IP,ID,因为我们正在向中介服务器注册以启用NAT遍历),他们的密码并进行连接。 Now let's suppose the connection is successful. 现在,我们假设连接成功。 Then the user is presented with a form with all the different modules. 然后,向用户显示具有所有不同模块的表单。 So he can open up a File Explorer, or he can mess with the Registry Editor, or he can choose to Chat with his buddy. 因此,他可以打开文件资源管理器,或者可以与注册表编辑器混为一谈,或者可以选择与好友聊天。 So now the program is sort of idle, just waiting for the user to do something. 因此,现在程序有点空闲,只是在等待用户做某事。 If the user opens up Live Control, then the program will be spending most of it's time receiving packets from the remote machine and drawing them to the form to provide a 'live' view. 如果用户打开Live Control,则该程序将花费大部分时间从远程计算机接收数据包并将其绘制到窗体上以提供“实时”视图。

2. Second design question. 2.第二个设计问题。 A spin off question #1. 衍生问题#1。 How would I pass module-specific commands to their respective Windows Forms? 如何将特定于模块的命令传递给它们各自的Windows窗体? What I mean is, I have a class like "NetworkHandler.cs" that checks for messages from the remote machine. 我的意思是,我有一个类似“ NetworkHandler.cs”的类,用于检查来自远程计算机的消息。 NetworkHandler.cs is a static class globally accessible. NetworkHandler.cs是可全局访问的静态类。 So let's say I get a command that enumerates the remote user's files for a specific directory. 假设我得到一个列举特定目录的远程用户文件的命令。 How would I " give " that command to the File Explorer Form. 怎么会“ 放弃 ”该命令将文件浏览器表单。 I was thinking of making an OnCommandReceivedEvent inside NetworkHandler, and having each form register to that event. 我当时正在考虑在NetworkHandler内制作一个OnCommandReceivedEvent,并让每种表单都注册到该事件。 When the NetworkHandler received a command, it would raise the event, all forms would check it to see if it was relevant, and the appropriate form would take action. 当NetworkHandler收到命令时,它将引发该事件,所有表单将对其进行检查以查看其是否相关,然后适当的表单将采取行动。 Is this an appropriate/the best solution available? 这是合适/最佳的解决方案吗?

3. The networking library I'm using, Lidgren, provides two options for checking networking messages. 3.我使用的网络库Lidgren提供了两个检查网络消息的选项。 One can either poll ReadMessage() to return null or a message, or one can use an AutoResetEvent OnMessageReceived (I'm guessing this is like an event). 一个人可以轮询ReadMessage()以返回null或一条消息,或者可以使用AutoResetEvent OnMessageReceived(我猜这就像一个事件)。 Which one is more appropriate? 哪一个更合适?

  1. Put as little code as possible in the form. 在表格中放入尽可能少的代码。 You should create a seperate class/set of classes to handle this and make the form use them to draw. 您应该创建一个单独的类/一组类来处理此问题,并使表单使用它们进行绘制。

  2. An event seems like a good idea. 活动似乎是个好主意。 I wouldn't let the form subscribe, but have another class do the processing and just pass the processed data to the form (through another event). 我不会让表单订阅,而是让另一个类进行处理,只是将处理后的数据传递给表单(通过另一个事件)。

  3. I would use the event, because it probably checks async, which is what you want. 我将使用该事件,因为它可能检查异步,这就是您想要的。 You do not want to lock the form while waiting on messages. 您不想在等待消息时锁定表单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM