2009-06-25 63 views
0

nhibernate會在每次有人發出請求時解析xml文件,或者每次應用程序啓動時解析xml文件?nhibernate開始執行

那麼這裏就是即時通訊做:

public class SessionManager 
{ 
    private readonly ISessionFactory _sessionFactory; 

    public SessionManager() 
    { 
     _sessionFactory = GetSessionFactory(); 
    } 

    public ISession GetSession() 
    { 
     return _sessionFactory.OpenSession(); 
    } 

    private static ISessionFactory GetSessionFactory() 
    { 
     return Fluently.Configure() 
      .Database(MsSqlConfiguration.MsSql2005 
          .ConnectionString(c => 
              c.Is(
               @"Data Source=Pc-De-Yassir;Initial Catalog=MyDatabase;User Id=sa;Password=password;Integrated Security=True") 
         )) 
      .Mappings(m => 
         m.AutoMappings.Add 
          (
          AutoPersistenceModel.MapEntitiesFromAssemblyOf<Model.Category>() 
         )) 
      .BuildSessionFactory(); 
    } 
} 

,這裏是我如何從數據庫中獲取數據

public IList<Category> GetCategories() 
    { 
var session = new SessionManager().GetSession(); 
     return session.CreateCriteria(typeof(Category)) 
      .List<Category>();} 

所以我的問題是將NHibernate的配置本身第一次遇到這種運行的方法每次發出請求?

回答

3

每次實例化ISessionFactory了我的頭頂時一旦...

+1

..這應該在應用程序啓動時發生一次。 – 2009-06-25 10:54:30

2

它做它只有一次。如果您想提高應用程序的性能,請使用ngen.exe工具。 nHibernate通常第一次很慢,因爲第一次啓動應用程序時需要編譯的代碼量很大。

我在應用程序啓動時遇到了類似的問題,並且ngen.exe解決了我的問題。