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

前端 未结 4 1126
清歌不尽
清歌不尽 2021-02-05 14:05

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 perso

相关标签:
4条回答
  • 2021-02-05 14:09

    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 讨论(0)
  • 2021-02-05 14:10

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

    0 讨论(0)
  • 2021-02-05 14:30

    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.

    0 讨论(0)
  • 2021-02-05 14:34

    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.

    0 讨论(0)
提交回复
热议问题