Singleton Pattern Alternative
-
Hi, I am new to the singleton pattern. I read that it could be used to load all your website setting and configuration values. I also read that people avoid singletons, and people that do unit testing don't use singleton patterns. Why is this? Are singletons good to use in my scenario? If not, how should I do it then? And how can this be unit tested? Thanks Brendan
-
Hi, I am new to the singleton pattern. I read that it could be used to load all your website setting and configuration values. I also read that people avoid singletons, and people that do unit testing don't use singleton patterns. Why is this? Are singletons good to use in my scenario? If not, how should I do it then? And how can this be unit tested? Thanks Brendan
First since this is a web related question you should have used the ASP.NET or Web Development forums. The singleton pattern is a tool like any other, it has its uses under the proper conditions to solve the proper problem. Since the web.config file contains the settings and configuration information for an ASP.NET app you are gaining nothing by trying to use a singleton. On the other hand if some settings are coming from a database, or other source, then a singleton instantiated by the application may be of use.
I know the language. I've read a book. - _Madmatt
-
First since this is a web related question you should have used the ASP.NET or Web Development forums. The singleton pattern is a tool like any other, it has its uses under the proper conditions to solve the proper problem. Since the web.config file contains the settings and configuration information for an ASP.NET app you are gaining nothing by trying to use a singleton. On the other hand if some settings are coming from a database, or other source, then a singleton instantiated by the application may be of use.
I know the language. I've read a book. - _Madmatt
Hi, My apologies for posting it here, but using it to get settings values was just an example. It was posted here because I had some questions regarding the design of the pattern. The main issue that I have is that there is some difficulty in unit testing it. How does one go about unit testing a singleton pattern, and testing all the methods in the .Instance property? Lets say I have something like: BlogSettings.Instance.Name How would I unit test .Name?
-
Hi, I am new to the singleton pattern. I read that it could be used to load all your website setting and configuration values. I also read that people avoid singletons, and people that do unit testing don't use singleton patterns. Why is this? Are singletons good to use in my scenario? If not, how should I do it then? And how can this be unit tested? Thanks Brendan
The Singleton pattern is more for C++ and is not of much use in C#, especially since the introduction of static classes. As for unit testing, I hear that they can make using mock objects difficult, but I'm not the one to ask about that.
-
Hi, My apologies for posting it here, but using it to get settings values was just an example. It was posted here because I had some questions regarding the design of the pattern. The main issue that I have is that there is some difficulty in unit testing it. How does one go about unit testing a singleton pattern, and testing all the methods in the .Instance property? Lets say I have something like: BlogSettings.Instance.Name How would I unit test .Name?
-
The Singleton pattern is more for C++ and is not of much use in C#, especially since the introduction of static classes. As for unit testing, I hear that they can make using mock objects difficult, but I'm not the one to ask about that.
-
PIEBALDconsult wrote:
The Singleton pattern is more for C++ and is not of much use in C#
Got a source for that assertion?
That's my own conclusion.
-
Hi, I am new to the singleton pattern. I read that it could be used to load all your website setting and configuration values. I also read that people avoid singletons, and people that do unit testing don't use singleton patterns. Why is this? Are singletons good to use in my scenario? If not, how should I do it then? And how can this be unit tested? Thanks Brendan