Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How could be implemented Singleton, which populates its values from db in C#?

I am trying to implement the Jon Skeet's example

public sealed class Singleton
{
    Singleton()
    {
    }

    public static Singleton Instance
    {
        get
        {
            return Nested.instance;
        }
    }

    class Nested
    {
        // Explicit static constructor to tell C# compiler
        // not to mark type as beforefieldinit
        static Nested()
        {
        }

        internal static readonly Singleton instance = new Singleton();
    }
}

1 Answers

Do all your database operations in the constructor for Singleton.

Without knowing what those operations are, we can't really provide much more help - but that's where you should put them. Obviously that doesn't mean creating a massive constructor - you can still split the code up into normal methods, but you need to call them from the constructor.

like image 112
daenthusiast Avatar answered Nov 29 '25 13:11

daenthusiast



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!