i am guessing that your Singleton is calling new Cinfiguration() before it has an instance of itself but I can be sure without loading it in the debugger. regardless, i don't think your Singleton is very defensive (safe). doing something like:
Code:
private static NHibernateConfigurationSingleton instance = new NHibernateConfigurationSingleton();
does nothing to prevent the instance from being initialized more than once. the probability is small, but distinct. imho, you should change your Instance property to a method call:
Code:
public static NHibernateConfigurationSingleton GetInstance()
{
if (instance== null)
{
lock (typeof(NHibernateConfigurationSingleton))
{
if (instance == null)
{
instance = new NHibernateConfigurationSingleton();
}
}
}
return instance;
}
i would also move the instantiation of the "cfg" variable inside your class constructor. so in the end you have:
Code:
/*
* NHibernateConfigurationSingleton.cs 0.1
*
* Copyright 2006 Ian Escarro. All rights reserved.
* PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
using System;
using NHibernate;
using NHibernate.Cfg;
namespace Agta.ManilaBulletin.Configurations
{
/// <summary>
///
/// </summary>
public class NHibernateConfigurationSingleton
{
private Configuration cfg = null;
private ISessionFactory factory = null;
private volitile static NHibernateConfigurationSingleton instance;
public static NHibernateConfigurationSingleton GetInstance()
{
if (instance == null)
{
lock (typeof(NHibernateConfigurationSingleton))
{
if (instance == null)
{
instance = new NHibernateConfigurationSingleton();
}
}
}
return instance;
}
/// <summary>
/// Creates a new instance of NHibernateConfigurationSingleton.
/// </summary>
protected NHibernateConfigurationSingleton()
{
cfg = new Configuration();
cfg.AddAssembly("webartiData");
factory = cfg.BuildSessionFactory();
}
public ISession OpenSession()
{
return factory.OpenSession();
}
}
}
if you do a search on this board, you will find a thread with several other examples.
-devon
EDIT: modified constructor to 'protected.' thanks for the note grennis. i just copied what was there before. d'oh!