Ejemplo n.º 1
0
 /// <summary>
 /// If an exchange rate provider is configured, this method will attempt to update
 /// the exchange rate based from the provider data.
 /// </summary>
 /// <param name="throwOnError">When false, if an error occurs during the update process
 /// the error is logged and ignored.  When true, the error is logged and then rethrown
 /// for the calling process</param>
 public void UpdateExchangeRate(bool throwOnError)
 {
     try
     {
         Currency baseCurrency = Token.Instance.Store.BaseCurrency;
         if (baseCurrency.CurrencyId == this.CurrencyId)
         {
             this.ExchangeRate = 1;
         }
         else
         {
             //GET THE FOREX PROVIDER
             IForexProvider provider = ForexProviderDataSource.GetCurrentProvider();
             if (provider != null)
             {
                 this.ExchangeRate = provider.GetExchangeRate(baseCurrency.ISOCode, this.ISOCode);
             }
             else
             {
                 Logger.Warn("Exchange rate update for " + this.Name + " failed because the rate provider could not be loaded.");
             }
         }
         this.Save();
     }
     catch
     {
         if (throwOnError)
         {
             throw;
         }
     }
 }
        public static List <IForexProvider> GetProviders()
        {
            List <IForexProvider> providers     = new List <IForexProvider>();
            List <string>         providerNames = new List <string>();

            foreach (System.Reflection.Assembly assemblyInstance in AppDomain.CurrentDomain.GetAssemblies())
            {
                try
                {
                    foreach (Type thisType in assemblyInstance.GetTypes())
                    {
                        if ((thisType.IsClass && !thisType.IsAbstract))
                        {
                            foreach (Type thisInterface in thisType.GetInterfaces())
                            {
                                IForexProvider instance = null;
                                if ((!string.IsNullOrEmpty(thisInterface.FullName) && thisInterface.FullName.Equals("CommerceBuilder.Stores.IForexProvider")))
                                {
                                    string classId        = Utility.Misc.GetClassId(thisType);
                                    string loweredClassId = classId.ToLowerInvariant();
                                    if (!providerNames.Contains(loweredClassId))
                                    {
                                        instance = Activator.CreateInstance(Type.GetType(classId)) as IForexProvider;
                                        if (instance != null)
                                        {
                                            providers.Add(instance);
                                        }
                                        providerNames.Add(loweredClassId);
                                    }
                                }
                            }
                        }
                    }
                }
                catch
                {
                    //ignore error
                }
            }
            return(providers);
        }
        /// <summary>
        /// Gets the current ForexProvider
        /// </summary>
        /// <returns>The current ForexProvider</returns>
        public static IForexProvider GetCurrentProvider()
        {
            IForexProvider instance = null;
            Store          store    = Token.Instance.Store;

            if (store != null)
            {
                string classId = store.Settings.ForexProviderClassId;
                if (!string.IsNullOrEmpty(classId))
                {
                    //TRY TO INSTANTIATE THE CLASS
                    try
                    {
                        instance = Activator.CreateInstance(Type.GetType(classId)) as IForexProvider;
                    }
                    catch
                    {
                        Logger.Error("Could not create Forex provider " + classId);
                        instance = null;
                    }
                }
            }
            return(instance);
        }