开发者

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

开发者 https://www.devze.com 2022-12-17 02:18 出处:网络
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

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!


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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消