在 Web 应用程序中选择静态数据访问类和实例数据访问类有何优缺点?

发布于 2024-08-18 14:57:28 字数 748 浏览 3 评论 0原文

我已经阅读了有关此主题的其他几个问题(此处,< a href="https://stackoverflow.com/questions/1721811/using-static-data-access-methods-with-the-ado-net-entity-framework">此处,以及 此处),但尚未看到很好的答案。我之前开发过相当多的数据访问层,并且个人更喜欢使用实例类而不是静态类。然而,这更多的是个人喜好(我喜欢测试我的业务对象,这种方法使得模拟 DAL 更容易)。我以前曾使用静态类来访问数据库,但我总觉得这样的设计是否合适(尤其是在 ASP.NET 环境中)有点不安全。

任何人都可以提供关于这两种使用 ADO.NET 提供程序(无 ORM)开发数据访问类的方法的一些好的优点/缺点,特别是在 ASP.NET 应用程序中。如果您还有一些更通用的静态与实例类技巧,请随时加入。

我特别关心的问题是:

  1. Threading &并发性 可
  2. 扩展性
  3. 性能
  4. 任何其他未知数

谢谢!

I've read several other questions on this topic (here, here, and here), but have yet to see a great answer. I've developed my fair share of data access layers before and personally prefer to use instance classes instead of static classes. However, it is more of a personal preference (I like to test my business objects, and this approach makes mocking out the DAL easier). I have used static classes to access the database before, but I've always felt a little insecure in the appropriateness of such a design (especially in an ASP.NET environment).

Can anyone provide some good pros/cons with regards to these two approaches to developing data access classes with ADO.NET providers (no ORM), in an ASP.NET application in particular. Feel free to chime in if you have some more general static vs. instance class tips as well.

In particular, the issues I'm concerned about are:

  1. Threading & concurrency
  2. Scalability
  3. Performance
  4. Any other unknowns

Thanks!

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

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

发布评论

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

评论(4

心碎无痕… 2024-08-25 14:57:28

基于静态的方法通常只有一个且只有一个主要优点:它们易于实现。

基于实例的方法的优势在于:

  1. 线程和并发 - 您不需要任何/尽可能多的同步,因此您可以获得更好的吞吐量
  2. 可扩展性 - 与上述
  3. 性能相同的问题。 - 与上面相同的问题
  4. 可测试性 - 这更容易测试,因为模拟一个实例很容易,而测试静态类很麻烦

静态方法可以胜出:

  1. 内存 - 你只有一个实例,所以占用空间较低
  2. 一致性/共享 - 它是很容易保持单个实例与其自身一致。

总的来说,我认为基于实例的方法更优越。如果您也打算扩展到单个服务器之外,这一点就变得更加重要,因为一旦您开始在多台计算机上实例化静态方法,它就会“崩溃”......

Static based approaches really typically have one, and only one, main advantage: they're easy to implement.

Instance based approaches win for:

  1. Threading and Concurrency - You don't need any/as much synchronization, so you get better throughput
  2. Scalability - Same issues as above
  3. Perf. - Same issues as above
  4. Testability - This is much easier to test, since mocking out an instance is easy, and testing static classes is troublesome

Static approaches can win on:

  1. Memory - You only have one instance, so lower footprint
  2. Consistency/Sharing - It's easy to keep a single instance consistent with itself.

In general, I feel that instance-based approaches are superior. This becomes more important if you're going to scale up beyond a single server, too, since the static approach will "break" as soon as you start instancing it on multiple machines...

梦纸 2024-08-25 14:57:28

我的总体感觉是:如果不需要,为什么要实例化?

当不需要多个实例并且不需要实例成员时,我使用静态类。至于DAL,重点是只有一个。如果它没有价值,为什么要实例化它?

这个链接,可以看出静态方法调用比实例调用要快类方法调用。

此链接显示使用静态类的优点是编译器可以检查以确保没有意外添加实例成员。

此链接显示静态类可以用作方便的容器仅对输入参数进行操作而不必获取或设置任何内部实例字段的方法集。对于 DAL,这正是您所拥有的。没有理由创建任何内部实例字段,因此也没有理由进行实例化。

My general feeling is: Why instantiate if you don't have to?

I use static classes when there wont be any use for multiple instances and there isn't a need for instance members. As for the DAL, the point is that there is only one. Why instantiate it if there is no value in it?

Look at this link, which shows that static method calls are faster than instance class method calls.

This link shows that an advantage of using a static class is that the compiler can check to make sure that no instance members are accidentally added.

This link shows that a static class can be used as a convenient container for sets of methods that just operate on input parameters and do not have to get or set any internal instance fields. For a DAL, this is exactly what you have. There is no reason to create any internal instance fields, and therefore, no reason to instantiate.

挥剑断情 2024-08-25 14:57:28

我多年来一直使用静态 DAL,我同意您的担忧。线程和并发是最具挑战性的,在我的例子中,我将不同的连接对象存储在线程静态结构中。事实证明,它具有很强的可扩展性并且性能良好,现在我将 PropertyInfo 转换为 PropertyDescriptor,这为我提供了与反射相同的好处和更好的性能。在我的 DAL 中,我只需编写:

 List<Entity> tableRows = SQL.Read(new SearchCriteria(), new Table());

一切都源自 SQL 静态类,这使我的代码变得更加简单。

I have been using a static DAL for years, and I agree with your concerns. Threading and concurrency is the most challenging and in my case I store different connection objects in thread static structures. It has proven to be very scalable and performs well, even more so now that I am converting PropertyInfo into PropertyDescriptor which gives me the same benefits of reflection with better performance. In my DAL I simply have to write:

 List<Entity> tableRows = SQL.Read(new SearchCriteria(), new Table());

Everything spawns off the SQL static class, and that makes my code a lot simpler.

烟酒忠诚 2024-08-25 14:57:28

对我来说,主要原因是我不需要保留该 DAL 对象的状态。它使用的对象的状态不跨越它们嵌入的方法的范围。这样一来,如果对象的多个实例都相同,为什么还要保留它们呢?

对于最新版本的 ADO.NET,最佳实践似乎是在调用范围内创建和销毁与数据库的连接,并让 ConnectionPool 无论如何处理整个连接可重用性问题。

同样,在最新版本的 .NET Framework 中,TransactionScope(这将是您自己管理连接的原因之一)上升到业务级别,这允许您在同一范围内加入对 DAL 的多个调用。

因此,我看不到创建和销毁(或缓存)DAL 实例的令人信服的情况。

For me the main reason is that I don't need to keep the state of that DAL object. The state of the objects it uses don't span the scope of the method they are embeded. This way why would you keep multiple instances of an object, if they are all the same?

With the latest versions of the ADO.NET, seems to be best practice to create and destroy the connection to the database within the scope of your call, and let the ConnectionPool take care of the whole connection reusability issue anyway.

Again with the latest versions of the .NET Framework, TransactionScope (which would be one reason to manage your connections yourself) move up to the business level, which allow you to join multiple calls to the DAL within the same Scope.

So I can't see a compeling case to create and destroy (or cache) instances of the DAL.

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