示例#1
0
            /// <summary>
            /// Returns the valid exchange rate for a given pair of currencies.
            /// </summary>
            /// <param name="context">The request context.</param>
            /// <param name="fromCurrency">The from currency.</param>
            /// <param name="toCurrency">The to currency.</param>
            /// <returns>The unrounded exchange rate.</returns>
            private static GetExchangeRateServiceResponse GetExchangeRate(RequestContext context, string fromCurrency, string toCurrency)
            {
                if (fromCurrency.Trim().ToLower() == toCurrency.Trim().ToLower())
                {
                    return(new GetExchangeRateServiceResponse(fromCurrency, toCurrency, 1M));
                }

                decimal exchangeRate = 0.00M;

                DateTime channelDateTime = context.GetNowInChannelTimeZone().DateTime;

                GetExchangeRatesDataRequest dataRequest = new GetExchangeRatesDataRequest(fromCurrency, toCurrency, channelDateTime);

                dataRequest.QueryResultSettings = QueryResultSettings.AllRecords;
                var exchangeRates = context.Execute <EntityDataServiceResponse <ExchangeRate> >(dataRequest).PagedEntityCollection.Results;

                // try to find forward exchange rate
                var rate = exchangeRates.SingleOrDefault(er => (er.FromCurrency == fromCurrency) && (er.ToCurrency == toCurrency));

                if (rate != null)
                {
                    exchangeRate = rate.Rate / 100M;
                }
                else
                {
                    // otherwise try to find backward exchange rate and reciprocate it
                    rate = exchangeRates.SingleOrDefault(er => (er.FromCurrency == toCurrency) && (er.ToCurrency == fromCurrency));
                    if (rate != null)
                    {
                        exchangeRate = 100M / rate.Rate;
                    }
                }

                return(new GetExchangeRateServiceResponse(fromCurrency, toCurrency, exchangeRate));
            }
示例#2
0
            /// <summary>
            /// Gets the exchange rate active on given date between these currencies on this channel.
            /// </summary>
            /// <param name="request">The data service request with the to and from currencies and active date.</param>
            /// <returns>The data service response with up to two exchange rates, which are forward and backward rates between the currencies.</returns>
            public EntityDataServiceResponse <ExchangeRate> GetExchangeRates(GetExchangeRatesDataRequest request)
            {
                ThrowIf.Null(request, "request");
                ThrowIf.NullOrWhiteSpace(request.FromCurrency, "fromCurrency");
                ThrowIf.NullOrWhiteSpace(request.ToCurrency, "toCurrency");

                ReadOnlyCollection <ExchangeRate> result;

                CurrencyDataManager manager = new CurrencyDataManager(request.RequestContext);

                CurrencyL2CacheDataStoreAccessor level2CacheDataAccessor = (CurrencyL2CacheDataStoreAccessor)manager.DataStoreManagerInstance.RegisteredAccessors[DataStoreType.L2Cache];

                bool updateL2Cache = DataStoreManager.DataStores[DataStoreType.L2Cache].Policy.MustUpdateOnMiss;

                result         = level2CacheDataAccessor.GetExchangeRates(request.FromCurrency, request.ToCurrency, request.ActiveDate);
                updateL2Cache &= result == null;

                if (result == null)
                {
                    ParameterSet parameters = new ParameterSet();

                    parameters[FromCurrencyVariable] = request.FromCurrency;
                    parameters[ToCurrencyVariable]   = request.ToCurrency;
                    parameters[ActiveDateVariable]   = request.ActiveDate;

                    using (SqlServerDatabaseContext sqlServerDatabaseContext = new SqlServerDatabaseContext(request))
                    {
                        result = sqlServerDatabaseContext.ExecuteNonPagedStoredProcedure <ExchangeRate>(GetExchangeRateProcedureName, parameters);
                    }

                    updateL2Cache &= result != null;
                }

                if (updateL2Cache)
                {
                    level2CacheDataAccessor.PutExchangeRates(request.FromCurrency, request.ToCurrency, request.ActiveDate, result);
                }

                return(new EntityDataServiceResponse <ExchangeRate>(result.AsPagedResult()));
            }