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

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


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.