views:

676

answers:

4

In our data access layer at work we have this standard implementation where the class is accessed through a singleton public property which looks something like this:

public static CustomerController Instance
     {
      get 
      {
       lock(singletonLock)
       {
        if( _instance == null )
        {
         _instance = new CustomerController();  
        }
        return _instance;
       }
      }
     }

now, I get what the code is doing, but I was wondering why you would do this over just creating an instance of the class each time it is used?

+2  A: 

EDIT: Oh whoops I didn't catch the "Data Access Layers" part. But I have an example of that too: If your multithreaded app funnels all of its' database calls through a singleton class, only one thread will ever access the database at once, avoiding race conditions.

If you have a logging mechanism in a multi-threaded application that you use to spit out all exceptions and just write info in (especially for services, or apps that are always running, printing out statuses whenever stuff happens), you will run into file-locking issues. I use a singleton logger class, so that only one thread ever will have access to the Logger, and the rest will wait until the Logger is free to write their line in the text file.

There are lots of cool reasons to use a singleton, but I was like you and had no idea what they were FOR until I ran into this issue with file access in a multithreaded app.

goldenratio
I can understand the need for a singleton in this instance, but if you're accessing a database, is there really a need to have a singleton instance of the class?
lomaxx
What would the database be fore? I am talking about writing to a text file, in which case you can only ever have 1 thread accessing a file at a time.
goldenratio
Oh I'm sorry, I thought you were asking what are singletons for IN GENERAL. The "Data Access Layers" part never clicked, sorry!
goldenratio
+1  A: 

You may wish to do this also, its double checked locking, it will speed up access to your singleton

 public static CustomerController Instance
        {
                get 
                {
                        if( _instance == null )
                        {
                           lock(singletonLock)
                           {
                                if( _instance == null )
                                {
                                        _instance = new CustomerController();  
                                }

                            }
                        }   
                        return _instance;
                }
        }
Keith Nicholas
A: 

The answer is quite simple: you want to get the same object each time it's used.

It gives you the advantages of being a global variable (i.e. there's only one) with the advantages of being a class object (amongst other things, it can do invisable initialization the first time it's needed).

staticsan
why is having it as a global variable a good thing tho? it seems like it's just a singleton for the sake of having it a singleton.
lomaxx
Sometimes having a singleton won't net you any benefits. If your app is single-threaded, you won't need it to be a singleton. If, however, you make it a singleton from the start, you won't get any negative impact and if you ever make it multithreaded in the future, you won't have to refactor.
goldenratio
@lomaxx: You don't want it as a global variable. First, this is namespace pollution. Secondly, something has to initialize it. A singleton solves both of those problems.
staticsan
@goldenratio: It has nothing to do with threaded or not. A singleton means you can ask for it anywhere and the code will always get the same object back. The code doesn't have to pass the resource all over the place. If it doesn't need, it doesn't ask for it.
staticsan
A: 

Isn't a benefit of a singleton Data Access component the fact that you conserve the costly resources required in instantiating a Data Access object (connecting to the database and such)?

cthomas