ConfigurationService.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Web.Configuration;
  8. using GreenTree.Nachtragsmanagement.Core.Configuration;
  9. using GreenTree.Nachtragsmanagement.Core.Domain.Config;
  10. using GreenTree.Nachtragsmanagement.Core.Data;
  11. namespace GreenTree.Nachtragsmanagement.Services.Configuration
  12. {
  13. public class ConfigurationService : IConfigurationService
  14. {
  15. #region Fields
  16. private readonly IRepository<ConfigItem> _configItemRepository;
  17. #endregion
  18. #region Ctor
  19. /// <summary>
  20. /// Initializes a new instance of the ConfigurationService class
  21. /// </summary>
  22. public ConfigurationService(
  23. IRepository<ConfigItem> configItemRepository)
  24. {
  25. _configItemRepository = configItemRepository;
  26. }
  27. #endregion
  28. /// <summary>
  29. /// Reads the current configuration from the global config
  30. /// </summary>
  31. public AppendixConfigurationSection GetCurrentConfiguration()
  32. {
  33. var config = WebConfigurationManager.OpenWebConfiguration("~/");
  34. var sectionGroup = config.GetSectionGroup("appendixSectionGroup");
  35. var section = sectionGroup.Sections["appendixConfigSection"];
  36. return (AppendixConfigurationSection)section;
  37. }
  38. #region ConfigItem
  39. /// <summary>
  40. /// Gets all configItems
  41. /// </summary>
  42. public IList<ConfigItem> GetAllConfigItems()
  43. {
  44. return _configItemRepository.Table.ToList();
  45. }
  46. /// <summary>
  47. /// Gets a configItem by specified Id
  48. /// </summary>
  49. /// <param name="id">ConfigItem identifier.</param>
  50. public ConfigItem GetConfigItemById(int id)
  51. {
  52. return _configItemRepository.GetById(id);
  53. }
  54. /// <summary>
  55. /// Gets a configItem by specified name
  56. /// </summary>
  57. /// <param name="name">ConfigItem name.</param>
  58. public ConfigItem GetConfigItemByName(string name)
  59. {
  60. return _configItemRepository.Table
  61. .FirstOrDefault(r => r.Name == name);
  62. }
  63. /// <summary>
  64. /// Gets all configItems to the specified ids
  65. /// </summary>
  66. public IList<ConfigItem> GetConfigItemsByIds(int[] ids)
  67. {
  68. return _configItemRepository.Table
  69. .Where(r => ids.Contains(r.Id))
  70. .ToList();
  71. }
  72. /// <summary>
  73. /// Insert a configItem
  74. /// </summary>
  75. /// <param name="configItem">ConfigItem.</param>
  76. public void InsertConfigItem(ConfigItem configItem)
  77. {
  78. _configItemRepository.Insert(configItem);
  79. }
  80. /// <summary>
  81. /// Update a configItem
  82. /// </summary>
  83. /// <param name="configItem">ConfigItem.</param>
  84. public void UpdateConfigItem(ConfigItem configItem)
  85. {
  86. _configItemRepository.Update(configItem);
  87. }
  88. /// <summary>
  89. /// Delete a configItem
  90. /// </summary>
  91. /// <param name="configItem">ConfigItem.</param>
  92. public void DeleteConfigItem(ConfigItem configItem)
  93. {
  94. _configItemRepository.Delete(configItem);
  95. }
  96. #endregion
  97. #region Casting
  98. /// <summary>
  99. /// Trys to convert the config items value type and returns its actual value, otherwise NULL
  100. /// </summary>
  101. /// <typeparam name="T">The actual type.</typeparam>
  102. /// <param name="configItem">ConfigItem.</param>
  103. /// <param name="success">Determines if the conversion was possible.</param>
  104. public T TryGetConfigItemValue<T>(ConfigItem configItem, out bool success)
  105. {
  106. success = false;
  107. if (configItem == null)
  108. {
  109. return default(T);
  110. }
  111. object result = null;
  112. try
  113. {
  114. result = Convert.ChangeType(configItem.Value, typeof(T));
  115. success = true;
  116. }
  117. catch
  118. {
  119. success = false;
  120. }
  121. return (T)result;
  122. }
  123. #endregion
  124. }
  125. }