c# - Singleton Data Access Layers - Stack Overflow

admin2022-06-01  5

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?

Answers:

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?

Answers:

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.

Answers:

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;
                }
        }

Answers:

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).

转载请注明原文地址:https://www.u19.cn/read-137220.html

New Post(0)