DictionaryBasedConfig.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using System;
  2. using System.Collections.Generic;
  3. using Abp.Collections.Extensions;
  4. namespace Abp.Configuration
  5. {
  6. /// <summary>
  7. /// Used to set/get custom configuration.
  8. /// </summary>
  9. public class DictionaryBasedConfig : IDictionaryBasedConfig
  10. {
  11. /// <summary>
  12. /// Dictionary of custom configuration.
  13. /// </summary>
  14. protected Dictionary<string, object> CustomSettings { get; private set; }
  15. /// <summary>
  16. /// Gets/sets a config value.
  17. /// Returns null if no config with given name.
  18. /// </summary>
  19. /// <param name="name">Name of the config</param>
  20. /// <returns>Value of the config</returns>
  21. public object this[string name]
  22. {
  23. get { return CustomSettings.GetOrDefault(name); }
  24. set { CustomSettings[name] = value; }
  25. }
  26. /// <summary>
  27. /// Constructor.
  28. /// </summary>
  29. protected DictionaryBasedConfig()
  30. {
  31. CustomSettings = new Dictionary<string, object>();
  32. }
  33. /// <summary>
  34. /// Gets a configuration value as a specific type.
  35. /// </summary>
  36. /// <param name="name">Name of the config</param>
  37. /// <typeparam name="T">Type of the config</typeparam>
  38. /// <returns>Value of the configuration or null if not found</returns>
  39. public T Get<T>(string name)
  40. {
  41. var value = this[name];
  42. return value == null
  43. ? default(T)
  44. : (T) Convert.ChangeType(value, typeof (T));
  45. }
  46. /// <summary>
  47. /// Used to set a string named configuration.
  48. /// If there is already a configuration with same <paramref name="name"/>, it's overwritten.
  49. /// </summary>
  50. /// <param name="name">Unique name of the configuration</param>
  51. /// <param name="value">Value of the configuration</param>
  52. public void Set<T>(string name, T value)
  53. {
  54. this[name] = value;
  55. }
  56. /// <summary>
  57. /// Gets a configuration object with given name.
  58. /// </summary>
  59. /// <param name="name">Unique name of the configuration</param>
  60. /// <returns>Value of the configuration or null if not found</returns>
  61. public object Get(string name)
  62. {
  63. return Get(name, null);
  64. }
  65. /// <summary>
  66. /// Gets a configuration object with given name.
  67. /// </summary>
  68. /// <param name="name">Unique name of the configuration</param>
  69. /// <param name="defaultValue">Default value of the object if can not found given configuration</param>
  70. /// <returns>Value of the configuration or null if not found</returns>
  71. public object Get(string name, object defaultValue)
  72. {
  73. var value = this[name];
  74. if (value == null)
  75. {
  76. return defaultValue;
  77. }
  78. return this[name];
  79. }
  80. /// <summary>
  81. /// Gets a configuration object with given name.
  82. /// </summary>
  83. /// <typeparam name="T">Type of the object</typeparam>
  84. /// <param name="name">Unique name of the configuration</param>
  85. /// <param name="defaultValue">Default value of the object if can not found given configuration</param>
  86. /// <returns>Value of the configuration or null if not found</returns>
  87. public T Get<T>(string name, T defaultValue)
  88. {
  89. return (T)Get(name, (object)defaultValue);
  90. }
  91. /// <summary>
  92. /// Gets a configuration object with given name.
  93. /// </summary>
  94. /// <typeparam name="T">Type of the object</typeparam>
  95. /// <param name="name">Unique name of the configuration</param>
  96. /// <param name="creator">The function that will be called to create if given configuration is not found</param>
  97. /// <returns>Value of the configuration or null if not found</returns>
  98. public T GetOrCreate<T>(string name, Func<T> creator)
  99. {
  100. var value = Get(name);
  101. if (value == null)
  102. {
  103. value = creator();
  104. Set(name, value);
  105. }
  106. return (T) value;
  107. }
  108. }
  109. }