Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Singleton Pattern Alternative

Singleton Pattern Alternative

Scheduled Pinned Locked Moved C#
testingbeta-testingregexquestionworkspace
8 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Brendan Vogt
    wrote on last edited by
    #1

    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

    N P Y 3 Replies Last reply
    0
    • B Brendan Vogt

      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

      N Offline
      N Offline
      Not Active
      wrote on last edited by
      #2

      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

      B 1 Reply Last reply
      0
      • N Not Active

        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

        B Offline
        B Offline
        Brendan Vogt
        wrote on last edited by
        #3

        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?

        N 1 Reply Last reply
        0
        • B Brendan Vogt

          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

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          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.

          J 1 Reply Last reply
          0
          • B Brendan Vogt

            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?

            N Offline
            N Offline
            Not Active
            wrote on last edited by
            #5

            Use the Google [^]Pattern


            I know the language. I've read a book. - _Madmatt

            1 Reply Last reply
            0
            • P PIEBALDconsult

              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.

              J Offline
              J Offline
              J4amieC
              wrote on last edited by
              #6

              PIEBALDconsult wrote:

              The Singleton pattern is more for C++ and is not of much use in C#

              Got a source for that assertion?

              P 1 Reply Last reply
              0
              • J J4amieC

                PIEBALDconsult wrote:

                The Singleton pattern is more for C++ and is not of much use in C#

                Got a source for that assertion?

                P Offline
                P Offline
                PIEBALDconsult
                wrote on last edited by
                #7

                That's my own conclusion.

                1 Reply Last reply
                0
                • B Brendan Vogt

                  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

                  Y Offline
                  Y Offline
                  yu jian
                  wrote on last edited by
                  #8

                  I always use the singletom to save the system setting. Because I never do the unit testing, I do not know why some people avoid singletons, perhaps there is better way than this.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups