예제 #1
0
        private ISession CreateScopeSession(Type type)
        {
            ISessionScope   scope          = threadScopeInfo.GetRegisteredScope();
            ISessionFactory sessionFactory = GetSessionFactory(type);

#if DEBUG
            System.Diagnostics.Debug.Assert(scope != null);
            System.Diagnostics.Debug.Assert(sessionFactory != null);
#endif
            if (scope.IsKeyKnown(sessionFactory))
            {
                return(scope.GetSession(sessionFactory));
            }
            else
            {
                ISession session;

                if (scope.WantsToCreateTheSession)
                {
                    session = OpenSessionWithScope(scope, sessionFactory);
                }
                else
                {
                    session = OpenSession(sessionFactory);
                }
#if DEBUG
                System.Diagnostics.Debug.Assert(session != null);
#endif
                scope.RegisterSession(sessionFactory, session);

                return(session);
            }
        }
        /// <summary>
        /// Obtem a sessão ativa quando utilizado em web
        /// </summary>
        /// <returns>Sessão a ser usada</returns>
        public static ISession GetActiveRecordSession()
        {
            ISessionFactoryHolder holder      = ActiveRecordMediator.GetSessionFactoryHolder();
            ISessionScope         activeScope = holder.ThreadScopeInfo.GetRegisteredScope();
            ISession session = null;
            var      key     = holder.GetSessionFactory(typeof(ActiveRecordBase));

            if (activeScope == null)
            {
                session = holder.CreateSession(typeof(ActiveRecordBase));
            }
            else
            {
                if (activeScope.IsKeyKnown(key))
                {
                    session = activeScope.GetSession(key);
                }
                else
                {
                    session = holder.GetSessionFactory(typeof(ActiveRecordBase)).OpenSession();
                }
            }
            return(session);
        }