关于静态和实例数据库访问.net的愚蠢问题

发布于 2024-09-30 08:40:00 字数 466 浏览 1 评论 0原文

我试图浏览大量帖子 - 非常有价值,因为您可以吸收其他方面,即使它与主要问题没有直接联系。

就我而言,正如标题所示,我真的想尝试了解使用静态类/方法进行数据访问的“现实世界”优点/缺点。

有关我正在使用的内容的更多信息。

  1. 我正在使用模式/实践企业库 5.0 作为数据访问块(以及 缓存等)。

  2. 我实际的“业务对象”是一个静态类,有一个静态方法来表示“getContacts” 反过来,返回一个 XML 字符串等。

所以问题实际上是 考虑到上面的结构,使用静态类返回 XML 字符串是“更好/更高效/良好的实践”吗?再次考虑到我不需要任何实例关系——只需“给我数据”之一,然后离开..还是使用实例基类更容易被接受?

我意识到每种方法都有其具体情况,但只是想获得反馈来指导我的追求。

谢谢! 大卫.

I have tried to trawl through an amazing amount posts - really valuable, because there are other aspects that you can absorb even though it's not directly linked to the primary question.

In my case, as the title says, I really want to try to understand the "real world" pros/cons of using static classes/methods for data access.

A little more info of what I am using.

  1. I am using the Patterns/Practices Enterprise Library 5.0 for the data access blocks (and
    caching and so on).

  2. My actual "business object" is a static class, with a static method to say "getContacts"
    in turn that returns say an XML string etc.

So the question really is
Given the structure above, is it "better/more efficient/good practice" to use a static class to return the XML string - again given that I do not need any instance relationship - just a one of "give me the data" and leave.. or is it more accepted to use an instance base class??

I realise the fact there is circumstances for each method, but just trying to get feedback to give me directions to pursue.

Thanks!
David.

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

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

发布评论

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

评论(1

只怪假的太真实 2024-10-07 08:40:00

这根本不是一个愚蠢的问题。首先,我将向您指出 何时使用C# 中的静态类,因为那里有很多很好的建议。

何时使用静态方法/类实际上完全取决于应用程序的设计。

即便如此,对于长期设计,我认为最好使用实例而不是仅使用静态方法。如果您打算在应用程序中使用任何面向对象的概念,那么静态方法将立即限制您。您通常可以使用实例等创建更灵活的系统。

您还需要非常小心地使用静态字段的静态方法。我见过很多开发人员不完全理解静态字段和实例字段之间的区别的例子,这导致了各种各样的问题。

因此,为了给你一个直接的答案,我认为你应该尽可能避免使用静态方法,因为从长远来看,沿着 OO 路线走会得到回报。

Its not a dumb question at all.First I'll point you to When to Use Static Classes in C#, as there is a lot of great advice there.

Using when to use a static method/class is really all down to the design of your application.

Even so, I would feel its better to use instances rather than just static methods for the long term design. If you're ever going to use any OO concepts in your application, then static methods will limit you straight away. You can generally create a more flexible system using instances etc.

You also need to be very careful with using static methods in regard to static fields. I've seen plenty of examples of developers not understanding exactly what the difference is between a static field and an instance one, and its led to all sorts of problems.

So, to give you a straight answer, I would think you should try to avoid the static methods if you can, since in the long term, it will pay off by going down the OO route.

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