具有完全不相关的静态方法的静态类有什么问题吗?

发布于 2024-11-17 19:25:21 字数 438 浏览 2 评论 0原文

我目前正在处理许多 C# 代码隐藏,这些代码最初不是由我编写的。它们被写成一大堆看起来很可怕的代码,现在我的工作就是把它们整理出来。

无论如何,在许多不同的 aspx.cs 文件中存在大量重复的代码。例如,大约有五个代码隐藏文件,它们具有重复多次的完全相同的代码段,以将一些文本输出到文件中。我可以使用以下方法:

private void outputToFile(string text)

在五个不同的代码隐藏文件中重复。

但是,我想知道声明一个名为“MethodHub”的静态类是否是不好的做法,或者只是错误的,它保存了outputToFile(字符串文本)以及十几个彼此不相关的其他方法?

总体目标是从静态类访问这些方法,这样我就可以有效地删除其他类中的数百行代码。

任何人都可以看到这有什么问题,或者我可能遇到任何问题吗?

I am currently working with a lot of C# code-behinds which were not originally written by me. They were written as massive lumps of horrible looking code and it is now my job to sort it out.

Anyway, there are a lot of repetitions of code, in lots of different aspx.cs files. For example, there are about five code-behind files which have exactly the same piece of code repeated a lot to output some text to a file. I could have the method:

private void outputToFile(string text)

repeated in the five different code behind files.

However, I am wondering if it is bad practice or just wrong to declare a static class called something like, "MethodHub" which holds outputToFile(string text) as well as a dozen or so other methods which do not relate to each other?

The overall goal is to access these methods from a static class so I can remove effectively hundreds of lines of code in others.

Can anybody see anything wrong with this, or any problems I may run into?

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

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

发布评论

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

评论(3

赢得她心 2024-11-24 19:25:21

我建议通过引入接口

interface IOutputService
{
  void Output(string data);
}

并使用您的实现实现类(例如

class FileOutputService : IOutputService
{
    // ...
}

代表此接口)来抽象这样的实用程序行为,要点是避免静态类,因为

编辑:

  1. 覆盖逻辑会很混乱,它使用静态类调用,通过单元测试
  2. 使用静态类,轻松切换逻辑/行为非常复杂,您必须更改逻辑实现本身,例如,您必须从文件输出切换到数据库输出,注入另一个服务会很容易实现通用接口而不是进行潜在的重构

I would suggest to abstract such an utility behaviour by introducing interface

interface IOutputService
{
  void Output(string data);
}

and using your implementation implement class like

class FileOutputService : IOutputService
{
    // ...
}

on behalf of this interface, the main point is to avoid static class because

EDIT:

  1. It would be mess to cover logic, which uses static clas calls, by unit tests
  2. With static class it is much complex to switch a logic/behaviour easily, you have to change a logic implementation itself, for instance, you've to switch from a file output to a database output, it will be much easily to inject another service which implements common interface rather than play with potential refactoring
奶茶白久 2024-11-24 19:25:21

当然,持有多个不相关方法的静态类并不是特别优雅,但它比“大量看起来可怕的代码到处重复”模式要好。

当你进行重构时,一定要小步进行。一旦你消除了重复,进一步提高就会变得更容易。

Surely a static class holding several unrelated methods is not particularly elegant, but it's better than the "massive lumps of horrible looking code repeated everywhere" pattern.

When you do refactoring, always do it in small steps. Once you have removed the repetitions, it will be easier to improve further.

画骨成沙 2024-11-24 19:25:21

当然,您可以使用静态类,但如果该方法使用任何共享字段,则可以避免线程安全问题。

Of course you can use static class, but avoid the thread-safety problem if there is any shared fields used by that method.

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