2010-02-28 79 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应用程序。