在 Winforms 中编写代码的最佳且合适的方式是什么?

发布于 2024-08-31 15:56:54 字数 570 浏览 11 评论 0原文

编写代码的最佳方式是什么?

(1) 就像直接在button_click()事件中编写代码一样。

(2)创建我在 button_click() 事件中编写的代码的函数,并将该函数写入一个类中,然后我应该在 button_Click() 中调用该函数> event.Like 是否称为三层方法来编写代码?

就像在 button_Click() 事件中一样,我编写了代码来将数据表中的记录保存在 csv 文件中。所以我应该在 button_Click() 中编写该代码> 事件,或者我应该创建一个新函数和一个新类,并在该函数(即新类)中编写该代码,并在 button_Click() 事件中调用该函数。

这只是一个示例,但是我正在谈论我的应用程序中编写的所有代码,这是编写代码的适当且最佳的方式以及有什么好处?请注意,我在 Winforms 中使用 c# 编写代码。

What is the best way to write the code ?

(1) Like directly writing the code in the button_click() event.

or

(2) Make the function of that code which I write in button_click() event and write this function in one class and then that function I should call in the button_Click() event.Like is it called three-tier approach to write the code ?

Like in button_Click() event I write the code to save the records in csv file from datatable.So I should write that code in button_Click() event or I should make one new function and one new class and write that code in that function which is the new class and calling that function in button_Click() event.

This is only one example but I am talking about all the code written in my application that which is the appropriate and best way to write the code and what are the benefits ? Note that I write the code in Winforms with c#.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

十年九夏 2024-09-07 15:56:54

您应该在不同的类中使用单独的函数。您应该这样做,因为您将使代码可重用,并在用户界面和应用程序逻辑之间创建良好的分离。例如,您可以像这样更改 UI,而不会影响应用程序的其余部分。

另请查看 MVC 模式,你会更好地理解整个想法。

我认为应该使用第一个选项的唯一情况是当它执行一些会影响 UI 的操作时,我仍然会在 Form 类内的单独函数中创建它。

如果它影响 UI,它应该位于同一个类中,因为它是相关的,例如,如果它是刷新网格的代码,我会将其放在同一个 Form 类中的单独方法中,因为这可以在内部的不同位置使用它。因此,更改 UI 对应用程序没有影响,您只需使代码可重用即可。可维护。

You should go for the separate function in a different class. You should do that because you'll make the code reusable and create a decent separation between the user interface and application logic. Like this, you could for example change the UI without affecting the rest of the application.

Also take a look at MVC pattern, you'll understand better the whole idea.

The only situation where i think that the first option should be used is when it does some action that will affect the UI, and still i'll create this in a separate function inside the Form class.

If it's affecting the UI, it should be in the same class because it's related and for example if it's a code to refresh a grid i'll put this in a separate method inside the same Form class because this could be used in different places inside it. So changing the UI has no impact on the application, you just make your code reusable & maintainable.

一张白纸 2024-09-07 15:56:54

这一切都取决于具体情况。

如果您要更新表单,那么最好在表单中包含更新代码。但是,如果有大量处理,那么最好设计一个单独的类来处理这项工作。

这一切都取决于具体情况。

It all depends on situation.

If you are going to make updates to the Form, then it's better to have the updating code in the Form. However, if there are lots of processing, then surely it's better design to have a separate class handle the job.

It all depends on situation.

清泪尽 2024-09-07 15:56:54

通常,您不希望事件处理程序中有任何逻辑,因为 GUI 往往会提供冗余机制(上下文菜单、菜单栏、工具栏、加速键)来触发相同的命令,并且事件签名并不兼容所有的这些。那么问题就变成了你的公共函数应该放在 Form 类中还是放在数据模型中。

我经常从表单中的逻辑开始,然后根据需要将其重构为模型类。许多小型应用程序永远不会变得足够大,以至于需要多个类来实现可维护性。只要避免代码重复(即复制+粘贴),那么如果您发现需要的话,稍后重构就会很简单。

Generally, you don't want any logic in the event handler, since GUIs tend to provide redundant mechanisms (context menu, menu bar, toolbar, accelerator key) for triggering the same command, and the event signatures aren't compatible for all of these. Then the question becomes whether your common function should go in the Form class or into the data model.

I often start out with the logic in the Form and then refactor it into model classes as needed. Many small apps will never get large enough that multiple classes are required for maintainability. As long as you avoid code duplication (i.e. copy+paste) then refactoring will be straightforward later if you find you need it.

轮廓§ 2024-09-07 15:56:54

为就业开发课程总是好的。因为它使您的代码可重用,并且还实现了三层架构。好处是很容易理解。
重要的是,只有适当地开发课程才有好处。在类中开发可供您重用的方法。
另一个好处是它隐藏了代码的复杂性。

It is always good to develop classes for jobs. As it makes your code reusable and it also implement three tier Architecture. The benefit is that it is easy to understand.
The important thing is that, it is only beneficial if you develop your classes appropriately. Develop methods in the class which can be reusable for you.
The another benefit is that it hides the complexity of your code.

桃气十足 2024-09-07 15:56:54

向代码添加结构有两种常用方法:自上而下和自下而上。自上而下的结构来自设计工作,可能涉及正式或非正式的流程以及纯粹的设计工件,例如 UML 图或功能规范。自上而下过程的最终目标是在代码中创建类和接口,以提供适当的结构以使代码可维护。这可以在编写代码之前发生,或者作为迭代的一部分发生,但其想法是首先创建结构,然后创建代码。

自下而上的结构来自于重构。例如,从单击按钮开始所有逻辑。当您添加与按钮单击执行相同操作的菜单项时,请将按钮单击函数的代码移至窗体上的私有成员函数中。当应用程序的不同部分需要相同的功能时,请将私有成员函数使用的代码和状态变量封装到类中。

我建议使用这两种方法。适合您情况的组合取决于开发团队(规模、位置、沟通能力)和应用程序的规模。一般来说,大型应用程序需要更多的自顶向下设计,而在非常小的应用程序上,最少量的自顶向下设计就足够了。但始终应该有一些高层规划,即使对于小型应用程序也是如此。

There are two general approaches to adding structure to code: top down and bottom up. Top down structure comes from design work that can involve a formal or informal process and pure design artifacts like UML diagrams or functional specs. The ultimate goal in a top down process is to create classes and interfaces in your code that will provide appropriate structure to make your code maintainable. This can happen before you write the code, or as part of an iteration, but the idea is that you create the structure first, then create the code.

Bottom up structure comes from refactoring. For example, start with all your logic in a button click. When you add a menu item that does the same thing as the button click, move the code for your button click function into a private member function on the form. When you need the same features in a different part of the application, encapsulate the code and state variables used by your private member function into a class.

I'd recommend using both approaches. The right mix for your situation depends on the development team (size, location, ability to communicate) and the size of your application. Generally speaking, large applications require more top down, and on very small applications a minimal amount of top down design is adequate. But there should always be some high level planning, even for a small app.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文