Global vs Singleton in .NET

前端 未结 6 1412
清酒与你
清酒与你 2021-01-06 00:10

I have a very common situation here. And for years I haven\'t found if what i am doing is RIGHT by industry standards.Consider an application which connects to the database,

6条回答
  •  离开以前
    2021-01-06 00:33

    it's nice to see so many creative solutions to such a simple problem ;-)

    salient facts, from OP question:

    1. the connect-string is passed on the command-line
    2. many other components may need to use the connect-string

    so, there is no way around the use of a static element; whether it's a global variable (hard to do in .NET, really, without an enclosing class), a static class, or a singleton really doesn't matter. The shortest-path solution would be a static class initialized by the Program class that processed the command-line.

    Every other solution would still require static access to the passed-in connect-string, though they may hide this behind one or more layers of indirection.

    I'm not saying that you don't want to dress up the basic solution with something fancier, but it is not necessary, and it does not eliminate the fundamentally static/global nature of the connect-string as described.

提交回复
热议问题