How and where to store a nhibernate session in winforms per request

Background

I read all blogs and nhibernate session management documentation. My problem, I need it for winforms and webforms. That's right, I use the same data layer in winforms (windows.exe) and webforms (asp.net) applications. I read a little about the level of work and is a good choice for winforms. Storing an nhibernate session in HttpRequest.Current.Items seems like a good way for web applications. But what about combo deals? I have web apps, windows apps, and WCF services that should all use the same nhibernate data layer. So back to my question ...

I plan to use this design: NhibernateBestPractices in my web application, for example:

private ISession ThreadSession {
    get {
        if (IsInWebContext()) {
            return (ISession)HttpContext.Current.Items[SESSION_KEY];
        }
        else {
            return (ISession)CallContext.GetData(SESSION_KEY); 
        }
    }
    set {
        if (IsInWebContext()) {
            HttpContext.Current.Items[SESSION_KEY] = value;
        }
        else {
            CallContext.SetData(SESSION_KEY, value); // PROBLEM LINE HERE!!!
        }
    }
}

Problem

The problem that I am encountering when using this code in my Windows application is with the line

CallContext.SetData(SESSION_KEY, value);

If I understand CallContext () correctly, this will allow the session to open the entire lifespan of my Windows application, since it stores the nhibernate session as part of the main application stream. And I heard all kinds of bad things about keeping the nhibernate session open for too long, and I know by design that doesn't mean staying open for too long. If all my assumptions are correct, then the above line of code is no, no.

, , nhibernate Windows. - HttpRequest. Windows, nhibernate ( ) . , .

, nhibernate Windows, ( , - ) ( , )?

** **

,

http://msdn.microsoft.com/en-us/magazine/dd882510.aspx

http://www.codeinsanity.com/2008/09/unit-of-work-pattern.html

+3
2

, IUnitOfWorkStorage

public interface IUnitOfWorkStorage
{
    void StoreUnitOfWork(IUnitOfWork uow);
}

IUnitOfWork ISession,

public interface IUnitOfWork
{
   void Begin();
   void End();
}

Begin , End - . , 2 IUnitOfWorkStorage, WebApp Windows App. - HttpContext.Current - , Windows , , UnitOfWork.

+1

. "", , , , . , , unit test:

namespace MyUnitTests
{
    /// <summary>
    /// Simulates the IHttpModule class but for windows apps.  
    /// There no need to call BeginSession() and EndSession() 
    /// if you wrap the object in a "using" statement.
    /// </summary>
    public class NhibernateSessionModule : IDisposable
    {
        public NhibernateSessionModule()
        {
            NHibernateSessionManager.Instance.BeginTransaction();
        }

        public void BeginSession()
        {
            NHibernateSessionManager.Instance.BeginTransaction();
        }

        public void EndSession()
        {
            NHibernateSessionManager.Instance.CommitTransaction();
            NHibernateSessionManager.Instance.CloseSession();
        }

        public void RollBackSession()
        {
            NHibernateSessionManager.Instance.RollbackTransaction();
        }

        #region Implementation of IDisposable

        public void Dispose()
        {
            // if an Exception was NOT thrown then commit the transaction
            if (Marshal.GetExceptionCode() == 0)
            {
                NHibernateSessionManager.Instance.CommitTransaction();
            }
            else
            {
                NHibernateSessionManager.Instance.RollbackTransaction();
            }
            CloseSession();
        }

        #endregion
    }
}

, - :

[Test]
public void GetByIdTest()
{
    // begins an nhibernate session and transaction
    using (new NhibernateSessionModule())
    {
        IMyCustomer myCust = MyCustomerDao.GetById(123);
        Assert.IsNotNull(myCust);           
    } // ends the nhibernate transaction AND the session
}

.. , , "" Dao, , . , , , , NhibernateSession (- Windows).

+1

Source: https://habr.com/ru/post/1788614/


All Articles