Created
April 28, 2013 21:56
-
-
Save cmendesce/5478566 to your computer and use it in GitHub Desktop.
Implementa o contexto do nhibernate para armazenar a session, dentro do HttpContext, caso exista, ou dentro do CallContext
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| namespace Core.Data.NH | |
| { | |
| using System.Runtime.Remoting.Messaging; | |
| using System.Web; | |
| using NHibernate; | |
| using NHibernate.Context; | |
| using NHibernate.Engine; | |
| /// <summary> | |
| /// Implementa o contexto do nhibernate para armazenar | |
| /// a session, dentro do HttpContext, caso exista, ou | |
| /// dentro do CallContext. | |
| /// </summary> | |
| public class HybridSessionContext : ICurrentSessionContext | |
| { | |
| /// <summary> | |
| /// Chave que armazena o valor. | |
| /// </summary> | |
| private const string SessionKey = "Core.Data.NH.HybridSessionContextKey"; | |
| /// <summary> | |
| /// Session factory | |
| /// </summary> | |
| [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields", | |
| Justification = "ICurrentSessionContext necessita de um construtor que receber sessionFactory.")] | |
| private readonly ISessionFactoryImplementor sessionFactory; | |
| /// <summary> | |
| /// Inicializa uma nova instância de SessionContext. | |
| /// </summary> | |
| /// <param name="sessionFactory">NHibernate session factory.</param> | |
| public HybridSessionContext(ISessionFactoryImplementor sessionFactory) | |
| { | |
| this.sessionFactory = sessionFactory; | |
| } | |
| /// <summary> | |
| /// Configura a session atual ao contexto. | |
| /// </summary> | |
| /// <param name="session">session</param> | |
| public static void Bind(ISession session) | |
| { | |
| var ctx = HttpContext.Current; | |
| if (ctx != null) | |
| { | |
| ctx.Items[SessionKey] = session; | |
| } | |
| else | |
| { | |
| CallContext.SetData(SessionKey, session); | |
| } | |
| } | |
| /// <summary> | |
| /// Retira a session do contexto. | |
| /// </summary> | |
| /// <returns>session</returns> | |
| public static ISession Unbind() | |
| { | |
| ISession session = null; | |
| var ctx = HttpContext.Current; | |
| if (ctx != null) | |
| { | |
| session = (ISession)ctx.Items[SessionKey]; | |
| ctx.Items[SessionKey] = null; | |
| } | |
| else | |
| { | |
| session = (ISession)CallContext.GetData(SessionKey); | |
| CallContext.SetData(SessionKey, null); | |
| } | |
| return session; | |
| } | |
| /// <summary> | |
| /// Obtém a session atual. | |
| /// </summary> | |
| /// <returns>session atual</returns> | |
| public ISession CurrentSession() | |
| { | |
| ISession session = null; | |
| var ctx = HttpContext.Current; | |
| if (ctx != null) | |
| { | |
| session = (ISession)ctx.Items[SessionKey]; | |
| } | |
| else | |
| { | |
| session = (ISession)CallContext.GetData(SessionKey); | |
| } | |
| return session; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment