예제 #1
0
        public void Setup()
        {
            if (String.IsNullOrEmpty(ConfigPath))
            {
                ConfigPath = Consts.DEFAULT_SMARTSQL_CONFIG_PATH;
            }
            if (String.IsNullOrEmpty(Alias))
            {
                Alias = ConfigPath;
            }
            if (LoggerFactory == null)
            {
                LoggerFactory = NoneLoggerFactory.Instance;
            }

            if (ConfigLoader == null)
            {
                ConfigLoader = new LocalFileConfigLoader(ConfigPath, LoggerFactory);
            }
            var sqlMapConfig = ConfigLoader.Load();

            SmartSqlContext = new SmartSqlContext(LoggerFactory.CreateLogger <SmartSqlContext>(), sqlMapConfig);
            if (DbSessionStore == null)
            {
                DbSessionStore = new DbConnectionSessionStore(LoggerFactory, SmartSqlContext.DbProvider.Factory);
            }
            if (DataSourceFilter == null)
            {
                DataSourceFilter = new DataSourceFilter(LoggerFactory.CreateLogger <DataSourceFilter>(), DbSessionStore, SmartSqlContext);
            }
            if (SqlBuilder == null)
            {
                SqlBuilder = new SqlBuilder(LoggerFactory.CreateLogger <SqlBuilder>());
            }

            if (PreparedCommand == null)
            {
                PreparedCommand = new PreparedCommand(LoggerFactory.CreateLogger <PreparedCommand>(), SmartSqlContext);
            }
            if (CommandExecuter == null)
            {
                CommandExecuter = new CommandExecuter(LoggerFactory.CreateLogger <CommandExecuter>(), PreparedCommand);
            }
            if (DataReaderDeserializerFactory == null)
            {
                DataReaderDeserializerFactory = new EmitDataReaderDeserializerFactory();
            }
            if (CacheManager == null)
            {
                if (SmartSqlContext.IsCacheEnabled)
                {
                    CacheManager = new CacheManager(LoggerFactory.CreateLogger <CacheManager>(), SmartSqlContext, DbSessionStore);
                }
                else
                {
                    CacheManager = new NoneCacheManager();
                }
            }
            ConfigLoader.OnChanged += ConfigLoader_OnChanged;
        }
예제 #2
0
 public T ExecuteWrap <T>(Func <IDbConnectionSession, T> execute, RequestContext context, DataSourceChoice sourceChoice = DataSourceChoice.Write)
 {
     SetupRequestContext(context, sourceChoice);
     if (CacheManager.TryGet <T>(context, out T cachedResult))
     {
         return(cachedResult);
     }
     return(WrapWithTransaction <T>(context, (dbSession) =>
     {
         if (dbSession == null)
         {
             var dataSource = DataSourceFilter.Elect(context);
             dbSession = SessionStore.GetOrAddDbSession(dataSource);
         }
         try
         {
             var result = execute(dbSession);
             CacheManager.RequestExecuted(dbSession, context);
             CacheManager.TryAdd <T>(context, result);
             return result;
         }
         catch (Exception ex)
         {
             _logger.LogError(ex.HelpLink, ex, ex.Message);
             throw ex;
         }
         finally
         {
             if (dbSession.LifeCycle == DbSessionLifeCycle.Transient)
             {
                 SessionStore.Dispose();
             }
         }
     }));
 }
예제 #3
0
        public async Task <T> ExecuteWrapAsync <T>(Func <IDbConnectionSession, Task <T> > execute, RequestContext context)
        {
            SetupRequestContext(context);
            if (CacheManager.TryGet <T>(context, out T cachedResult))
            {
                return(cachedResult);
            }
            var dataSource = DataSourceFilter.Elect(context);
            var dbSession  = SessionStore.GetOrAddDbSession(dataSource);

            try
            {
                var result = await execute(dbSession).ConfigureAwait(false);

                CacheManager.RequestExecuted(dbSession, context);
                CacheManager.TryAdd <T>(context, result);
                return(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.HelpLink, ex, ex.Message);
                throw ex;
            }
            finally
            {
                if (dbSession.LifeCycle == DbSessionLifeCycle.Transient)
                {
                    SessionStore.Dispose();
                }
            }
        }
예제 #4
0
        public IDbConnectionSession BeginSession(RequestContext context)
        {
            if (SessionStore.LocalSession != null)
            {
                throw new SmartSqlException("SmartSqlMapper could not invoke BeginSession(). A LocalSession is already existed.");
            }
            var dataSource = DataSourceFilter.Elect(context);
            var dbSession  = SessionStore.CreateDbSession(dataSource);

            dbSession.Begin();
            return(dbSession);
        }