Exemplo n.º 1
0
Arquivo: Orm.cs Projeto: darakeon/dfm
        public static void Config(IApplicationBuilder app, IHostApplicationLifetime life)
        {
            try
            {
                SessionFactoryManager.Initialize <UserMap, User>(Cfg.DB);

                app.Use <Orm>(async(context, next) =>
                {
                    if (context.IsAsset())
                    {
                        return;
                    }

                    SessionManager.Init(
                        () => Session.GetKey(() => context)
                        );

                    await next();

                    SessionManager.Close();
                });

                life.ApplicationStopping.Register(SessionFactoryManager.End);
            }
            catch (Exception e)
            {
                e.TryLogHandled("Error on initialize DB");
            }
        }
Exemplo n.º 2
0
        public void Update(IList <TEntity> entitys)
        {
            ISession session = SessionFactoryManager.GetCurrentSession();

            using (var tx = TransactionHelper.NhTransactionHelper.BeginTransaction())
            {
                try
                {
                    foreach (var entity in entitys)
                    {
                        session.Update(entity);
                    }

                    tx.Commit(() =>
                    {
                        return(true);
                    });
                }
                catch (Exception e)
                {
                    tx.Rollback();
                    throw;
                }
            }
        }
Exemplo n.º 3
0
 /// <summary>
 /// 清除对象一级缓存(Session.Flush()不会同步到数据库)
 /// </summary>
 /// <param name="list"></param>
 public void Evict(IEnumerable <TEntity> list)
 {
     foreach (var p in list)
     {
         SessionFactoryManager.GetCurrentSession().Evict(p);
     }
 }
Exemplo n.º 4
0
        public void not_build_the_database_when_the_connection_cache_is_empty()
        {
            var session = IsolateSetupDatabaseMethod(null);

            _services.InitDatabase();
            Isolate.Verify.WasNotCalled(() => SessionFactoryManager.BuildSchema("TestGenForm", session));
        }
Exemplo n.º 5
0
        public void build_the_database_when_the_SessionCache_is_not_EmptySessionCache()
        {
            var connection = Isolate.Fake.Instance <IDbConnection>();
            var session    = IsolateSetupDatabaseMethod(connection);

            _services.InitDatabase();
            Isolate.Verify.WasCalledWithAnyArguments(() => SessionFactoryManager.BuildSchema("TestGenForm", session));
        }
Exemplo n.º 6
0
        public static ISessionFactory GetSessionFactory(string environment)
        {
            if (!Factories.ContainsKey(environment))
            {
                Factories.Add(environment, SessionFactoryManager.GetSessionFactory(environment));
            }

            return(Factories[environment]);
        }
Exemplo n.º 7
0
        private ISession IsolateSetupDatabaseMethod(IDbConnection connection)
        {
            Isolate.WhenCalled(() => _sessionState["connection"]).WillReturn(connection);
            Isolate.WhenCalled(() => _sessionState["environment"]).WillReturn("TestGenForm");

            var session = Isolate.Fake.Instance <ISession>();

            Isolate.WhenCalled(() => SessionFactoryManager.BuildSchema("TestGenForm", session)).IgnoreCall();
            return(session);
        }
        public void MyTestInitialize()
        {
            Context = new SessionContext();
            SessionFactoryManager.BuildSchema(SessionFactoryManager.Test, Context.CurrentSession());
            Context.CurrentSession().Transaction.Begin();

            var fact = Context.CurrentSession().SessionFactory;

            ObjectFactory.Configure(x => x.For <ISessionFactory>().Use(fact));
        }
Exemplo n.º 9
0
        private static void sessionFactory(Action action)
        {
            SessionFactoryManager.Initialize <UserMap, User>(Cfg.DB);

            try
            {
                action();
            }
            finally
            {
                SessionFactoryManager.End();
            }
        }
Exemplo n.º 10
0
        private static void SetupInMemoryDatabase(ISessionStateCache stateCache, IDbConnection conn)
        {
            var fact = SessionFactoryManager.GetSessionFactory(stateCache.GetEnvironment());

            stateCache.SetSessionFactory(fact);

            UseSessionFactoryFromApplicationOrSessionCache(stateCache);
            var session = ObjectFactory.GetInstance <ISessionFactory>().OpenSession(conn);

            SessionFactoryManager.BuildSchema(stateCache.GetEnvironment(), session);
            CurrentSessionContext.Bind(session);

            UserServices.ConfigureSystemUser();
        }
        public static void SetupInMemoryDatabase(HttpSessionStateBase sessionState, IDbConnection conn)
        {
            var fact = SessionFactoryManager.GetSessionFactory(GetEnvironment(sessionState));

            sessionState["sessionfactory"] = fact;

            UseSessionFactoryFromApplicationOrSessionState(sessionState);
            var session = ObjectFactory.GetInstance <ISessionFactory>().OpenSession(conn);

            SessionFactoryManager.BuildSchema(GetEnvironment(sessionState), session);
            CurrentSessionContext.Bind(session);

            UserServices.ConfigureSystemUser();
        }
