Singleton constructor question

Posted by gillyb on Stack Overflow See other posts from Stack Overflow or by gillyb
Published on 2010-05-03T06:53:57Z Indexed on 2010/05/03 6:58 UTC
Read the original article Hit count: 279

Filed under:
|

Hi,

I created a Singleton class in c#, with a public property that I want to initialize when the Singleton is first called.

This is the code I wrote :

public class BL
{
    private ISessionFactory _sessionFactory;
    public ISessionFactory SessionFactory
    {
        get { return _sessionFactory; }
        set { _sessionFactory = value; }
    }

    private BL()
    {
        SessionFactory = Dal.SessionFactory.CreateSessionFactory();
    }

    private object thisLock = new object();

    private BL _instance = null;
    public BL Instance
    {
        get
        {
            lock (thisLock)
            {
                if (_instance == null)
                {
                    _instance = new BL();
                }
                return _instance;
            }
        }
    }
}

As far as I know, when I address the Instance BL object in the BL class for the first time, it should load the constructor and that should initialize the SessionFactory object.

But when I try : BL.Instance.SessionFactory.OpenSession(); I get a Null Reference Exception, and I see that SessionFactory is null...

why?

© Stack Overflow or respective owner

Related posts about c#

Related posts about singleton