我正在链接到以下问题 - > https://stackoverflow.com/a/2550935/46724 特别是Jon Skeet的代码:
public sealed class Singleton
{
private static readonly Singleton instance = new Singleton();
public static Singleton Instance { get { return instance; } }
static Singleton() {}
private Singleton() {}
}
我想知道的是我在初始化时需要将逻辑放在哪里?私有或静态构造函数?按逻辑,我的意思是我的Singleton是我的DapperLite连接所以我需要像这样初始化映射:
Database = new SqliteDatabase<int>(myConn);
Database.Init();
编辑:我被困在Compact Framework 3.5和VS 2008上以供参考。
答案 0 :(得分:1)
如果您的类中需要任何静态字段,则应在此构造函数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 :(得分:0)
这是Jon Skeet的书C# in Depth
的链接如果您使用的是.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
}
}
}
编辑: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;
}
}
}