2010-02-28 75 views
0

我有一個與Nihbernate設置的asp.net應用程序,現在我想將其轉換爲Windows窗體應用程序。Nhibernate與Windows窗體

這是在Global.asax.cs中設置的代碼。任何人都可以給我一些代碼示例如何在Windows窗體中執行此操作?

protected void Application_BeginRequest(object sender, EventArgs e) 
    { 
     ManagedWebSessionContext.Bind(HttpContext.Current, SessionManager.SessionFactory.OpenSession()); 
    } 

    protected void Application_EndRequest(object sender, EventArgs e) 
    { 
     ISession session = ManagedWebSessionContext.Unbind(HttpContext.Current, SessionManager.SessionFactory); 
     if (session != null) 
     { 
      try 
      { 
       if (session.Transaction != null && session.Transaction.IsActive) 
       { 
        session.Transaction.Rollback(); 
       } 
       else 
       { 
        session.Flush(); 
       } 
      } 
      finally 
      { 

       session.Close(); 
      } 
     } 
    } 

回答

1

嗯,有幾種方法在有狀態的應用程序訪問ISessionFactory(和桌面應用程序是一種應用),其中:

辛格爾頓 你可以在一旦建立會話工廠您的程序啓動並通過靜態單例類訪問它。 這將強制應用程序僅使用會話工廠的一個實例。

例如:

public sealed class NHibernateHelper 
{ 
    private static ISessionFactory SessionFactory; 
    private static readonly Configuration NhibernateConfig; 
    // .... 
    static NHibernateHelper() 
    { 
     NhibernateConfig = new Configuration().Configure(); 
     SessionFactory = NhibernateConfig.BuildSessionFactory(); 
    } 

    public static ISessionFactory GetSessionFactory() 
    { 
     return SessionFactory; 
    } 
    // .... 
} 

...和訪問通過了getSessionFactory方法的會話工廠遍佈應用。

語境對象和/或依賴注入

你可以建立從配置的會話工廠,並通過上下文對象通過它遍佈應用。

例如:

  1. 啓動時:

    // here you configure NHibernate. 
    ISessionFactory _sessionFactory = BuildMySessionFactory(); 
    // ... 
    ObjectFactory.Initialize(x => 
    { 
           x.For<IPatientRepository>() 
           .Use<StandardPatientRepository>() 
           .Ctor<ISessionFactory>().Is(_sessionFactory); 
    
    // ... initialize the rest of your repositories... 
    }); 
    
  2. 則:

    public class StandardPatientRepository : IPatientRepository 
    { 
        private readonly ISessionFactory _sessionFactory; 
        public StandardPatientRepository(ISessionFactory sessionFactory) 
        { 
         if (sessionFactory == null) 
          throw new ArgumentNullException("sessionFactory"); 
    
         _sessionFactory = sessionFactory; 
        } 
        public virtual Patient Get(Guid id) 
        { 
         using (IStatelessSession session = 
            _sessionFactory.OpenStatelessSession()) 
         { 
          return session.Get<Patient>(id); 
         } 
        } 
        // the rest of data-access methods. 
    } 
    

然後在你的類,這將使使用數據(即。使用存儲庫),您將使用:

Patient = ObjectFactory.GetInstance<IPatientRepository>().Get(patient); 

在我看來,第二種方法比較好,因爲我認爲在大多數情況下,這個單是一個反模式。第二種方法可以讓你更好地控制數據層,你會知道誰和訪問它的時間。

+0

卡里姆謝謝你。 可以請給我一個小工作的桌面應用程序作爲示例? – 2010-02-28 10:47:45

+0

你的電子郵件是什麼? – 2010-02-28 14:46:27

1

這裏是一個桌面應用程序使用NHibernate一個做得好的和廣泛的樣本應用程序:

Building a Desktop To-Do Application with NHibernate

在桌面應用程序管理NHibernate會話往往是很多比管理NHibernate會話更多地參與一個Web應用程序。