title: What is a good way to setup CRUD methods and SessionFactory in Nhibernate?
body: <p>I currently have a NHibernateHelper class which creates a session factory. I keep recieving errors saying 'Session Closed!'. Am I going about this in the wrong way? The error occurs when I call the Add(Login login) which gets called after Add(User user)</p>
  
  <pre><code>public class NHibernateHelper
  {
      private static ISessionFactory _sessionFactory;
  
      private static ISessionFactory SessionFactory
      {
          get
          {
              if (_sessionFactory == null)
              {
                  var configuration = new Configuration();
                  configuration.Configure();
                  configuration.AddAssembly("System.Core");
                  _sessionFactory = configuration.BuildSessionFactory();
              }
              return _sessionFactory;
          }
      }
  
      public static ISession OpenSession()
      {
          return SessionFactory.OpenSession();
      }
  }
  </code></pre>
  
  <p>Here is my repository:</p>
  
  <pre><code>internal class UserRepository : IUserRepository
  {
      private ISession _db = NHibernateHelper.OpenSession();
  
      public void Add(User user)
          {
              using (_db)
              {
                  using (ITransaction transaction = _db.BeginTransaction())
                  {
                      IEnumerable&lt;UserRole&gt; userRoles = user.UserRoles;
                      user.UserRoles = null;
                      _db.Save(user);
                      foreach (UserRole userRole in userRoles)
                      {
                          userRole.UserID = user.UserID;
                          _db.Save(userRole);
                      }
                      transaction.Commit();
                  }
              }
          }
  
  
  
          public void Add(Login login)
          {
              using (_db)
              {
                  using (ITransaction transaction = _db.BeginTransaction())
                  {
                      _db.Save(login);
                      transaction.Commit();
                  }
              }
  
          }
  }
  </code></pre>