Ejemplo n.º 1
0
        /// <summary>
        /// Initializes the specified throw exception.
        /// </summary>
        /// <param name="throwException">if set to <c>true</c> [throw exception].</param>
        /// <returns></returns>
        protected override Dictionary <string, RuntimeConfigurationItem> Initialize(bool throwException = false)
        {
            Dictionary <string, RuntimeConfigurationItem> result = new Dictionary <string, RuntimeConfigurationItem>();

            try
            {
                SqlConnectionString.CheckEmptyString(nameof(SqlConnectionString));

                using (var controller = new ConfigurationRawItemAccessController(SqlConnectionString))
                {
                    var rawConfigurations = controller.GetSystemConfigurations();

                    foreach (var item in rawConfigurations)
                    {
                        var runtimeConfigurationItem = RuntimeConfigurationItem.FromRaw(SourceAssembly, nameof(SqlConfigurationReader), CoreComponentVersion, item);
                        result.AddIfBothNotNull(runtimeConfigurationItem?.Key, runtimeConfigurationItem);
                    }
                }
            }
            catch (Exception ex)
            {
                if (throwException)
                {
                    throw ex.Handle();
                }
            }

            return(result);
        }
        /// <summary>
        /// Tries the get configuration.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="result">The result.</param>
        /// <returns></returns>
        public bool TryGetConfiguration(string key, out object result)
        {
            RuntimeConfigurationItem configuration = null;

            if (_items.SafeTryGetValue(key, out configuration) && configuration.IsActive)
            {
                result = configuration.Value;
                return(true);
            }

            result = default(object);
            return(false);
        }
        /// <summary>
        /// Tries the get configuration.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key">The key.</param>
        /// <param name="result">The result.</param>
        /// <returns></returns>
        public bool TryGetConfiguration <T>(string key, out T result)
        {
            RuntimeConfigurationItem configuration = null;

            if (_items.SafeTryGetValue(key, out configuration) && configuration.IsActive)
            {
                result = configuration.Value == null ? default(T) : (T)(configuration.Value);
                return(true);
            }

            result = default(T);
            return(false);
        }
        /// <summary>
        /// Fills the object collection.
        /// </summary>
        /// <param name="container">The container.</param>
        /// <param name="coreComponentVersion">The core component version.</param>
        /// <param name="configurationRawItem">The configuration raw item.</param>
        /// <param name="sourceAssembly">The source assembly.</param>
        /// <param name="readerType">Type of the reader.</param>
        /// <param name="throwException">if set to <c>true</c> [throw exception].</param>
        protected static void FillObjectCollection(IDictionary <string, RuntimeConfigurationItem> container, Version coreComponentVersion, ConfigurationRawItem configurationRawItem, string sourceAssembly, string readerType, bool throwException = false)
        {
            try
            {
                container.CheckNullObject(nameof(container));
                configurationRawItem.CheckNullObject(nameof(configurationRawItem));
                configurationRawItem.Key.CheckEmptyString(nameof(configurationRawItem.Key));

                var runtimeConfigurationItem = RuntimeConfigurationItem.FromRaw(sourceAssembly, readerType, coreComponentVersion, configurationRawItem);
                runtimeConfigurationItem.CheckNullObject(nameof(runtimeConfigurationItem));
                container.Merge(runtimeConfigurationItem.Key, runtimeConfigurationItem);
            }
            catch (Exception ex)
            {
                if (throwException)
                {
                    throw ex.Handle(data: new { coreComponentVersion, configurationRawItem, sourceAssembly, readerType });
                }
            }
        }
        /// <summary>
        /// Froms the raw.
        /// </summary>
        /// <param name="sourceAssembly">The source assembly.</param>
        /// <param name="readerType">Type of the reader.</param>
        /// <param name="coreComponentVersion">The core component version.</param>
        /// <param name="configurationItem">The configuration item.</param>
        /// <returns></returns>
        public static RuntimeConfigurationItem FromRaw(string sourceAssembly, string readerType, Version coreComponentVersion, ConfigurationRawItem configurationItem)
        {
            try
            {
                configurationItem.CheckNullObject(nameof(configurationItem));
                configurationItem.Type.CheckEmptyString(nameof(configurationItem.Type));

                var result = new RuntimeConfigurationItem(configurationItem)
                {
                    Assembly    = sourceAssembly,
                    ReaderType  = readerType,
                    RuntimeType = ReflectionExtension.SmartGetType(configurationItem.Type, false) ?? ReflectionExtension.SmartGetType(configurationItem.Type, true)
                };

                result.IsActive = JudgeActive(coreComponentVersion, result.MinComponentVersionRequired, result.MaxComponentVersionLimited);
                result.Value    = RawStringToObject(configurationItem.Value, result.RuntimeType);

                return(result);
            }
            catch (Exception ex)
            {
                throw ex.Handle();
            }
        }