comparison Stocks/Stocks.Service/StocksService.cs @ 2:29ed98d659e9

Adding WebClientShim files
author stevenh7776 stevenhollidge@hotmail.com
date Mon, 20 Feb 2012 22:04:50 +0700
parents e5d46bb6cdb0
children 57f20ba55884
comparison
equal deleted inserted replaced
1:bd448bb6e0ba 2:29ed98d659e9
1 using System; 1 using System;
2 using System.Collections.Concurrent;
3 using System.Collections.Generic; 2 using System.Collections.Generic;
4 using System.Diagnostics; 3 using System.Diagnostics;
5 using System.Linq; 4 using System.Linq;
6 using System.Net;
7 using System.Threading.Tasks; 5 using System.Threading.Tasks;
8 using NLog; 6 using NLog;
9 using Stocks.Common; 7 using Stocks.Common;
8 using Stocks.Common.Events;
10 using Stocks.Common.Models; 9 using Stocks.Common.Models;
11 using Stocks.Common.Events;
12 10
13 namespace Stocks.Service 11 namespace Stocks.Service
14 { 12 {
15 public class StocksService : IStocksService 13 public class StocksService : IStocksService
16 { 14 {
18 16
19 private IList<Company> _companies; 17 private IList<Company> _companies;
20 private IConfigurationService _configurationService; 18 private IConfigurationService _configurationService;
21 private List<Price> _currentPrices; 19 private List<Price> _currentPrices;
22 private string _serviceUrl; 20 private string _serviceUrl;
23 private WebClient _webClient; 21 private IWebClientShim _webClient;
24 22
25 public bool IsActive { get; private set; } 23 public bool IsActive { get; private set; }
26 public SummaryStats Stats { get; private set; } 24 public SummaryStats Stats { get; private set; }
27 25
28 public StocksService(IConfigurationService configurationService) 26 public StocksService(
27 IConfigurationService configurationService,
28 IWebClientShim webClientShim)
29 { 29 {
30 new AssemblyInit(); 30 new AssemblyInit();
31 31
32 _webClient = new WebClient(); 32 _webClient = webClientShim;
33 _configurationService = configurationService; 33 _configurationService = configurationService;
34 _companies = configurationService.GetCompanies(); 34 Stats = new SummaryStats();
35
36 GetCompanyList();
37 }
38
39 private void GetCompanyList()
40 {
41 _companies = _configurationService.GetCompanies();
35 42
36 string symbolsCsv = _companies.Select( 43 string symbolsCsv = _companies.Select(
37 c => c.Symbol).Aggregate((c, d) => c + "," + d); 44 c => c.Symbol).Aggregate((c, d) => c + "," + d);
38 _serviceUrl = _configurationService.GetServiceUrl(symbolsCsv); 45 _serviceUrl = _configurationService.GetServiceUrl(symbolsCsv);
39
40 Stats = new SummaryStats();
41 } 46 }
42 47
43 public delegate void PriceChangedEventHandler(object sender, PriceChangedEventArgs e); 48 public delegate void PriceChangedEventHandler(object sender, PriceChangedEventArgs e);
44 public event PriceChangedEventHandler PriceChanged; 49 public event PriceChangedEventHandler PriceChanged;
45 protected virtual void RaisePriceChanged(Price price) 50 protected virtual void RaisePriceChanged(Price price)
108 if (localPrice == null) { 113 if (localPrice == null) {
109 _currentPrices.Add(new Price(webPrice.Symbol, webPrice.CurrentPrice, webPrice.PreviousPrice)); 114 _currentPrices.Add(new Price(webPrice.Symbol, webPrice.CurrentPrice, webPrice.PreviousPrice));
110 continue; 115 continue;
111 } 116 }
112 117
113 if (localPrice.Equals(webPrice)) { 118 if (localPrice.Equals(webPrice))
114 continue; 119 continue;
115 } 120 else
116 else { 121 UpdateLocalPrice(webPrice, localPrice);
117 localPrice.PreviousPrice = localPrice.CurrentPrice;
118 localPrice.CurrentPrice = webPrice.CurrentPrice;
119 RaisePriceChanged(localPrice);
120 }
121 } 122 }
123 }
124
125 private void UpdateLocalPrice(Price webPrice, Price localPrice)
126 {
127 localPrice.PreviousPrice = localPrice.CurrentPrice;
128 localPrice.CurrentPrice = webPrice.CurrentPrice;
129 RaisePriceChanged(localPrice);
122 } 130 }
123 131
124 private void UpdateStats(Stopwatch timeToDownload, string webResponse) 132 private void UpdateStats(Stopwatch timeToDownload, string webResponse)
125 { 133 {
126 Stats.LastWebRequest.Duration = (int)timeToDownload.ElapsedMilliseconds; 134 Stats.LastWebRequest.Duration = (int)timeToDownload.ElapsedMilliseconds;