changeset 2:29ed98d659e9

Adding WebClientShim files
author stevenh7776 stevenhollidge@hotmail.com
date Mon, 20 Feb 2012 22:04:50 +0700
parents bd448bb6e0ba
children ef54074d5285
files .hgignore Stocks/Stocks.Common/IWebClientShim.cs Stocks/Stocks.Common/Stocks.Common.csproj Stocks/Stocks.Common/WebClientShim.cs Stocks/Stocks.Service.Tests.Unit/StockServiceTests.cs Stocks/Stocks.Service/StocksService.cs
diffstat 6 files changed, 70 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Mon Feb 20 21:53:40 2012 +0700
+++ b/.hgignore	Mon Feb 20 22:04:50 2012 +0700
@@ -1,5 +1,6 @@
 syntax: glob
-*.csproj.user */obj/*
-*/bin/*
-*.ncb
+*/bin/*
+*.ncb
 *.suo
+*.csproj.user
+*/obj/*
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Stocks/Stocks.Common/IWebClientShim.cs	Mon Feb 20 22:04:50 2012 +0700
@@ -0,0 +1,7 @@
+namespace Stocks.Common
+{
+  public interface IWebClientShim
+  {
+    string DownloadString(string address);
+  }
+}
--- a/Stocks/Stocks.Common/Stocks.Common.csproj	Mon Feb 20 21:53:40 2012 +0700
+++ b/Stocks/Stocks.Common/Stocks.Common.csproj	Mon Feb 20 22:04:50 2012 +0700
@@ -53,6 +53,7 @@
     <Compile Include="IConfigurationService.cs" />
     <Compile Include="Exceptions\InvalidWebPriceData.cs" />
     <Compile Include="IStocksService.cs" />
+    <Compile Include="IWebClientShim.cs" />
     <Compile Include="Models\Company.cs" />
     <Compile Include="Models\Price.cs" />
     <Compile Include="Models\Quote.cs" />
@@ -60,6 +61,7 @@
     <Compile Include="Models\WebRequestStats.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="Core\FileSerializer.cs" />
+    <Compile Include="WebClientShim.cs" />
   </ItemGroup>
   <ItemGroup />
   <ItemGroup>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Stocks/Stocks.Common/WebClientShim.cs	Mon Feb 20 22:04:50 2012 +0700
@@ -0,0 +1,23 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Net;
+
+namespace Stocks.Common
+{
+  public class WebClientShim : IWebClientShim
+  {
+    private readonly WebClient _webClient;
+
+    public WebClientShim(WebClient webClient)
+    {
+      _webClient = webClient;
+    }
+    
+    public string DownloadString(string address)
+    {
+      return _webClient.DownloadData(address).ToString();
+    }
+  }
+}
--- a/Stocks/Stocks.Service.Tests.Unit/StockServiceTests.cs	Mon Feb 20 21:53:40 2012 +0700
+++ b/Stocks/Stocks.Service.Tests.Unit/StockServiceTests.cs	Mon Feb 20 22:04:50 2012 +0700
@@ -6,6 +6,7 @@
 using Stocks.Common;
 using System.Threading.Tasks;
 using System.Threading;
+using System.Net;
 
 namespace Stocks.Service.Tests.Unit
 {
@@ -15,13 +16,20 @@
     public void Service_IsActive_property()
     {
       var file = "../../../Stocks.Service/companyData.json";
-      var service = new StocksService(new ConfigurationService(file));
+      var configurationService = new ConfigurationService(file);
+      var webClientShim = new WebClientShim(new WebClient());
+      
+      var service = new StocksService(
+        configurationService, 
+        webClientShim);
+
+      Assert.Equal(false, service.IsActive);
 
       using (var task = Task.Factory.StartNew(() =>
         {
-          Assert.Equal(false, service.IsActive);
           service.Start();
           Assert.Equal(true, service.IsActive);
+      
           using (var task2 = Task.Factory.StartNew(() => Thread.Sleep(500)))
             { task2.Wait(); }
           service.Stop();
--- a/Stocks/Stocks.Service/StocksService.cs	Mon Feb 20 21:53:40 2012 +0700
+++ b/Stocks/Stocks.Service/StocksService.cs	Mon Feb 20 22:04:50 2012 +0700
@@ -1,14 +1,12 @@
 using System;
-using System.Collections.Concurrent;
 using System.Collections.Generic;
 using System.Diagnostics;
 using System.Linq;
-using System.Net;
 using System.Threading.Tasks;
 using NLog;
 using Stocks.Common;
+using Stocks.Common.Events;
 using Stocks.Common.Models;
-using Stocks.Common.Events;
 
 namespace Stocks.Service
 { 
@@ -20,24 +18,31 @@
     private IConfigurationService _configurationService;
     private List<Price> _currentPrices;
     private string _serviceUrl;
-    private WebClient _webClient;
+    private IWebClientShim _webClient;
 
     public bool IsActive { get; private set; }
     public SummaryStats Stats { get; private set; }
 
-    public StocksService(IConfigurationService configurationService)
+    public StocksService(
+      IConfigurationService configurationService,
+      IWebClientShim webClientShim)
     {
       new AssemblyInit();
 
-      _webClient = new WebClient();
+      _webClient = webClientShim;
       _configurationService = configurationService;
-      _companies = configurationService.GetCompanies();
+      Stats = new SummaryStats();
+
+      GetCompanyList();
+    }
+
+    private void GetCompanyList()
+    {
+      _companies = _configurationService.GetCompanies();
 
       string symbolsCsv = _companies.Select(
         c => c.Symbol).Aggregate((c, d) => c + "," + d);
       _serviceUrl = _configurationService.GetServiceUrl(symbolsCsv);
-      
-      Stats = new SummaryStats();
     }
 
     public delegate void PriceChangedEventHandler(object sender, PriceChangedEventArgs e);
@@ -110,17 +115,20 @@
           continue;
         }
 
-        if (localPrice.Equals(webPrice)) {
+        if (localPrice.Equals(webPrice))
           continue;
-        }
-        else {
-          localPrice.PreviousPrice = localPrice.CurrentPrice;
-          localPrice.CurrentPrice = webPrice.CurrentPrice;
-          RaisePriceChanged(localPrice); 
-        }
+        else
+          UpdateLocalPrice(webPrice, localPrice);
       }
     }
 
+    private void UpdateLocalPrice(Price webPrice, Price localPrice)
+    {
+      localPrice.PreviousPrice = localPrice.CurrentPrice;
+      localPrice.CurrentPrice = webPrice.CurrentPrice;
+      RaisePriceChanged(localPrice);
+    }
+
     private void UpdateStats(Stopwatch timeToDownload, string webResponse)
     {
       Stats.LastWebRequest.Duration = (int)timeToDownload.ElapsedMilliseconds;