BasicUsageDemo.cs 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. namespace Apq.Cfg.Samples.Demos;
  2. /// <summary>
  3. /// 示例 1: 基础用法 - JSON 配置与层级覆盖
  4. /// </summary>
  5. public static class BasicUsageDemo
  6. {
  7. public static async Task RunAsync(string baseDir)
  8. {
  9. Console.WriteLine("═══════════════════════════════════════════════════════════════");
  10. Console.WriteLine("示例 1: 基础用法 - JSON 配置与层级覆盖");
  11. Console.WriteLine("═══════════════════════════════════════════════════════════════\n");
  12. var configPath = Path.Combine(baseDir, "config.json");
  13. var localConfigPath = Path.Combine(baseDir, "config.local.json");
  14. // 创建基础配置
  15. File.WriteAllText(configPath, """
  16. {
  17. "App": {
  18. "Name": "MyApp",
  19. "Version": "1.0.0",
  20. "Debug": false
  21. },
  22. "Database": {
  23. "Host": "localhost",
  24. "Port": 3306,
  25. "Name": "mydb"
  26. }
  27. }
  28. """);
  29. // 创建本地覆盖配置(高优先级)
  30. File.WriteAllText(localConfigPath, """
  31. {
  32. "App": {
  33. "Debug": true
  34. },
  35. "Database": {
  36. "Host": "192.168.1.100"
  37. }
  38. }
  39. """);
  40. // 构建配置:level 越大优先级越高
  41. // 注意:环境变量不可写,所以 isPrimaryWriter 设置在 JSON 配置源上
  42. var cfg = new CfgBuilder()
  43. .AddJson(configPath, level: 0, writeable: false)
  44. .AddJson(localConfigPath, level: 1, writeable: true, isPrimaryWriter: true)
  45. .AddEnvironmentVariables(level: 2, prefix: "MYAPP_")
  46. .Build();
  47. // 读取配置
  48. Console.WriteLine("1.1 读取配置值:");
  49. Console.WriteLine($" App:Name = {cfg.Get("App:Name")}");
  50. Console.WriteLine($" App:Version = {cfg.Get("App:Version")}");
  51. Console.WriteLine($" App:Debug = {cfg.Get("App:Debug")} (被本地配置覆盖为 true)");
  52. Console.WriteLine($" Database:Host = {cfg.Get("Database:Host")} (被本地配置覆盖)");
  53. Console.WriteLine($" Database:Port = {cfg.Get("Database:Port")}");
  54. // 检查配置是否存在
  55. Console.WriteLine("\n1.2 检查配置是否存在:");
  56. Console.WriteLine($" Exists(App:Name) = {cfg.Exists("App:Name")}");
  57. Console.WriteLine($" Exists(NotExist:Key) = {cfg.Exists("NotExist:Key")}");
  58. // 修改配置(写入到 isPrimaryWriter 的配置源,需要指定 targetLevel)
  59. Console.WriteLine("\n1.3 修改配置:");
  60. cfg.Set("App:LastRun", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), targetLevel: 1);
  61. await cfg.SaveAsync(targetLevel: 1);
  62. Console.WriteLine($" 已设置 App:LastRun = {cfg.Get("App:LastRun")}");
  63. // 删除配置
  64. Console.WriteLine("\n1.4 删除配置:");
  65. cfg.Set("App:TempKey", "临时值", targetLevel: 1);
  66. Console.WriteLine($" 设置 App:TempKey = {cfg.Get("App:TempKey")}");
  67. cfg.Remove("App:TempKey", targetLevel: 1);
  68. await cfg.SaveAsync(targetLevel: 1);
  69. Console.WriteLine($" 删除后 App:TempKey = {cfg.Get("App:TempKey") ?? "(null)"}");
  70. // 转换为 Microsoft.Extensions.Configuration
  71. Console.WriteLine("\n1.5 转换为 IConfigurationRoot:");
  72. var msConfig = cfg.ToMicrosoftConfiguration();
  73. Console.WriteLine($" msConfig[\"App:Name\"] = {msConfig["App:Name"]}");
  74. cfg.Dispose();
  75. File.Delete(configPath);
  76. File.Delete(localConfigPath);
  77. Console.WriteLine("\n[示例 1 完成]\n");
  78. }
  79. }