我有一個使用nhibernate的web服務。我在存儲庫上有一個單例模式,但是在每次調用服務時,它會創建一個非常昂貴的會話工廠的新實例。我能做什麼?nhibernate sessionfactory實例不止一次在web服務上
#region Atributos
/// <summary>
/// Session
/// </summary>
private ISession miSession;
/// <summary>
/// Session Factory
/// </summary>
private ISessionFactory miSessionFactory;
private Configuration miConfiguration = new Configuration();
private static readonly ILog log = LogManager.GetLogger(typeof(NHibernatePersistencia).Name);
private static IRepositorio Repositorio;
#endregion
#region Constructor
private NHibernatePersistencia()
{
//miConfiguration.Configure("hibernate.cfg.xml");
try
{
miConfiguration.Configure();
this.miSessionFactory = miConfiguration.BuildSessionFactory();
this.miSession = this.SessionFactory.OpenSession();
log.Debug("Se carga NHibernate");
}
catch (Exception ex)
{
log.Error("No se pudo cargar Nhibernate " + ex.Message);
throw ex;
}
}
public static IRepositorio Instancia
{
get
{
if (Repositorio == null)
{
Repositorio = new NHibernatePersistencia();
}
return Repositorio;
}
}
#endregion
#region Propiedades
/// <summary>
/// Sesion de NHibernate
/// </summary>
public ISession Session
{
get { return miSession.SessionFactory.GetCurrentSession(); }
}
/// <summary>
/// Sesion de NHibernate
/// </summary>
public ISessionFactory SessionFactory
{
get { return this.miSessionFactory; }
}
#endregion
以哪種方式可以爲所有服務創建單個實例?
建議:不要使用「throw ex;」。你想「扔」。否則,堆棧跟蹤將看起來像是來自「throw ex」位置的異常。 – 2011-01-19 00:39:05