コード例 #1
0
        /// <summary>
        /// Refreshes data using the market data provider
        /// </summary>
        private void GetLatestQuotes()
        {
            lock (_subscriptionDataLock)
            {
                // stop timer while data is retrieved
                Stop();

                if (_subscriptionData.IsActiveForCurrentTimeOfDay)
                {
                    try
                    {
                        // get the list of stocks for which to get quotes
                        var stocks = _stockListProvider.GetStocks(_subscriptionData);

                        // get quotes from provider
                        var quotes = _marketDataProvider.GetQuotes(stocks);

                        if (quotes != null)
                        {
                            using (var repository = _repositoryFactory.CreateRepository())
                            {
                                foreach (var quote in quotes)
                                {
                                    // set created date
                                    quote.SubscriptionID = _subscriptionData.ID;
                                    quote.Created        = DateTime.Now;

                                    // add to repository
                                    repository.StockQuotes.Add(quote);
                                }

                                // save
                                repository.SaveChanges();
                            }

                            // if a publisher was provided, publish new quote data
                            if (_quotesPublisher != null)
                            {
                                _quotesPublisher.Publish(new NewQuotesData
                                {
                                    SubscriptionId = _subscriptionData.ID.ToString(CultureInfo.InvariantCulture),
                                    Quotes         = quotes.ToList()
                                });
                            }
                        }
                    }
                    catch (Exception exception)
                    {
                        // log the exception
                        _logger.Error(Resources.SubscriptionUpdateException, exception);
                    }
                }

                // restart the timer now
                Start();
            }
        }