统一 Dump、Logging、Database 等功能的配置文件加载和定义类,优化ConfigLoader,职责分散各个模块。
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
using System.Configuration;
|
||||
|
||||
namespace XP.Common.Database.Configs
|
||||
{
|
||||
/// <summary>
|
||||
/// SQLite 配置加载器,从 App.config 读取数据库相关配置项 | SQLite configuration loader, reads database-related configuration from App.config
|
||||
/// </summary>
|
||||
public static class ConfigLoader
|
||||
{
|
||||
/// <summary>
|
||||
/// 配置键前缀 | Configuration key prefix
|
||||
/// </summary>
|
||||
private const string KeyPrefix = "Sqlite:";
|
||||
|
||||
/// <summary>
|
||||
/// 从 App.config 加载 SQLite 配置 | Load SQLite configuration from App.config
|
||||
/// </summary>
|
||||
/// <returns>SQLite 配置实体,缺失或无效配置项使用默认值 | SQLite configuration entity, uses default values for missing or invalid items</returns>
|
||||
public static SqliteConfig LoadSqliteConfig()
|
||||
{
|
||||
var config = new SqliteConfig();
|
||||
|
||||
// 加载数据库文件路径 | Load database file path
|
||||
var dbPath = ConfigurationManager.AppSettings[KeyPrefix + "DbFilePath"];
|
||||
if (!string.IsNullOrEmpty(dbPath)) config.DbFilePath = dbPath;
|
||||
|
||||
// 加载连接超时时间 | Load connection timeout
|
||||
var timeout = ConfigurationManager.AppSettings[KeyPrefix + "ConnectionTimeout"];
|
||||
if (int.TryParse(timeout, out var t) && t > 0) config.ConnectionTimeout = t;
|
||||
|
||||
// 加载是否自动创建 | Load create if not exists
|
||||
var createIfNotExists = ConfigurationManager.AppSettings[KeyPrefix + "CreateIfNotExists"];
|
||||
if (bool.TryParse(createIfNotExists, out var c)) config.CreateIfNotExists = c;
|
||||
|
||||
// 加载是否启用 WAL 模式 | Load enable WAL mode
|
||||
var enableWal = ConfigurationManager.AppSettings[KeyPrefix + "EnableWalMode"];
|
||||
if (bool.TryParse(enableWal, out var w)) config.EnableWalMode = w;
|
||||
|
||||
// 加载是否开启 SQL 日志 | Load enable SQL logging
|
||||
var enableSqlLog = ConfigurationManager.AppSettings[KeyPrefix + "EnableSqlLogging"];
|
||||
if (bool.TryParse(enableSqlLog, out var l)) config.EnableSqlLogging = l;
|
||||
|
||||
return config;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace XP.Common.Database.Configs
|
||||
{
|
||||
/// <summary>
|
||||
/// SQLite 配置实体(从 App.config 读取)| SQLite configuration entity (loaded from App.config)
|
||||
/// </summary>
|
||||
public class SqliteConfig
|
||||
{
|
||||
/// <summary>
|
||||
/// 数据库文件路径 | Database file path
|
||||
/// </summary>
|
||||
public string DbFilePath { get; set; } = Path.Combine(
|
||||
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
|
||||
"Files", "Data", "XP.db");
|
||||
|
||||
/// <summary>
|
||||
/// 连接超时时间(秒,默认30)| Connection timeout (seconds, default 30)
|
||||
/// </summary>
|
||||
public int ConnectionTimeout { get; set; } = 30;
|
||||
|
||||
/// <summary>
|
||||
/// 数据库不存在时是否自动创建(默认true)| Whether to auto-create if not exists (default true)
|
||||
/// </summary>
|
||||
public bool CreateIfNotExists { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// 是否启用 WAL 模式(提升并发性能,默认true)| Whether to enable WAL mode (default true)
|
||||
/// </summary>
|
||||
public bool EnableWalMode { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// 是否开启日志记录(记录所有SQL操作,默认false)| Whether to enable SQL logging (default false)
|
||||
/// </summary>
|
||||
public bool EnableSqlLogging { get; set; } = false;
|
||||
|
||||
/// <summary>
|
||||
/// 获取 SQLite 连接字符串 | Get SQLite connection string
|
||||
/// </summary>
|
||||
public string GetConnectionString()
|
||||
{
|
||||
var builder = new Microsoft.Data.Sqlite.SqliteConnectionStringBuilder
|
||||
{
|
||||
DataSource = DbFilePath,
|
||||
Cache = Microsoft.Data.Sqlite.SqliteCacheMode.Default,
|
||||
DefaultTimeout = ConnectionTimeout
|
||||
};
|
||||
return builder.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user