views:

171

answers:

2

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();
    }
}
+4  A: 

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.

Jon Skeet
I got the right guy answering ; ) ... thanks ... yep .. adding a LoadSettings() method call in the constructor... I was trying to add :foreach (PropertyInfo propertyInformation in sett.GetProperties()){reflection walking ... the bug was there ...
A: 

Allmost straight copy paste from the 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
    }