Alternatives for singleton pattern?

I have been a web developer using ASP.NET and C # for some time now, I want to try to improve my skills using best practices.

I have a website. I want to load the settings after turning it off and just reference it when I need it. So I did some research, and 50% of the developers seem to use the singleton pattern for this. And the remaining 50% of developers are ant -singleton. They all hate single people. They recommend dependency injection.

Why are singlets bad? What is the best practice for loading website settings? Should they be loaded only once and referenced where necessary? How will I do this with dependency injection (I'm new to this)? Are there any samples anyone could recommend for my script? And I would also like to see unit test code for this (for my script).

Thanks Brendan

+7
design-patterns singleton
source share
4 answers

As a rule, I avoid singles because they make unit test your application difficult. Singletones are difficult to integrate for unit tests precisely because of their nature - you always get the same thing, but you cannot easily configure for unit test. The configuration data is strictly typed configuration data, in any case, this is one exception that I make. Typically, configuration data is relatively static in any case, and the alternative is to write enough code to avoid the static classes that the infrastructure provides for accessing web.config.

There are several ways to use this application, which will still allow you to use the unit test of your application. One way (maybe both, if your singleton is not lazily reading app.cofnig) should have a default app.config file in your unit test project that provides the default values โ€‹โ€‹needed for your tests. You can use reflection to replace any specific values โ€‹โ€‹as needed in unit tests. As a rule, I set up a private method that allows to exclude an instance of private singleton in the test setup if I make changes for specific tests.

Another way is to not actually use singleton directly, but create an interface for it that implements the singleton class. You can use manual interface injection, by default for a singleton instance, if the given value is null. This allows you to create a mock instance that you can pass to the test class for your tests, but your single code uses a singleton instance. Essentially, every class that needs it maintains a private reference to a singleton instance and uses it. I like this method a little better, but since a singleton will be created, you might still need the default app.config file if all the values โ€‹โ€‹are not loaded lazily.

public class Foo { private IAppConfiguration Configuration { get; set; } public Foo() : this(null) { } public Foo( IAppConfiguration config ) { this.Configuration = config ?? AppConfiguration.Instance; } public void Bar() { var value = this.Config.SomeMaximum; ... } } 
+6
source share

Singleton templates and coding examples are well discussed here ... http://en.wikipedia.org/wiki/Singleton_pattern See also here ... http://en.wikipedia.org/wiki/Dependency_injection

For some reason, singlets seem to divide programmers into strong pro and anti camps. Whatever the merits of the approach, if your colleagues are against it, it is probably best not to use it. If you are on your own, try it and see.

+1
source share

Design patterns can be awesome. Unfortunately, the singleton seems to stick out like a sore thumb and in many cases can be considered anti-shaped (it contributes to bad practice). It is noteworthy that most developers will know only one design pattern, and this is singleton.

Ideally, your settings should be a member variable in a high-level place, for example, the application object that owns the web pages that you spawn. Pages can then request the application for settings, or the application can transfer settings as the pages are created.

+1
source share

One way to approach this problem is to knock it out due to a DAL problem .

No matter what class / webpage, etc. should use the configuration settings, declare a dependency on IConfigSettingsService (factory / repository / whatever you want to use).

 private IConfigSettingsService _configSettingsService; public WebPage(IConfigSettingsService configSettingsService) { _configSettingsService = configSettingsService; } 

Thus, your class will receive the following settings:

 ConfigSettings _configSettings = _configSettingsService.GetTheOnlySettings(); 

the implementation of ConfigSettingsService will have a dependency, which is a Dal class. How would this Dal populate a ConfigSettings object? Who cares.

  • Perhaps it will populate ConfigSettings from the database or XML .config file each time.

  • Perhaps he does this for the first time, and then populates the static _configSettings for subsequent calls.

  • Perhaps he will get the settings from Redis. If something indicates a change in settings, then dal or something external can update Redis. (This approach will be useful if you have several applications using settings.

No matter what it does, your only addiction is the non-Internet service interface. It is very easy to taunt. In your tests, you can return ConfigSettings to it with what you want in it).

In reality, most likely this is MyPageBase, which has an IConfigSettingsService dependency, but it can also be a simple web service, a Windows service, somehatsit MVC, or all of the above.

0
source share

All Articles