| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Web.Configuration;
- using GreenTree.Nachtragsmanagement.Core.Configuration;
- using GreenTree.Nachtragsmanagement.Core.Domain.Config;
- using GreenTree.Nachtragsmanagement.Core.Data;
- namespace GreenTree.Nachtragsmanagement.Services.Configuration
- {
- public class ConfigurationService : IConfigurationService
- {
- #region Fields
- private readonly IRepository<ConfigItem> _configItemRepository;
- #endregion
- #region Ctor
- /// <summary>
- /// Initializes a new instance of the ConfigurationService class
- /// </summary>
- public ConfigurationService(
- IRepository<ConfigItem> configItemRepository)
- {
- _configItemRepository = configItemRepository;
- }
- #endregion
- /// <summary>
- /// Reads the current configuration from the global config
- /// </summary>
- public AppendixConfigurationSection GetCurrentConfiguration()
- {
- var config = WebConfigurationManager.OpenWebConfiguration("~/");
- var sectionGroup = config.GetSectionGroup("appendixSectionGroup");
- var section = sectionGroup.Sections["appendixConfigSection"];
- return (AppendixConfigurationSection)section;
- }
- #region ConfigItem
- /// <summary>
- /// Gets all configItems
- /// </summary>
- public IList<ConfigItem> GetAllConfigItems()
- {
- return _configItemRepository.Table.ToList();
- }
- /// <summary>
- /// Gets a configItem by specified Id
- /// </summary>
- /// <param name="id">ConfigItem identifier.</param>
- public ConfigItem GetConfigItemById(int id)
- {
- return _configItemRepository.GetById(id);
- }
- /// <summary>
- /// Gets a configItem by specified name
- /// </summary>
- /// <param name="name">ConfigItem name.</param>
- public ConfigItem GetConfigItemByName(string name)
- {
- return _configItemRepository.Table
- .FirstOrDefault(r => r.Name == name);
- }
- /// <summary>
- /// Gets all configItems to the specified ids
- /// </summary>
- public IList<ConfigItem> GetConfigItemsByIds(int[] ids)
- {
- return _configItemRepository.Table
- .Where(r => ids.Contains(r.Id))
- .ToList();
- }
- /// <summary>
- /// Insert a configItem
- /// </summary>
- /// <param name="configItem">ConfigItem.</param>
- public void InsertConfigItem(ConfigItem configItem)
- {
- _configItemRepository.Insert(configItem);
- }
- /// <summary>
- /// Update a configItem
- /// </summary>
- /// <param name="configItem">ConfigItem.</param>
- public void UpdateConfigItem(ConfigItem configItem)
- {
- _configItemRepository.Update(configItem);
- }
- /// <summary>
- /// Delete a configItem
- /// </summary>
- /// <param name="configItem">ConfigItem.</param>
- public void DeleteConfigItem(ConfigItem configItem)
- {
- _configItemRepository.Delete(configItem);
- }
- #endregion
- #region Casting
- /// <summary>
- /// Trys to convert the config items value type and returns its actual value, otherwise NULL
- /// </summary>
- /// <typeparam name="T">The actual type.</typeparam>
- /// <param name="configItem">ConfigItem.</param>
- /// <param name="success">Determines if the conversion was possible.</param>
- public T TryGetConfigItemValue<T>(ConfigItem configItem, out bool success)
- {
- success = false;
- if (configItem == null)
- {
- return default(T);
- }
- object result = null;
- try
- {
- result = Convert.ChangeType(configItem.Value, typeof(T));
- success = true;
- }
- catch
- {
- success = false;
- }
- return (T)result;
- }
- #endregion
- }
- }
|