Singleton for Application Configuration

前端 未结 6 488
南方客
南方客 2021-02-05 02:49

In all my projects till now, I use to use singleton pattern to access Application configuration throughout the application. Lately I see lot of articles taking about not to use

相关标签:
6条回答
  • 2021-02-05 03:01

    If any component relies on configuration that can be changed at runtime (for example theme support for widgets), you need to provide some callback or signaling mechanism to notify about the changed config. That's why it is not enough to pass only the needed parameters to the component at creation time (like color). You also need to provide access to the config from inside of the component (pass complete config to component), or make a component factory that stores references to the config and all its created components so it can eventually apply the changes.

    The former has the big downside that it clutters the constructors or blows up the interface, though it is maybe fastest for prototyping. If you take the "Law of Demeter" into account this is a big no because it violates encapsulation. The latter has the advantage that components keep their specific interface where components only take what they need, and as a bonus gives you a central place for refactoring (the factory). In the long run code maintenance will likely benefit from the factory pattern.

    Also, even if the factory was a singleton, it would likely be used in far fewer places than a configuration singleton would have been.

    0 讨论(0)
  • 2021-02-05 03:02

    For that specific situation I would create one configuration object and pass it around to those who need it.

    Since it is the configuration it should be used only in certain parts of the app and not necessarily should be Omnipresent.

    However if you haven't had problems using them, and don't want to test it that hard, you should keep going as you did until today.

    Read the discussion about why are they considered harmful. I think most of the problems come when a lot of resources are being held by the singleton.

    For the app configuration I think it would be safe to keep it like it is.

    0 讨论(0)
  • 2021-02-05 03:07

    Use dependency injection to inject the single configuration object into any classes that need it. This way you can use a mock configuration for testing or whatever you want... you're not explicitly going out and getting something that needs to be initialized with configuration files. With dependency injection, you are not passing the object around either.

    0 讨论(0)
  • 2021-02-05 03:12

    I think an application configuration is an excellent use of the Singleton pattern. I tend to use it myself to prevent having to reread the configuration each time I want to access it and because I like to have the configuration be strongly typed (i.e, not have to convert non-string values each time). I usually build in some backdoor methods to my Singleton to support testability -- i.e., the ability to inject an XML configuration so I can set it in my test and the ability to destroy the Singleton so that it gets recreated when needed. Typically these are private methods that I access via reflection so that they are hidden from the public interface.

    EDIT We live and learn. While I think application configuration is one of the few places to use a Singleton, I don't do this any more. Typically, now, I will create an interface and a standard class implementation using static, Lazy<T> backing fields for the configuration properties. This allows me to have the "initialize once" behavior for each property with a better design for testability.

    0 讨论(0)
  • 2021-02-05 03:19

    Here is an example done using Castale.Core >> DictionaryAdapter and StructureMap

    0 讨论(0)
  • 2021-02-05 03:23

    The singleton pattern seems to be the way to go. Here's a Setting class that I wrote that works well for me.

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