Mercurial > silverbladetech
diff Stocks/Stocks.Service/StocksService.cs @ 23:399398841fd0
Working version for Stocks (including loosely coupled components
author | adminsh@apollo |
---|---|
date | Tue, 20 Mar 2012 16:53:29 +0000 |
parents | 6e84a4c92378 |
children |
line wrap: on
line diff
--- a/Stocks/Stocks.Service/StocksService.cs Tue Mar 20 15:07:49 2012 +0000 +++ b/Stocks/Stocks.Service/StocksService.cs Tue Mar 20 16:53:29 2012 +0000 @@ -10,136 +10,146 @@ using Stocks.Common.Models; namespace Stocks.Service -{ - public class StocksService : IStocksService - { - private static readonly Logger Log = LogManager.GetCurrentClassLogger(); +{ + public class StocksService : IStocksService + { + #region Delegates - private IList<Company> _companies; - private IConfigurationService _configurationService; - private List<Price> _currentPrices; - private string _serviceUrl; - private IWebClientShim _webClient; + public delegate void PriceChangedEventHandler(object sender, PriceChangedEventArgs e); - public bool IsActive { get; private set; } - public SummaryStats Stats { get; private set; } + #endregion + + private static readonly Logger Log = LogManager.GetCurrentClassLogger(); - public StocksService( - IConfigurationService configurationService, - IWebClientShim webClientShim) - { - new AssemblyInit(); - - _webClient = webClientShim; - _configurationService = configurationService; - Stats = new SummaryStats(); + private readonly IConfigurationService _configurationService; + private readonly IWebClientShim _webClient; + private IList<Company> _companies; + private List<Price> _currentPrices; + private string _serviceUrl; - GetCompanyList(); - } - - private void GetCompanyList() - { - _companies = _configurationService.GetCompanies(); - - string symbolsCsv = _companies.Select( - c => c.Symbol).Aggregate((c, d) => c + "," + d); - _serviceUrl = _configurationService.GetServiceUrl(symbolsCsv); - } + public StocksService( + IConfigurationService configurationService, + IWebClientShim webClientShim) + { + new AssemblyInit(); - public event PriceChangedEventHandler PriceChanged; - public delegate void PriceChangedEventHandler(object sender, PriceChangedEventArgs e); - protected virtual void OnPriceChanged(Price price) - { - Stats.PriceChangeEvents++; + _webClient = webClientShim; + _configurationService = configurationService; + Stats = new SummaryStats(); - if (PriceChanged != null) - PriceChanged(this, new PriceChangedEventArgs() { Price = price }); - } + GetCompanyList(); + } - public void Start() - { - PrepareForServiceStarting(); - Task.Factory.StartNew(() => DownloadPrices()); - } + public SummaryStats Stats { get; private set; } + + #region IStocksService Members - public void Stop() - { - IsActive = false; - Log.Debug("StockService stopped"); - } + public bool IsActive { get; private set; } + + public void Start() + { + PrepareForServiceStarting(); + Task.Factory.StartNew(DownloadPrices); + } - private void PrepareForServiceStarting() - { - Log.Debug("StockService starting"); - IsActive = true; - Stats.Reset(); - _currentPrices = new List<Price>(_companies.Count); - } + public void Stop() + { + IsActive = false; + Log.Debug("StockService stopped"); + } - private void DownloadPrices() - { - try - { - Stopwatch timeToDownload; - - while (IsActive) - { - string webResponse = TimedDelegates.Execute<string>( - _webClient.DownloadString, - _serviceUrl, - out timeToDownload); + #endregion - PopulatePricesFromWebResponse(webResponse); - UpdateStats(timeToDownload, webResponse); - } - } - catch (Exception e) - { - Log.Error("Exception during DownloadPrices()"); - Log.Error("Stack Trace {0}: /r/nException Message: {1}", e.StackTrace, e.Message); - this.Stop(); - } - } + private void GetCompanyList() + { + _companies = _configurationService.GetCompanies(); + + string symbolsCsv = _companies.Select( + c => c.Symbol).Aggregate((c, d) => c + "," + d); - private void PopulatePricesFromWebResponse(string webResponse) - { - string[] webPrices = webResponse.Split( - new string[] { "\n", "\r\n" }, - StringSplitOptions.RemoveEmptyEntries); + _serviceUrl = _configurationService.GetServiceUrl(symbolsCsv); + } + + public event PriceChangedEventHandler PriceChanged; - foreach (string webPriceData in webPrices) - { - var webPrice = Factory.CreatePrice(webPriceData); - var localPrice = _currentPrices.Find(x => x.Symbol == webPrice.Symbol); + protected virtual void OnPriceChanged(Price price) + { + Stats.PriceChangeEvents++; - if (localPrice == null) { - _currentPrices.Add(new Price(webPrice.Symbol, webPrice.CurrentPrice, webPrice.PreviousPrice)); - continue; + if (PriceChanged != null) + PriceChanged(this, new PriceChangedEventArgs {Price = price}); } - if (localPrice.Equals(webPrice)) - continue; - else - UpdateLocalPrice(webPrice, localPrice); - } - } + private void PrepareForServiceStarting() + { + Log.Debug("StockService starting"); + IsActive = true; + Stats.Reset(); + _currentPrices = new List<Price>(_companies.Count); + } + + private void DownloadPrices() + { + try + { + while (IsActive) + { + Stopwatch timeToDownload; + string webResponse = TimedDelegates.Execute( + _webClient.DownloadString, + _serviceUrl, + out timeToDownload); + + PopulatePricesFromWebResponse(webResponse); + UpdateStats(timeToDownload, webResponse); + } + } + catch (Exception e) + { + Log.Error("Exception during DownloadPrices()"); + Log.Error("Stack Trace {0}: /r/nException Message: {1}", e.StackTrace, e.Message); + Stop(); + } + } - private void UpdateLocalPrice(Price webPrice, Price localPrice) - { - localPrice.PreviousPrice = localPrice.CurrentPrice; - localPrice.CurrentPrice = webPrice.CurrentPrice; - OnPriceChanged(localPrice); + private void PopulatePricesFromWebResponse(string webResponse) + { + string[] webPrices = webResponse.Split( + new[] {"\n", "\r\n"}, + StringSplitOptions.RemoveEmptyEntries); + + foreach (string webPriceData in webPrices) + { + Price webPrice = Factory.CreatePrice(webPriceData); + Price localPrice = _currentPrices.Find(x => x.Symbol == webPrice.Symbol); + + if (localPrice == null) + { + _currentPrices.Add(new Price(webPrice.Symbol, webPrice.CurrentPrice, webPrice.PreviousPrice)); + continue; + } + + if (!localPrice.Equals(webPrice)) + UpdateLocalPrice(webPrice, localPrice); + } + } + + private void UpdateLocalPrice(Price webPrice, Price localPrice) + { + localPrice.PreviousPrice = localPrice.CurrentPrice; + localPrice.CurrentPrice = webPrice.CurrentPrice; + OnPriceChanged(localPrice); + } + + private void UpdateStats(Stopwatch timeToDownload, string webResponse) + { + Stats.LastWebRequest.Duration = (int) timeToDownload.ElapsedMilliseconds; + Stats.LastWebRequest.PricesDownloaded = _currentPrices.Count; + Stats.LastWebRequest.Response = webResponse; + Stats.LastWebRequest.Request = _serviceUrl; + Stats.LastWebRequest.SymbolCount = _companies.Count; + Stats.NumberOfRequests++; + Log.Trace(Stats); + } } - - private void UpdateStats(Stopwatch timeToDownload, string webResponse) - { - Stats.LastWebRequest.Duration = (int)timeToDownload.ElapsedMilliseconds; - Stats.LastWebRequest.PricesDownloaded = _currentPrices.Count; - Stats.LastWebRequest.Response = webResponse; - Stats.LastWebRequest.Request = _serviceUrl; - Stats.LastWebRequest.SymbolCount = _companies.Count; - Stats.NumberOfRequests++; - Log.Trace(Stats); - } - } } \ No newline at end of file