Exemplo n.º 12
0
        public void Merge(TEntity entity)
        {
            ISession session = SessionFactoryManager.GetCurrentSession();

            using (var tx = TransactionHelper.NhTransactionHelper.BeginTransaction())
            {
                try
                {
                    session.Merge(entity);
                    tx.Commit();
                }
                catch
                {
                    tx.Rollback();
                    throw;
                }
            }
        }
Exemplo n.º 13
0
        /// <summary>
        /// 执行存储过程
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="cmdParms"></param>
        public void ExecuteProc(string sql, params MySqlParameter[] cmdParms)
        {
            ISession   session = SessionFactoryManager.GetCurrentSession();
            IDbCommand cmd     = session.Connection.CreateCommand();

            session.Transaction.Enlist(cmd);

            cmd.CommandText = sql;
            cmd.CommandType = CommandType.StoredProcedure;
            if (cmdParms != null)
            {
                foreach (MySqlParameter parm in cmdParms)
                {
                    cmd.Parameters.Add(parm);
                }
            }
            cmd.Prepare();
            cmd.ExecuteNonQuery();
        }
Exemplo n.º 14
0
        public TKey Save(TEntity entity)
        {
            ISession session = SessionFactoryManager.GetCurrentSession();

            using (var tx = TransactionHelper.NhTransactionHelper.BeginTransaction())
            {
                try
                {
                    var key = (TKey)session.Save(entity);
                    tx.Commit(() => {
                        return(true);
                    });
                    return(key);
                }
                catch
                {
                    tx.Rollback();
                    throw;
                }
            }
        }
Exemplo n.º 15
0
        /// <summary>
        ///        SqlParameter[] cmdParms = new SqlParameter[] { };
        ///cmdParms[0] = new SqlParameter("@" + AccidentParameter.AccidentDate + "",);
        /// </summary>
        /// <param name="sql"></param>
        ///<param name="cmdParms"> </param>
        public int ExecuteQuery(string sql, params MySqlParameter[] cmdParms)
        {
            ISession   session = SessionFactoryManager.GetCurrentSession();
            IDbCommand cmd     = session.Connection.CreateCommand();

            session.Transaction.Enlist(cmd);

            cmd.CommandText = sql;
            cmd.CommandType = CommandType.Text;
            if (cmdParms != null)
            {
                foreach (MySqlParameter parm in cmdParms)
                {
                    cmd.Parameters.Add(parm);
                }
            }
            cmd.Prepare();
            var count = cmd.ExecuteNonQuery();

            return(count);
        }
Exemplo n.º 16
0
        public DataTable ExecuteNoQueryToTable(string sql, params MySqlParameter[] cmdParms)
        {
            ISession session = SessionFactoryManager.GetCurrentSession();

            MySqlDataAdapter mysqlAdapter = new MySqlDataAdapter();

            mysqlAdapter.SelectCommand.Connection  = (MySqlConnection)session.Connection;
            mysqlAdapter.SelectCommand.CommandType = CommandType.Text;
            mysqlAdapter.SelectCommand.CommandText = sql;
            if (cmdParms != null)
            {
                foreach (MySqlParameter parm in cmdParms)
                {
                    mysqlAdapter.SelectCommand.Parameters.Add(parm);
                }
            }
            DataSet ds = new DataSet();

            mysqlAdapter.Fill(ds);
            return(ds.Tables[0]);
        }
Exemplo n.º 17
0
        public static void Start()
        {
            Cfg.Init();

            setLogName();

            setRepositories();

            log("General", "BeforeTestRun");

            TZ.Init(false);

            ControlMap.IsTest = true;

            SessionFactoryManager.Initialize <UserMap, User>(Cfg.DB);
            SessionManager.Init(getTicketKey);

            service = new ServiceAccess(getTicket, getSite);

            PlainText.Initialize(runPath);
        }
