コード例 #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ProviderRepository&lt;T&gt;"/> class.
        /// </summary>
        /// <param name="provider">The provider.</param>
        public ProviderRepository(
            T provider)
        {
            _providers = new ProviderCollection <T>();
            _providers.Add(provider);

            _provider = provider;
        }
コード例 #2
0
        /// <summary>
        /// Loads the providers.
        /// </summary>
        protected virtual void LoadProviders(
            bool throwErrorIfUnableToLoadSection)
        {
            // Avoid claiming lock if providers are already loaded
            if (_providers == null)
            {
                lock (_syncRoot)
                {
                    // Do this again to make sure provider is still null
                    if (_providers == null)
                    {
                        // Get the providers
                        _providers = new ProviderCollection <T>();

                        // Get a reference to the section
                        ProvidersSection section = null;
                        try
                        {
                            section =
                                ConfigurationManager.GetSection(_sectionName) as ProvidersSection;
                        }
                        catch (Exception)
                        {
                            section = null;
                        }

                        if (section == null)
                        {
                            if (throwErrorIfUnableToLoadSection)
                            {
                                throw new ProviderException(
                                          String.Format("Unable to load configuration section: '{0}'.",
                                                        _sectionName));
                            }
                        }
                        else
                        {
                            ProvidersHelper.InstantiateProviders(section.Providers, _providers, typeof(T));

                            if (_providers.Count > 0)
                            {
                                // If there is a default provider specified, then grab it,
                                // else grab the first provider in the collection
                                if (!String.IsNullOrEmpty(section.DefaultProvider))
                                {
                                    _provider = (T)_providers[section.DefaultProvider];
                                }
                                else
                                {
                                    _provider = _providers[0];
                                }
                            }

                            if (throwErrorIfUnableToLoadSection)
                            {
                                // If we have no provider, then throw an exception
                                if (_provider == null)
                                {
                                    throw new ProviderException(
                                              String.Format("Unable to load default provider for section: '{0}'.",
                                                            _sectionName));
                                }
                            }
                        }
                    }
                }
            }
        }
コード例 #3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ProviderRepository&lt;T&gt;"/> class.
 /// </summary>
 /// <param name="providers">The providers.</param>
 public ProviderRepository(
     ProviderCollection <T> providers)
 {
     _providers = providers;
 }