locked
Singleton Class with readonly variable and inheritance RRS feed

  • Question

  • I have an abstract class called Table and a concrete single class called Contact.

    In the Contact class, I have several constant Column variable which is readonly.

     

     

    public sealed class DB_Contact : Table
    {
    
    public static readonly DB_Contact o = new DB_Contact();
    
    public readonly Column ID;
    public readonly Column NAME;
    
    private DB_Contact() : base("contact")
    {
        //Init all the Column variables here.
    }
    
    }
    

     


    Is the above thread safe?

    Is that a good way to implement the singleton class for my situation?

    The plan is, Contact is a table definition which can be used throughout the application for SQL generation.


    • Edited by o00oo00o Wednesday, January 4, 2012 1:07 AM
    Wednesday, January 4, 2012 1:06 AM

Answers

  • Check this out guys:  http://csharpindepth.com/Articles/General/Singleton.aspx

    I use 4th version and this is the code. Just in case if anyone is in my situation. That is you want a singleton that inherit from another class and have class variables.

     

    public sealed class DB_Company : m_Table
    {
    public static readonly DB_Company o = new DB_Company();
    
    public readonly Column ID;
    public readonly Column SNAME;
    
    // Explicit static constructor to tell C# compiler not to mark type as beforefieldinit
    static DB_Company(){}
    private DB_Company() : base("company")
    {<br/>    //Init variables here<br/>}
    
    }
    


    The above works!!, I can leave the static constructor empty and the class variable is still initialized.

    I'm not really worried about the lazyness, since Its only used as database definition for my app, so it won't take up a lot of memory.


    • Edited by o00oo00o Wednesday, January 4, 2012 11:56 PM
    • Marked as answer by o00oo00o Wednesday, January 4, 2012 11:57 PM
    Wednesday, January 4, 2012 11:55 PM

All replies

  • Yes, although using readonly for reference types is not generally a great idea because there is no way to enforce that a reference type is immutable.  To wit:

    public class Foo
    {
       public string A = "Hello";
    }
    
    public class Bar
    {
      public static readonly Bar Instance = new Bar();
      public readonly Foo = new Foo();
    }
    
    Bar.Instance.Foo.A = "World";
    

    Foo is readonly but because Foo has members that are not readonly, it is not actually readonly.  FxCop will warn you about this if you have the rule enabled.

    However, personaly, I use readonly for reference types anyway, because although the compiler can't enforce that a reference type is readonly, as a programmer I can.  I created a custom attribute that I decorate reference types with.  It doesn't actually do anything, it's just a cue to me and other developers that the reference type should not have any mutable members.

    As far as thread immutability goes, I can't say for certain that static class initializers are thread safe but I feel like they probably are.

    Wednesday, January 4, 2012 4:06 AM
  • public sealed class DB_Contact : Table
    {
        private static DB_Contact o = null;
        private static readonly object mutex= new object();
    
        private DB_Contact()
        {
        }
    
        public static DB_Contact Instance
        {
            get
            {
                lock (mutex)
                {
                    if (o==null)
                    {
                        o = new DB_Contact();
                    }
                    return o;
                }
            }
        }
    }
    
    
    

    Above is more correct way to create singleton instance by using lock.
    • Proposed as answer by Ashwin Shetty Wednesday, January 4, 2012 8:08 AM
    Wednesday, January 4, 2012 6:01 AM
  • Check this out guys:  http://csharpindepth.com/Articles/General/Singleton.aspx

    I use 4th version and this is the code. Just in case if anyone is in my situation. That is you want a singleton that inherit from another class and have class variables.

     

    public sealed class DB_Company : m_Table
    {
    public static readonly DB_Company o = new DB_Company();
    
    public readonly Column ID;
    public readonly Column SNAME;
    
    // Explicit static constructor to tell C# compiler not to mark type as beforefieldinit
    static DB_Company(){}
    private DB_Company() : base("company")
    {<br/>    //Init variables here<br/>}
    
    }
    


    The above works!!, I can leave the static constructor empty and the class variable is still initialized.

    I'm not really worried about the lazyness, since Its only used as database definition for my app, so it won't take up a lot of memory.


    • Edited by o00oo00o Wednesday, January 4, 2012 11:56 PM
    • Marked as answer by o00oo00o Wednesday, January 4, 2012 11:57 PM
    Wednesday, January 4, 2012 11:55 PM