// // Manager.cs // // Author: // Paul Schneider // // Copyright (c) 2015 GNU GPL // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public License // along with this program. If not, see . using System; using System.Configuration; using System.Reflection; using System.Configuration.Provider; using System.Collections.Specialized; using System.Linq; using System.Collections.Generic; namespace Yavsc.Model { /// /// Manager. /// public static class ManagerHelper { /// /// Gets the provider. /// /// The provider. public static TProvider CreateDefaultProvider (string configSetion) where TProvider: ProviderBase { var config = GetConfiguration (configSetion); ProviderSettings celt = config.Providers [config.DefaultProvider]; return CreateProvider (celt.Type, celt.Name, celt.Parameters); } /// /// Creates the providers. /// /// The providers. /// Config setion. /// The 1st type parameter. public static TProvider [] CreateProviders (string configSetion) where TProvider: ProviderBase { var config = GetConfiguration (configSetion); List providers = new List (); foreach (ProviderSettings provConfig in config.Providers) { var prov = CreateProvider (provConfig.Type, provConfig.Name, provConfig.Parameters); providers.Add (prov); } return providers.ToArray (); } private static DataProviderConfigurationSection GetConfiguration(string configSetion) { DataProviderConfigurationSection config = ConfigurationManager.GetSection (configSetion) as DataProviderConfigurationSection; if (config == null) throw new ConfigurationErrorsException ( string.Format( "The providers configuration bloc `{0}` was not found", configSetion)); return config; } private static TProvider CreateProvider(string type, string name, NameValueCollection config) where TProvider: ProviderBase { Type provtype = Type.GetType (type); if (provtype == null) throw new ProviderException ( string.Format ( "Provider type '{0}' was not found",type)); ConstructorInfo ci = provtype.GetConstructor (Type.EmptyTypes); TProvider bp = ci.Invoke (Type.EmptyTypes) as TProvider; bp.Initialize (name, config); return bp; } /// /// Gets the default provider. /// /// The default provider. /// Config setion. public static ProviderBase CreateDefaultProvider (string configSetion) { return CreateDefaultProvider(configSetion); } /// /// Creates the providers. /// /// The providers. /// Config setion. public static ProviderBase[] CreateProviders (string configSetion) { return CreateProviders(configSetion); } } }