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

前端 未结 4 1129
清歌不尽
清歌不尽 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: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...

提交回复
热议问题