I am trying to implement the Jon Skeet's example
我正在尝试实施Jon Skeet的例子
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();
}
}
2 个解决方案
#1
4
Do all your database operations in the constructor for Singleton
.
在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.
在不知道这些操作是什么的情况下,我们无法真正提供更多帮助 - 但这就是你应该放置它们的地方。显然,这并不意味着创建一个庞大的构造函数 - 您仍然可以将代码拆分为普通方法,但是您需要从构造函数中调用它们。
#2
0
Almost straight copy paste from the BlogEngine ...
来自BlogEngine的几乎直接的复制粘贴......
System.Collections.Specialized.ListDictionary lstSettings;
string msg;
MyApp.Bo.AppUser objAppUser = new AppUser();
MyApp.Db.SqlServer2008Provider p = new MyApp.Db.SqlServer2008Provider(objAppUser);
p.LoadSettings(out msg, out lstSettings);
foreach (string key in lstSettings.Keys)
{
string name = key;
string value = (string)lstSettings[key];
#region CycleTroughobjAppSettingProperties
Type objAppSettingsType = typeof(MyApp.Bo.AppSettings);
foreach (PropertyInfo propInfo in objAppSettingsType.GetProperties())
{
if (propInfo.Name.Equals(name, StringComparison.OrdinalIgnoreCase))
{
try
{
propInfo.SetValue(this, Convert.ChangeType(value, propInfo.PropertyType, CultureInfo.CurrentCulture), null);
}
catch
{
logger.Fatal("Failed setting the Application settings ");
}
break;
}
}
#endregion CycleTroughobjAppSettingProperties
}
#1
4
Do all your database operations in the constructor for Singleton
.
在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.
在不知道这些操作是什么的情况下,我们无法真正提供更多帮助 - 但这就是你应该放置它们的地方。显然,这并不意味着创建一个庞大的构造函数 - 您仍然可以将代码拆分为普通方法,但是您需要从构造函数中调用它们。
#2
0
Almost straight copy paste from the BlogEngine ...
来自BlogEngine的几乎直接的复制粘贴......
System.Collections.Specialized.ListDictionary lstSettings;
string msg;
MyApp.Bo.AppUser objAppUser = new AppUser();
MyApp.Db.SqlServer2008Provider p = new MyApp.Db.SqlServer2008Provider(objAppUser);
p.LoadSettings(out msg, out lstSettings);
foreach (string key in lstSettings.Keys)
{
string name = key;
string value = (string)lstSettings[key];
#region CycleTroughobjAppSettingProperties
Type objAppSettingsType = typeof(MyApp.Bo.AppSettings);
foreach (PropertyInfo propInfo in objAppSettingsType.GetProperties())
{
if (propInfo.Name.Equals(name, StringComparison.OrdinalIgnoreCase))
{
try
{
propInfo.SetValue(this, Convert.ChangeType(value, propInfo.PropertyType, CultureInfo.CurrentCulture), null);
}
catch
{
logger.Fatal("Failed setting the Application settings ");
}
break;
}
}
#endregion CycleTroughobjAppSettingProperties
}