Exemplo n.º 18
0
        public IList <TKey> Save(IList <TEntity> entitys)
        {
            ISession session = SessionFactoryManager.GetCurrentSession();

            using (var tx = TransactionHelper.NhTransactionHelper.BeginTransaction())
            {
                try
                {
                    IList <TKey> keys = entitys.Select(entity => (TKey)session.Save(entity)).ToList();
                    tx.Commit(() =>
                    {
                        return(true);
                    });
                    return(keys);
                }
                catch
                {
                    tx.Rollback();
                    throw;
                }
            }
        }
        public void BeAbleToCreateASessionFactory()
        {
            var factory = SessionFactoryManager.GetSessionFactory();

            Assert.IsNotNull(factory);
        }
Exemplo n.º 20
0
 public static void End()
 {
     log("General", "End");
     SessionFactoryManager.End();
 }
Exemplo n.º 21
0
 public void SetUp()
 {
     var configurationProperties = new Dictionary<string, string>
                          {
                              { "connection.driver_class", "NHibernate.Driver.SQLite20Driver" },
                              { "dialect", "NHibernate.Dialect.SQLiteDialect" },
                              { "connection.connection_string", "Data Source=:memory:;Version=3;New=True;" },
                              { "connection.provider", "NHibernate.Connection.DriverConnectionProvider" },
                              { "session_factory_name", "TestFactory" },
                              { "proxyfactory.factory_class", "NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle" },
                              { "current_session_context_class", "Aspid.NHibernate.Context.ConversationSessionContext, Aspid.NHibernate" },
                          };
     var configuration = new Configuration { Properties = configurationProperties };
     var sessionFactoryManager = new SessionFactoryManager(configuration);
     conversationManager = new NHibernateConversationManager(sessionFactoryManager);
 }
        /// <summary>
        /// Method to get the <see cref="SessionFactoryElement" /> object based on the <see cref="DataBaseConnectivity" />.
        /// </summary>
        /// <param name="dataBaseConnectivity"><see cref="DataBaseConnectivity" /> to be used for getting the <see cref="SessionFactoryElement" /></param>
        /// <returns>Returns <see cref="SessionFactoryElement" /></returns>
        private SessionFactoryElement GetSessionFactoryElementFromName(DataBaseConnectivity dataBaseConnectivity)
        {
            Check.Require(!string.IsNullOrEmpty(dataBaseConnectivity.ToString()),
                "DataBaseConnectivity should not be null nor empty");

            SessionFactoryElement sessionFactoryElement = null;

            try
            {

                using (SessionFactoryManager sfm = new SessionFactoryManager())
                {
                    sessionFactoryElement = sfm.GetSessionFactoryElementFromName(dataBaseConnectivity);
                }

                Check.Require(sessionFactoryElement != null,
                    "SessionFactoryElement should not be null.");
            }
            catch (Exception ex)
            {
                Logger.Error("Error at GetSessionFactoryElementFromName method", ex);
                throw;
            }

            return sessionFactoryElement;
        }
Exemplo n.º 23
0
 /// <summary>
 /// 刷新一级缓存中的单个实体
 /// </summary>
 /// <param name="entity"></param>
 public void Refresh(TEntity entity)
 {
     SessionFactoryManager.GetCurrentSession().Refresh(entity);
 }
Exemplo n.º 24
0
 public void DisposeSession()
 {
     SessionFactoryManager.Dispose();
 }
Exemplo n.º 25
0
 public void ClearSession()
 {
     SessionFactoryManager.Clear();
 }
Exemplo n.º 26
0
 /// <summary>
 /// Get Current Session
 /// </summary>
 /// <returns>返回ISession</returns>
 public ISession GetCurrentSession()
 {
     return(SessionFactoryManager.GetCurrentSession());
 }
Exemplo n.º 27
0
        public IQueryable <TEntity> Query()
        {
            ISession session = SessionFactoryManager.GetCurrentSession();

            return(session.Query <TEntity>());
        }
Exemplo n.º 28
0
 /// <summary>
 /// 完全的清除缓存
 /// </summary>
 /// <param name="entity"></param>
 public void Clear()
 {
     SessionFactoryManager.GetCurrentSession().Clear();
 }
Exemplo n.º 29
0
        public TEntity Load(TKey id)
        {
            ISession session = SessionFactoryManager.GetCurrentSession();

            return(session.Load <TEntity>(id));
        }
Exemplo n.º 30
0
 /// <summary>
 /// 清除对象一级缓存(Session.Flush()不会同步到数据库)
 /// </summary>
 /// <param name="entity"></param>
 public void Evict(TEntity entity)
 {
     SessionFactoryManager.GetCurrentSession().Evict(entity);
 }