locked
Singleton CRM Configuration Manager Class for plugins or workflows RRS feed

  • General discussion

  • Create Crm Configuration Entity with new_key,new_value,new_description text fields.

    string value = ConfigurationManager.Instance.GetValue(ConfigurationKey.TimeForCheckRound, _service);

    using System;
    using System.Collections.Generic;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Query;
    
    namespace Plugins
    {
        /// <summary>
        /// CRMConfiguration maintains a list of ConfigurationContainer in a 
        /// globally accessible hashtable. This is a Singleton helper class to the 
        /// getting data from CRM Configurations entity.    
        /// </summary>
        /// <remarks>
        /// Singleton.
        /// 
        /// The Singleton Design Pattern ensures that just one instance (the Singleton) 
        /// holds a reference to a list of all ConfigurationContainer providers declared in the 
        /// CRM Configuration entity.
        /// </remarks>
        public class ConfigurationManager
        {
            /// <summary>
            /// The one and only Singleton ConfigurationManager instance. 
            /// </summary>
            /// <remarks>
            /// This is the single instance of this class
            /// </remarks>
            private static readonly ConfigurationManager _uniqueInstance = new ConfigurationManager();
            /// <summary>
            /// Lock synchronization object
            /// </summary>
            private static readonly object _syncLock = new object();
            /// <summary>
            /// Type-safe generic ConfigurationContainer
            /// </summary>
            private ConfigurationContainer container = null;
            /// <summary>
            /// Create ConfigurationContainer instance.
            /// </summary>
            /// <remarks>
            /// Private constructor.
            /// </remarks>
            private ConfigurationManager()
            {
                //create a ConfigurationContainer instance
                container = new ConfigurationContainer();
            }
            /// <summary>
            /// Gets the instance of the ConfigurationManager object.
            /// </summary>
            public static ConfigurationManager Instance
            {
                get
                {
                    return _uniqueInstance;
                }
            }
    
            /// <summary>
            /// Gets or sets the value of the specified key
            /// </summary>
            /// <param name="configurationKey">key of crm configuration entity</param>
            /// <param name="service">IOrganizationService instance</param>
            /// <returns>value of the key parameter</returns>
            public string GetValue(ConfigurationKey configurationKey, IOrganizationService service)
            {
                string key = configurationKey.ToString();
                // if key is already exist than directly return value.
                if (container.ContainsKey(key))
                    return container[key];
                // Lock area where instance is created
                lock (_syncLock)
                {
                    //control for more than one thread
                    if (container.ContainsKey(key)) return container[key];
    
                    //Entity definition
                    const string entityName = "new_crmconfiguration";
                    const string valueAttributeName = "new_value";
                    const string keyAttributeName = "new_key";
    
                    //read value from crm
                    var fetch = string.Format(@"
                            <fetch version='1.0' count='1' output-format='xml-platform' mapping='logical' distinct='false'>
                              <entity name='{0}'>
                                <attribute name='{1}' />
                                <filter type='and'>
                                  <condition attribute='statecode' operator='eq' value='0' />
                                  <condition attribute='{2}' operator='eq' value='{3}' />
                                </filter>
                              </entity>
                            </fetch>",entityName,valueAttributeName,keyAttributeName,key);
    
                    EntityCollection collection = service.RetrieveMultiple(new FetchExpression(fetch));
                    if (collection.Entities.Count > 0 && collection.Entities[0].Attributes.Contains(valueAttributeName))
                    {
                        //Add new key and value to dictionary
                        container.Add(key, collection.Entities[0][valueAttributeName].ToString());
                    }
                    else
                    {
                        throw new Exception("configuration key not found...");
                    }
                }
    
                return container[key];
            }
        }
        /// <summary>    
        /// Gets or sets a list of ConfigurationContainers dictionary that contains key and value
        /// </summary>
        internal class ConfigurationContainer : Dictionary<string, string>
        {
        }
    
        /// <summary>
        /// Gets or sets a configuration key that contains key of configuration entity.
        /// </summary>
        public enum ConfigurationKey
        {
            TimeForCheckRound,
        } 
    }
    



    Ms Crm developer

    Tuesday, August 12, 2014 8:43 AM