在Web应用程序中的静态数据访问类和实例数据访问类之间进行选择的优点/缺点是什么? [英] What are the pros/cons of choosing between static and instance data access classes in a web app?

查看:85
本文介绍了在Web应用程序中的静态数据访问类和实例数据访问类之间进行选择的优点/缺点是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了有关此主题的其他几个问题(此处此处此处),但尚未看到一个很好的答案.我之前已经开发了很多数据访问层,并且个人更喜欢使用实例类而不是静态类.但是,这更多是个人喜好(我喜欢测试我的业务对象,并且这种方法使模拟DAL更加容易).我以前使用静态类访问数据库,但是我总是对这种设计的适用性(特别是在ASP.NET环境中)感到有些不安全.

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).

在使用ADO.NET提供程序(无ORM)开发数据访问类的这两种方法(特别是在ASP.NET应用程序中)时,任何人都可以提供一些利弊.如果您也有一些一般性的静态和实例类技巧,请随时关注.

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.

我特别关心的问题是:

  1. 线程和并发
  2. 可扩展性
  3. 性能
  4. 其他未知数

谢谢!

推荐答案

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

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

基于实例的方法可以赢得:

Instance based approaches win for:

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

静态方法可以取胜:

  1. 内存-您只有一个实例,因此占用空间更少
  2. 一致性/共享-使单个实例与其自身保持一致很容易.

总的来说,我认为基于实例的方法是更好的方法.如果您也要扩展到单个服务器之外,这将变得更加重要,因为一旦在多台计算机上实例化静态方法,静态方法就会中断".

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...

这篇关于在Web应用程序中的静态数据访问类和实例数据访问类之间进行选择的优点/缺点是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