public ISession GetOrCreateSession <TSessionContext>(Type type = null) where TSessionContext : ISessionContext
        {
            // var context = Ioc.Create(typeof(TSessionContext));
            var             _context       = Ioc.Create <ISessionContext>(typeof(TSessionContext));
            ISessionFactory sessionFactory = _sessionFactoryProvider.GetSessionFactory(_context.Name);

            string sessionKey       = _context.Name + "#";
            string connectionString = _context.GetConnectionString();

            if (!ActiveSessions.TryGetValue(sessionKey, out ISession session))
            {
                if (Options.IsTransactional == true)
                {
                    ActiveTransactionInfo activeTransactionInfo = ActiveTransactions.GetOrDefault(connectionString);
                    if (activeTransactionInfo == null)
                    {
                        session = DbConnection != null
                            ? sessionFactory.WithOptions().Connection(DbConnection).OpenSession()
                            : sessionFactory.OpenSession();

                        ITransaction transaction = Options.IsolationLevel.HasValue
                            ? session.BeginTransaction(Options.IsolationLevel.Value.ToSystemDataIsolationLevel())
                            : session.BeginTransaction();

                        activeTransactionInfo = new ActiveTransactionInfo(transaction, session.Connection, session);
                        ActiveTransactions[connectionString] = activeTransactionInfo;
                    }
                    else
                    {
                        session = activeTransactionInfo.StarterSession;
                        activeTransactionInfo.AttendedSessions.Add(session);
                    }
                }
                else
                {
                    session = DbConnection != null
                        ? sessionFactory.OpenSessionWithConnection(DbConnection)
                        : sessionFactory.OpenSession();
                }

                ActiveSessions[sessionKey] = session;
            }

            var filters = _context.Filters().ToList();

            foreach (var filter in filters)
            {
                var name       = filter.Name();
                var parameters = filter.Parameters();
                var _filter    = session.EnableFilter(name);
                foreach (var parameter in parameters)
                {
                    _filter.SetParameter(parameter.Key, parameter.Value);
                }
            }
            return(session);
        }
Exemplo n.º 2
0
        public override DbContext CreateDbContext <TDbContext>(string connectionString, IDbContextResolver dbContextResolver, string moduleName)
        {
            //var dtx = base.CreateDbContext<TDbContext>(connectionString, dbContextResolver, moduleName);

            //var currentIsolation = (IsolationLevel)SafeConvert.ToEnum(typeof(IsolationLevel), (object)Options.IsolationLevel, IsolationLevel.ReadUncommitted);
            //if (Options.IsTransactional == true && isBeginTransaction)
            //{
            //    dataBaseTransactions.Add(new DataBaseTransaction()
            //    {
            //        Transaction = dtx.Database.BeginTransaction(currentIsolation),
            //        dbContext = dtx,
            //        isCommit = false
            //    });
            //}
            //return dtx;
            DbContext dbContext;

            var activeTransaction = ActiveTransactions.GetOrDefault(connectionString);

            if (activeTransaction == null)
            {
                dbContext = dbContextResolver.Resolve <TDbContext>(connectionString, moduleName);

                var dbtransaction = dbContext.Database.BeginTransaction((Options.IsolationLevel ?? IsolationLevel.ReadCommitted).ToSystemDataIsolationLevel());
                activeTransaction = new ActiveTransactionInfo(dbtransaction, dbContext);
                ActiveTransactions[connectionString] = activeTransaction;
            }
            else
            {
                dbContext = dbContextResolver.Resolve <TDbContext>(
                    activeTransaction.DbContextTransaction.GetDbTransaction().Connection,
                    moduleName,
                    true
                    );

                if (dbContext.HasRelationalTransactionManager())
                {
                    dbContext.Database.UseTransaction(activeTransaction.DbContextTransaction.GetDbTransaction());
                }
                else
                {
                    dbContext.Database.BeginTransaction();
                }

                activeTransaction.AttendedDbContexts.Add(dbContext);
            }

            return(dbContext);
        }
Exemplo n.º 3
0
        public ISession GetOrCreateSession <TSessionContext>() where TSessionContext : StoveSessionContext
        {
            ISessionFactory sessionFactory = _sessionFactoryProvider.GetSessionFactory <TSessionContext>();
            var             connectionStringResolveArgs = new ConnectionStringResolveArgs();

            connectionStringResolveArgs["SessionContextType"] = typeof(TSessionContext);
            string connectionString = ResolveConnectionString(connectionStringResolveArgs);

            string sessionKey = typeof(TSessionContext).FullName + "#" + connectionString;

            if (!ActiveSessions.TryGetValue(sessionKey, out ISession session))
            {
                if (Options.IsTransactional == true)
                {
                    ActiveTransactionInfo activeTransactionInfo = ActiveTransactions.GetOrDefault(connectionString);
                    if (activeTransactionInfo == null)
                    {
                        session = DbConnection != null
                            ? sessionFactory.WithOptions().Connection(DbConnection).OpenSession()
                            : sessionFactory.OpenSession();

                        ITransaction transaction = Options.IsolationLevel.HasValue
                            ? session.BeginTransaction(Options.IsolationLevel.Value.ToSystemDataIsolationLevel())
                            : session.BeginTransaction();

                        activeTransactionInfo = new ActiveTransactionInfo(transaction, session.Connection, session);
                        ActiveTransactions[connectionString] = activeTransactionInfo;
                    }
                    else
                    {
                        session = activeTransactionInfo.StarterSession;

                        activeTransactionInfo.AttendedSessions.Add(session);
                    }
                }
                else
                {
                    session = DbConnection != null
                        ? sessionFactory.OpenSessionWithConnection(DbConnection)
                        : sessionFactory.OpenSession();
                }

                ActiveSessions[sessionKey] = session;
            }

            return(session);
        }