Where can I put my code in Singleton?

I am referring to the following question - > https://stackoverflow.com/a/168383/16732 in particular, the Jon Skeet code:

public sealed class Singleton
 {
     private static readonly Singleton instance = new Singleton();
     public static Singleton Instance { get { return instance; } }

     static Singleton() {}
     private Singleton() {}
 }

What would I like to know, where can I put any logic needed for initialization? Private or static constructor? Logically, I mean that my Singleton is my DapperLite connection, so I need to initialize the mapping as follows:

Database = new SqliteDatabase<int>(myConn);
Database.Init();

EDIT: I am stuck on Compact Framework 3.5 and VS 2008 for reference.

+4
source share
2 answers

- , , static Singleton() {} , private Singleton() {}

 public class Bus
    {
        // Static variable used by all Bus instances. 
        // Represents the time the first bus of the day starts its route. 
        protected static readonly DateTime globalStartTime;

        // Property for the number of each bus. 
        protected int RouteNumber { get; set; }

        // Static constructor to initialize the static variable. 
        // It is invoked before the first instance constructor is run. 
        static Bus()
        {
            globalStartTime = DateTime.Now;

            // The following statement produces the first line of output,  
            // and the line occurs only once.
            Console.WriteLine("Static constructor sets global start time to {0}",
                globalStartTime.ToLongTimeString());
        }

        // Instance constructor. 
        public Bus(int routeNum)
        {
            RouteNumber = routeNum;
            Console.WriteLine("Bus #{0} is created.", RouteNumber);
        }

        // Instance method. 
        public void Drive()
        {
            TimeSpan elapsedTime = DateTime.Now - globalStartTime;

            // For demonstration purposes we treat milliseconds as minutes to simulate 
            // actual bus times. Do not do this in your actual bus schedule program!
            Console.WriteLine("{0} is starting its route {1:N2} minutes after global start time {2}.",
                                    this.RouteNumber,
                                    elapsedTime.TotalMilliseconds,
                                    globalStartTime.ToShortTimeString());
        }
    }

    class TestBus
    {
        static void Main()
        {
            // The creation of this instance activates the static constructor.
            Bus bus1 = new Bus(71);

            // Create a second bus.
            Bus bus2 = new Bus(72);

            // Send bus1 on its way.
            bus1.Drive();

            // Wait for bus2 to warm up.
            System.Threading.Thread.Sleep(25);

            // Send bus2 on its way.
            bus2.Drive();

            // Keep the console window open in debug mode.
            System.Console.WriteLine("Press any key to exit.");
            System.Console.ReadKey();
        }
    }

 private Singleton() 
{
Database = new SqliteDatabase<int>(myConn);
Database.Init();
}
+1

#

.NET 4.0:

public sealed class Singleton
{
    private static readonly Lazy<Singleton> lazy =
        new Lazy<Singleton>(() => new Singleton());

    public static Singleton Instance { get { return lazy.Value; } }

    private Singleton()
    {
        try 
        {
            Database = new SqliteDatabase<int>(myConn);
            Database.Init();
        }
        catch(Exception ex)
        {
           // Handle errors
        }
    }

}

EDIT: Pre-4.0, # 4 . , .

public sealed class Singleton
{
    private static readonly Singleton instance = new Singleton();

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

    private Singleton()
    {
        try
        {
            Database = new SqliteDatabase<int>(myConn);
            Database.Init();
        }
        catch (Exception ex)
        {
            // Handle errors
        }
    }

    public static Singleton Instance
    {
        get
        {
            return instance;
        }
    }
}
0

Source: https://habr.com/ru/post/1525799/


All Articles