SubstitutingConfigurationProvider.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using Microsoft.Extensions.Configuration;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using EasyDevCore.Common;
  5. namespace EasyDevCore.Configuration
  6. {
  7. /// <summary>
  8. /// Auto substitute value from variableSection into section with value have data contains {@KeyName}
  9. /// </summary>
  10. /// <seealso cref="Microsoft.Extensions.Configuration.ConfigurationProvider" />
  11. public class SubstitutingConfigurationProvider : ConfigurationProvider
  12. {
  13. private readonly IConfigurationRoot _configurationRoot;
  14. private readonly string _variablesSection;
  15. private Dictionary<string, string> _variables = null;
  16. /// <summary>
  17. /// Initializes a new instance of the <see cref="SubstitutingConfigurationProvider"/> class.
  18. /// </summary>
  19. /// <param name="configurationRoot">The configuration root.</param>
  20. /// <param name="variablesSection">The variables section.</param>
  21. public SubstitutingConfigurationProvider(IConfigurationRoot configurationRoot, string variablesSection)
  22. {
  23. _configurationRoot = configurationRoot;
  24. _variablesSection = variablesSection;
  25. }
  26. /// <summary>
  27. /// Attempts to find a value with the given key, returns true if one is found, false otherwise.
  28. /// </summary>
  29. /// <param name="key">The key to lookup.</param>
  30. /// <param name="value">The value found at key if one is found.</param>
  31. /// <returns>True if key has a value, false otherwise.</returns>
  32. public override bool TryGet(string key, out string value)
  33. {
  34. try
  35. {
  36. foreach (var provider in _configurationRoot.Providers.Reverse())
  37. {
  38. if (provider != this)
  39. {
  40. if (provider.TryGet(key, out value))
  41. {
  42. value = SubstitutePlaceholders(value);
  43. return true;
  44. }
  45. }
  46. }
  47. }
  48. catch { }
  49. value = null;
  50. return false;
  51. }
  52. /// <summary>
  53. /// Sets a value for a given key.
  54. /// </summary>
  55. /// <param name="key">The configuration key to set.</param>
  56. /// <param name="value">The value to set.</param>
  57. public override void Set(string key, string value)
  58. {
  59. foreach (var provider in _configurationRoot.Providers)
  60. {
  61. if (provider != this)
  62. {
  63. provider.Set(key, value);
  64. }
  65. }
  66. }
  67. /// <summary>
  68. /// Loads (or reloads) the data for this provider.
  69. /// </summary>
  70. public override void Load()
  71. {
  72. if(_variables != null)
  73. {
  74. _variables.Clear();
  75. }
  76. else
  77. {
  78. _variables = new();
  79. }
  80. _configurationRoot.GetSection(_variablesSection).GetChildren().ForEach(kp => _variables.Add(kp.Key, kp.Value ?? string.Empty));
  81. }
  82. private string SubstitutePlaceholders(string value)
  83. {
  84. if (value == null || !value.Contains("{@")) return value;
  85. foreach (var varriableName in _variables)
  86. {
  87. value = value.Replace($"{{@{varriableName.Key}}}", varriableName.Value);
  88. if(!value.Contains("{@"))
  89. {
  90. break;
  91. }
  92. }
  93. return value;
  94. }
  95. }
  96. }