Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 624
Kevin Babcock Avatar asked Jan 20 '10 01:01

Kevin Babcock


People also ask

What is the advantage of using static class?

The advantage of using a static class is that the compiler can check to make sure that no instance members are accidentally added. The compiler will guarantee that instances of this class cannot be created. Static classes are sealed and therefore cannot be inherited. They cannot inherit from any class except Object.

Why static method should be avoided?

Static methods are bad for testability. Since static methods belong to the class and not a particular instance, mocking them becomes difficult and dangerous. Overriding a static method is not that simple for some languages.

What is the disadvantage of static in Java?

in static method we cannot call normal functions that is non - static functions. The static method can not use non static data member or call non-static method directly. this and super cannot be used in static context. Access only static type data (static type instance variable).

What is the advantage of static class in Java?

What are the benefits of static classes in Java? A static class like any other nested class can always access the private members and methods of the outer class. Static classes are declared to remove restrictions on member classes of an outer class.


2 Answers

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

like image 155
Reed Copsey Avatar answered Oct 14 '22 22:10

Reed Copsey


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.

like image 26
Gabriel McAdams Avatar answered Oct 14 '22 23:10

Gabriel McAdams