我有一个使用nhibernate的Web服务。我在repositorry库上有一个单例模式,但每次调用该服务时,它都会创建一个非常昂贵的会话工厂的新实例。我该怎么办?
#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
我可以通过哪种方式为所有服务创建单个实例?
答案 0 :(得分:1)