Example #1
0
        private SmartSqlConfigOptions LoadSmartSqlConfig()
        {
            var configFilePath = Path.Combine(AppContext.BaseDirectory, _sqlMapConfigFilePath);
            var configStream   = new ConfigStream
            {
                Path   = configFilePath,
                Stream = FileLoader.Load(configFilePath)
            };

            using (configStream.Stream)
            {
                XmlSerializer xmlSerializer  = new XmlSerializer(typeof(SmartSqlMapConfig));
                var           smartSqlConfig = xmlSerializer.Deserialize(configStream.Stream) as SmartSqlMapConfig;
                if (smartSqlConfig.TypeHandlers != null)
                {
                    foreach (var typeHandler in smartSqlConfig.TypeHandlers)
                    {
                        typeHandler.Handler = TypeHandlerFactory.Create(typeHandler.Type);
                    }
                }

                return(new SmartSqlConfigOptions()
                {
                    Settings = smartSqlConfig.Settings,
                    SmartSqlMaps = smartSqlConfig.SmartSqlMapSources,
                    TypeHandlers = smartSqlConfig.TypeHandlers
                });
            }
        }
Example #2
0
 public SmartSqlMapConfig LoadConfig(ConfigStream configStream)
 {
     using (configStream.Stream)
     {
         XmlSerializer xmlSerializer = new XmlSerializer(typeof(SmartSqlMapConfig));
         SqlMapConfig              = xmlSerializer.Deserialize(configStream.Stream) as SmartSqlMapConfig;
         SqlMapConfig.Path         = configStream.Path;
         SqlMapConfig.SmartSqlMaps = new Dictionary <String, SmartSqlMap> {
         };
         if (SqlMapConfig.TypeHandlers != null)
         {
             foreach (var typeHandler in SqlMapConfig.TypeHandlers)
             {
                 typeHandler.Handler = TypeHandlerFactory.Create(typeHandler.Type);
             }
         }
         return(SqlMapConfig);
     }
 }
Example #3
0
        public override SmartSqlMapConfig Load()
        {
            SqlMapConfig = new SmartSqlMapConfig()
            {
                SmartSqlMaps       = new Dictionary <String, SmartSqlMap>(),
                SmartSqlMapSources = _options.SmartSqlMaps,
                Database           = new Configuration.Database()
                {
                    DbProvider      = _options.Database.DbProvider,
                    WriteDataSource = _options.Database.Write,
                    ReadDataSources = _options.Database.Read
                },
                Settings     = _options.Settings,
                TypeHandlers = _options.TypeHandlers,
            };
            if (SqlMapConfig.TypeHandlers != null)
            {
                foreach (var typeHandler in SqlMapConfig.TypeHandlers)
                {
                    typeHandler.Handler = TypeHandlerFactory.Create(typeHandler.Type);
                }
            }
            foreach (var sqlMapSource in SqlMapConfig.SmartSqlMapSources)
            {
                switch (sqlMapSource.Type)
                {
                case SmartSqlMapSource.ResourceType.File:
                {
                    LoadSmartSqlMap(SqlMapConfig, sqlMapSource.Path);
                    break;
                }

                case SmartSqlMapSource.ResourceType.Directory:
                case SmartSqlMapSource.ResourceType.DirectoryWithAllSub:
                {
                    SearchOption searchOption = SearchOption.TopDirectoryOnly;
                    if (sqlMapSource.Type == SmartSqlMapSource.ResourceType.DirectoryWithAllSub)
                    {
                        searchOption = SearchOption.AllDirectories;
                    }
                    var dicPath            = Path.Combine(AppContext.BaseDirectory, sqlMapSource.Path);
                    var childSqlmapSources = Directory.EnumerateFiles(dicPath, "*.xml", searchOption);
                    foreach (var childSqlmapSource in childSqlmapSources)
                    {
                        LoadSmartSqlMap(SqlMapConfig, childSqlmapSource);
                    }
                    break;
                }

                default:
                {
                    if (_logger.IsEnabled(LogLevel.Debug))
                    {
                        _logger.LogDebug($"OptionConfigLoader unknow SmartSqlMapSource.ResourceType:{sqlMapSource.Type}.");
                    }
                    break;
                }
                }
            }
            InitDependency();
            if (_logger.IsEnabled(LogLevel.Debug))
            {
                _logger.LogDebug($"OptionConfigLoader Load End");
            }

            if (SqlMapConfig.Settings.IsWatchConfigFile)
            {
                if (_logger.IsEnabled(LogLevel.Debug))
                {
                    _logger.LogDebug($"OptionConfigLoader Load Add WatchConfig Starting.");
                }
                WatchConfig(SqlMapConfig);
                if (_logger.IsEnabled(LogLevel.Debug))
                {
                    _logger.LogDebug($"OptionConfigLoader Load Add WatchConfig End.");
                }
            }
            return(SqlMapConfig);
        }