简体   繁体   English

在Web应用程序中选择静态和实例数据访问类的优缺点是什么?

[英]What are the pros/cons of choosing between static and instance data access classes in a web app?

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). 但是,它更多的是个人偏好(我喜欢测试我的业务对象,这种方法可以更容易地模拟DAL)。 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). 我之前使用静态类来访问数据库,但我总是觉得这种设计的适当性有点不安全(特别是在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. 任何人都可以在这两种方法中使用ADO.NET提供程序(无ORM)开发数据访问类,特别是在ASP.NET应用程序中,可以提供一些好的优点/缺点。 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! 谢谢!

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... 如果您要扩展到单个服务器之外,这一点变得更加重要,因为一旦您开始在多台计算机上实例化,静态方法就会“中断”...

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. 至于DAL,重点是只有一个。 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. 对于DAL,这正是您所拥有的。 There is no reason to create any internal instance fields, and therefore, no reason to instantiate. 没有理由创建任何内部实例字段,因此没有理由进行实例化。

I have been using a static DAL for years, and I agree with your concerns. 我多年来一直在使用静态DAL,我同意你的担忧。 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. 它已被证明是非常可扩展的并且表现良好,现在我将PropertyInfo转换为PropertyDescriptor,这给了我更好的性能反射的相同好处。 In my DAL I simply have to write: 在我的DAL中,我只需写:

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

Everything spawns off the SQL static class, and that makes my code a lot simpler. 所有东西都会产生SQL静态类,这使我的代码变得更加简单。

For me the main reason is that I don't need to keep the state of that DAL object. 对我来说,主要原因是我不需要保持该DAL对象的状态。 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. 使用最新版本的ADO.NET,似乎是在调用范围内创建和销毁数据库连接的最佳实践,并且让ConnectionPool无论如何都要处理整个连接的可重用性问题。

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. 再次使用最新版本的.NET Framework,TransactionScope(这将是您自己管理连接的一个原因)向上移动到业务级别,这允许您在同一范围内连接多个DAL调用。

So I can't see a compeling case to create and destroy (or cache) instances of the DAL. 因此,我无法看到创建和销毁(或缓存)DAL实例的强制案例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#中的静态类,有什么优缺点? - Static classes in C#, what's the pros/cons? Silverlight,Wpf Web App(xbap)还是Click Click?优点和缺点 - Silverlight, Wpf Web App (xbap) or Click Once? Pros and Cons 请求对象,有什么优缺点? - Request object, what are the pros and cons? OOP-在实例和静态方法之间进行选择 - OOP - Choosing between instance and static method C#中“厨房水槽”类的优缺点 - Pros and Cons of “kitchen sink” classes in C# 增加存储库实例的 scope 的优点/缺点? - Pros/Cons to increasing the scope of a repository instance? 使用从WCF生成的类与创建自己的模型dll的优缺点是什么? - What's the pros and cons of using classes generated from WCF vs Creating your own model dll? 将 FAT 类设为部分类并将其拆分为部分类的优缺点是什么? - What are the pros and cons of making a FAT class a partial one & splitting it into partial classes? 我可以在异步等待架构中的 c# 中制作 Static 变量 Volatile 吗? 优缺点都有什么? - Can I make a Static variable Volatile in c# in Async-await architecture ?. What are the pros and cons? 使用 CancellationTokens 作为事件替代方案的优缺点是什么? - What are the pros and cons of using CancellationTokens as alternatives to events?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM